-
Notifications
You must be signed in to change notification settings - Fork 10
World
A World contains and wires together Entities and Systems. When something happens, such as adding/removing an Entity or ticking time forward, corresponding methods on the Systems are called. The World also exposes methods to export its Entites as Rooms.
var world = new hitagi.World();
world.register(new hitagi.systems.VelocitySystem());
var entity = new hitagi.Entity()
.attach(new hitagi.components.Position({
x: 0,
y: 0
}))
.attach(new hitagi.components.Velocity({
xspeed: 0,
yspeed: 0
}));
world.add(entity);
requestAnimationFrame(animate);
function animate() {
world.tick(1000);
requestAnimationFrame(animate);
}
parameters:
none
add: Add an Entity to the World. All registered Systems build the Entity and track it if they want to.
parameters:
entity - Entity to be added.
return: World, for chaining
remove: Remove an Entity from the World. All registered Systems destroy the Entity and stop tracking it.
parameters:
entity - Entity to be removed.
return: none
register: Register a System to the World. This builds and tracks all Entities, so should happen before Entities are added to the World if possible for performance reasons.
parameters:
system - System to be registered.
return: System that's just been registered
deregister: Deregister a System from the World.
parameters:
system - System to be deregistered.
return: nothing
rebuild: Destroy and untrack an Entity from all registered Systems, then build and track it again. This is used when an Entity goes under a large enough change that it needs to undergo some 'initialisation' again. Setting the 'just' parameter to a Component ID causes only build/destroy methods specific to that Component.
parameters:
entity - Entity to be rebuilt.
optional parameters:
just - Component ID of systems to only rebuild in.
return: nothing
clear: Remove all Entities from the World.
parameters:
none
return: nothing
save: Export all Entities in the World to a Room.
parameters:
none
return: Saved Room
load: Load a Room into the World. Does not clear the World, so Entities are appended to existing ones.
parameters:
room - Room to load.
return: nothing