Jump to content

Component Entity System Framework


granquesote
 Share

Recommended Posts

Hi guys, I'm new in the forum and relatively new in game development.

In the last months I began working on a small framework based in the Component-Entity-System paradigm. It was hard to understand the logic behind that paradigm in the begining, but a magical day my mind made a click and everything is clear since then.

In the project, the Entity is only an ID that act like a foreign key in components tables, the components are all reciclables and the update systems works over groups of components.

I'm hosting it in https://bitbucket.org/fhgarcia/cesjs. There are some tests in the root folder and an example in the examples folder (I'm working in more examples).

I'll be glad, if you guys, can give me feedback and recomendations about the project.

Thx.

Link to comment
Share on other sites

  • 2 weeks later...

I want to make an small example of how I'm implementing the ECS in my games:

These are the components, small objects with no methods, pure data:

function Position() {}Position.prototype.initialize = function (x, y) {    this.x = x;    this.y = y;};function Velocity() {}Velocity.prototype.initialize = function (x, y) {    this.x = x;    this.y = y;};function Gravity() {};Gravity.prototype.initialize = function (force) {    this.force = force;}

These are the updates that we will register to the engine:

function updateVelocity(engine, gravity, velocity) {    velocity.y += gravity.force * engine.dt;}function updatePosition(engine, velocity, position) {    position.x += velocity.x * engine.dt;    position.y += velocity.y * engine.dt;}

This is the engine initialization, it require a preallocation of components (10 of each one in this examples) and the registration of the updates methods (with an array of component types to filter the entities).

var engine = new cesjs.Engine();engine.createComponents(Position, 10);engine.createComponents(Velocity, 10);engine.createComponents(Gravity, 10);engine.addUpdate(updateVelocity, [Gravity, Velocity]);engine.addUpdate(updatePosition, [Velocity, Position]);engine.start();

Then we can create the entities:

var entityA = engine.createEntity();engine.addComponentToEntity(Position, entityA).initialize(0, 0);engine.addComponentToEntity(Velocity, entityA).initialize(1, 0);engine.addComponentToEntity(Gravity, entityA).initialize(-9.8);var entityB = engine.createEntity(); engine.addComponentToEntity(Position, entityB).initialize(0, 0);engine.addComponentToEntity(Velocity, entityB).initialize(1, 0);

It's time to run the engine update with a delta time argument and see the results:

engine.update(1); engine.getComponentFromEntity(Position, entityA); // x = 1; y = -9.8;engine.getComponentFromEntity(Position, entityB); // x = 1; y = 0;

The updateVelocity only runs over the entity that has the Gravity component (entityA) and it updates the velocity component with the gravity force.
The updatePosition runs over both entities, because they have the Velocity and Position components and it updates the Position component.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...