This plugin aims to bring the component pattern, like in the Unity 3D engine, to Impact Entities.
A component is a piece of reusable code that can be (un)plugged to any Entity, at runtime. The component will then have access to the Entity's attributes.
Download the file lib/plugins/component.js
and drop it in your project.
Create your components by subclassing the Component class, and require them in your entities, like any other dependency.
Basically, your components are defined like any other Impact ig.Class
. You may pass a settings
object at initialization
that will override default values.
The settings
object cannot be used to create new attributes or overwrite functions.
Once the instance is created, simply inject it into your entity.
This project is also provided with a simple example: an Entity (a red square) is given a "clickable" component, that outputs a console.log each time the left mouse button is pressed down.
Each component references its linked entity via the delegate
attribute.
If you have any experience with Unity 3D, the delegate
is the equivalent of gameObject
.
var component = new MyComponentSubclass(parameters));
entity.addComponent(component);
entity.removeComponent(component);
var components = entity.getComponents(MyComponentSubclass); // Retrieve an array containing all components that match the class
var component = entity.getComponents(MyComponentSubclass, true); // Retrieve only the first component to match the class
var component = entity.getCmp(MyComponentSubclass); // Shorthand for the above line
Components have four built-in events.
added()
is called just afteraddComponent()
update()
is called just after the component'supdate()
calldraw()
, likeupdate()
, is called just after the component'sdraw()
calldrawOver()
will be called after theig.game
draw()
function. Useful to draw UI elements like health bars.