-
Notifications
You must be signed in to change notification settings - Fork 0
Components
M.K edited this page Jul 9, 2017
·
8 revisions
Component is a bag of data. No actual functionality will ever go here. This data can describe various things, like position of object, its velocity, physical properties, graphics sprite etc. You can compose various things from them. Let's assume we want to make a game about bouncing balls. To describe moving ball we'll need (for example) these components:
- Position (x, y) - we have to know, where the ball is (it's not quantum mechanics, so this is actually well defined)
- Velocity (vx, vy) - every moving object has it
- Radius (r) - very important property of all circles and balls
- Sprite (texture) - ball's look
These components will be used to describe ball entity (more on this later). But before you can add component to entity, you have to register it:
game.registerComponent({
type: 'Position',
onCreate({x, y}) {
this.x = x;
this.y = y;
},
});
If only thing you are doing in onCreate
method is assigning properties from parameter object to properties of component, you can completly omit onCreate
method and Entropy will do all the hard work for you:
game.registerComponent({
type: 'Position',
})
Then you can add component to entity:
// somewhere in enitity's `onCreate` method...
this.add('Position', {
x: 1,
y: 1,
});