Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Last active April 10, 2017 02:14
Show Gist options
  • Save agmcleod/3e901d45cd97335aa3d0 to your computer and use it in GitHub Desktop.
Save agmcleod/3e901d45cd97335aa3d0 to your computer and use it in GitHub Desktop.
MelonJS ECS Proposal

Melonjs ECS proposal

After working with unity a fair bit, I've come to appreciate the modular code base. Components have an expected api, and provide a single thing to the parent object or entity.

The problems with the current setup.

  • It can be hard to know what objects you need. Renderable/Sprite/Animation Sheet
  • Entity is the only thing that has physics, defaults to an animation sheet.
  • Sprite is the only thing that provides scale and rotation, plus subclasses

Proposed solution

There is one base class type that can be added to the scene. That is: me.Entity

This class will have:

  • children[]
  • components[]

This means me.Container would simply go away. Any entity can have children.

From there we can change existing classes to be simple components.

An instance of me.Entity will have the Transform component by default.

Transform

  • position
  • rotation
  • scale
  • width & height

The entity uses the transform to apply those values to the renderer, before executing then draw step. Once the draw step is done, and children have drawn, the transform will be undone.

In the engine, _absPos and me.ObservableVector2d are used to maintain a world position of a given renderable. The transform will then use properties to maintain the world position. The world position is needed for collisions, and quad tree data.

Sprite

This uses the renderer to draw its image.

AnimationSheet

Maintains management of animations and animation state. It determines which frame to be drawn.

Body

Works similar to now, but is not called manually. It becomes a component that has update().

What this means is that tiled objects can be simple entities that create a body & Sprite/animationsheet.

Renderable

This becomes obsolete. Any custom drawing users wish to do, they can just create their own component.

Game loop

The game loop will involve executing mounted systems. Including a few built in:

  • Animation System - This runs against animation components to update their current frame.
  • Physics system - Runs body components using their shapes to check collisions. Does look ups via quad tree.
  • Drawing - This runs last after all other systems. It goes through all entities, applying the transform component, and drawing components that implement a draw function. As it goes through children, it keeps the previous transform applied.

The entity update calls its components as needed for information. Updates position, and other transform info, etc.

It's worth noting the camera will be an entity part of the system, so one can attach things to the camera.

Things to figure out still

  • How this changes/effects the quad tree
  • Resolving absolute position, re-think how we do it with containers now.
  • How someone can apply a shader to a single entity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment