Game Development Reference
In-Depth Information
pos.x = -50;
entities[i].setPosition(pos);
}
} catch (e) {}
}
if (enemiesDeleted > 0) {
for (var i = 0; i < enemiesDeleted; i++) {
var offset = (Math.random() * 100 >> 0)
% (worldWidth / 75 >> 0);
var x = 50 * offset + 25 + (25 *
offset);
var y = 0 - Math.random() * 100 - 100;
addEnemies(x, y, {});
}
}
};
};
Because we are using a component-based architecture, these three tasks aren't
complex at all. In order to create a new entity, we simply instantiate the class and
add the necessary components it needs to function. To add variety to the game, we
can randomly assign a different sprite to each entity created as well as randomly
tweak the properties of each entity, such as making it move faster, cause more dam-
age, look bigger, and so on.
Removing dead entities is even easier. All we need to do is iterate over the list of
active entities and remove them from the list if the active flag for an entity is unset.
One thing we could also do is remove any entities that wander too far off the screen
area, so that we don't need to manage entities that can't possibly be hit by the play-
er's lasers.
Finally, the update function takes the job of updating each active entity's position (or
rather, it tells each entity to update its own position based on the direction they're
headed), simulate some basic artificial intelligence by moving each entity forward,
then removing any dead entities.
Search WWH ::




Custom Search