r3-legacy/src/game-lib-system-physics.js

96 lines
1.9 KiB
JavaScript

/**
* System takes care of updating all the entities (based on their component data)
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System.Physics = function(
apiSystem
) {
GameLib.System.call(
this,
apiSystem
);
this.worldSubscription = null;
this.rigidBodySubscription = null;
};
GameLib.System.Physics.prototype = Object.create(GameLib.System.prototype);
GameLib.System.Physics.prototype.constructor = GameLib.System.Physics;
GameLib.System.Physics.prototype.updateWorlds = function() {
this.worlds = GameLib.EntityManager.Instance.query(
[
GameLib.D3.World
]
);
};
GameLib.System.Physics.prototype.updateRigidBodies = function() {
this.rigidBodyEntities = GameLib.EntityManager.Instance.query(
[
GameLib.D3.RigidBody
]
);
};
GameLib.System.Physics.prototype.start = function() {
this.worldSubscription = this.subscribe(
GameLib.Event.WORLD_INSTANCE_CREATED,
this.updateWorlds
);
this.rigidBodySubscription = this.subscribe(
GameLib.Event.RIGID_BODY_INSTANCE_CREATED,
this.updateRigidBodies
);
this.updateWorlds();
this.updateRigidBodies();
};
/**
* Update script
*/
GameLib.System.Physics.prototype.update = function() {
/**
* TODO: progress the world according to delta time
*/
this.worlds.map(function(
world
){
world.step(1 / 60.0);
});
this.rigidBodyEntities.map(
function (rigidBody) {
/**
* TODO: update mesh position based on rigidbody position
*/
}
);
};
GameLib.System.Physics.prototype.stop = function() {
this.worlds = [];
this.rigidBodyEntities = [];
if (this.worldSubscription) {
this.worldSubscription.remove();
}
if (this.rigidBodySubscription) {
this.rigidBodySubscription.remove();
}
};