Game Development Reference
In-Depth Information
var Packt = Packt || {};
Packt.Components = Packt.Components || {};
Packt.Components.Move = function(entity, speed)
{
var entity = entity;
var speed = speed;
var direction = new Packt.Vec2(0, 0);
// Move the entity in the direction it is
facing by a constantspeed
this.update = function() {
var pos = entity.getPosition();
direction.normalize();
var newPos = {
x: pos.x + direction.get("x") * speed,
y: pos.y + direction.get("y") * speed
};
entity.setPosition(newPos);
};
// Allow the input mechanism to tell the
entity where to move
this.setDirection = function(x, y) {
direction.set(x, y);
};
};
Now, the way that both a player and an enemy can use this same Move component
to move their entities is slightly different. In the case of an enemy, we can simply
create some raw artificial intelligence to set the direction of the enemy's entity every
so often and the Move component takes care of updating the entity's position as
needed.
In order to make the player's ship move, however, we want the player himself or her-
self to tell the entity where to go. To accomplish this, we simply create an input com-
Search WWH ::




Custom Search