Game Development Reference
In-Depth Information
};
// This represents how much energy the entity
has left. When
// the energy gets to or below zero, the
entity dies
this.getEnergy = function() {
return energy;
};
// Update the entity's energy levels
this.takeDamage = function(damage) {
energy -= damage;
return energy;
};
};
The
LaserGun
component is slightly more involved because it contains a collection
of entities that it manages. Each time a laser beam is fired by the entity containing
the laser gun, a new entity is created to represent that laser beam. This entity is sim-
ilar to all the other entities in the game since it also contains a
sprite
component
to draw itself a
Move
component and a
physics
component as well.
Each time the laser gun updates itself, it needs to move all of its lasers forward
and remove any of its laser beams from its control if the laser has gone outside the
screen area.
var Packt = Packt || {};
Packt.Components = Packt.Components || {};
Packt.Components.LaserGun = function(entity,
canvas, maxShots) {
var entity = entity;
var MAX_SHOTS = maxShots;
var canvas = canvas;
var shots = new Array();
var shotsPerSec = 1000 / 15;