npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

surreal-engine

v0.4.0

Published

A typescript game engine

Downloads

8

Readme

Surreal Engine

NPM Package

A simple and powerful typescript game engine.

npm install --save surreal-engine

See examples at https://surreal-engine.onrender.com/examples

Usage

Let's create the simplest scene possible: A falling box. We have to initialize the engine and then create some entities based on existing templates. As a start create two boxes: one for the ground, and one for the falling box.

// Create a new instance of the engine with debug on
const engine = new Engine('#root', { debug: BasicDebug });
// Engine must be initialized before we can start adding components
await engine.init();

// Create a global lighting so our objects are visible
engine.new(GlobalLight, {
  color: '#ffffff',
  intensity: 0.5,
});

// Create a big white square as our ground
engine.new(Box, {
  size: new Vector3(25, 1, 25),
  mass: 0,
  restitution: 0.3,
  material: 'white',
  rigid: true,
  receiveShadow: true,
});

// Create a magenta box that will fall down.
engine.new(Box, {
  size: new Vector3(1, 1, 1),
  pos: new Vector3(0, 10, 0),
  mass: 0.5,
  restitution: 0.3,
  material: 'magenta',
  rigid: true,
  castShadow: true,
});

engine.start();

Now, let's add a ball after 1 second has passed.

engine.timer(() => {
  engine.new(Sphere, {
    radius: 1,
    pos: new Vector3(0, 10, 0),
    mass: 0.5,
    restitution: 0.3,
    material: 'yellow',
    rigid: true,
    castShadow: true,
  });
}, 1000, false);

Now, for some fun, let's make our ball more bouncy.

    mass: 0.5,
-   restitution: 0.3,
+   restitution: 2.5,
    material: 'yellow',
    rigid: true,

We now have a nice bouncy ball, yeeey. This still looks ugly. Notice the castShadow and receiveShadow options. Why aren't they working? It's because ambient lights cannot cast shadows. For that, we need a directionLight. So let's do that.

engine.new(DirectionalLight, {
  color: '#ffffff',
  intensity: 1,
  pos: new Vector3(-10, 10, 10),
  target: new Vector3(0, 0, 0),
  castShadow: true,
  shadowAreaHeight: 10,
  shadowAreaWidth: 10,
});

So much better. It would be nice if we could play around with the ball a bit, so let's add a character!

engine.new(Box, {
  size: new Vector3(2, 2, 2),
  pos: new Vector3(5, 0, 5),
  mass: 0.5,
  restitution: 0.5,
  material: 'red',
  rigid: true,
  castShadow: true,
  mods: [
    offsetCameraMod(),
    keyboardMotionMod(),
  ],
});

Entity templates can be moded to add new behaviour to them. That way, you can easily customize entities without having to create new entity templates. For our character, we want to apply the keyboardMotionMod, which allows us to move the character using WASD. Additionally, we want the camera to follow the character, so we apply the offsetCameraMod as well.

Note: Motion is not included in the base engine. It comes as a plugin instead. so we will have to register the plugin to the engine first:

engine.registerPlugin(new MotionPlugin());

Now we have nice red box that we can control. Let's have some more fun. Let's spawn a ball every 5 seconds, so we never run out of balls.

engine.timer(() => {
  engine.new(Sphere, {
    radius: 1,
    pos: new Vector3(0, 10, 0),
    mass: 0.5,
    restitution: 2.5,
    material: 'yellow',
    rigid: true,
    castShadow: true,
  });
- }, 1000, false);
+ }, 5000, true);

Check out a simple platformer tutorial here. (Work in Progress)

Alternatively take a look at the below examples to give you some ideas.

Examples