Game Development Reference
In-Depth Information
sY = y;
sWidth = width;
sHeight = height;
};
// Render the entity
this.update = function() {
if (ctx && entity.isActive()) {
var pos = entity.getPosition();
ctx.drawImage(img, sX, sY, sWidth,
sHeight, pos.x, pos.y,width, height);
}
};
// Return both values at once, instead of
using two getterfunctions
this.getSize = function() {
return {
width: width,
height: height
};
};
};
Once the functionality to render an entity is in place, we can now move on to adding
a component to allow the player to move the entity about the screen. Now, the whole
point of using components is to allow for maximum code reuse. In our case, we want
to reuse the component that makes a player move so that we can have each enemy
ship move about the game world using the same functionality.
To make the entity move, we use a very standard Move component which moves
the entity based on its direction vector and a constant speed at which the entity is to
move in this given direction. The Vec2 data type is a custom general purpose class
discussed later in the chapter. Basically, this class represents a vector where it holds
two variables representing the two components of a vector and defines a very handy
function to normalize the vector when needed.
Search WWH ::




Custom Search