Game Development Reference
In-Depth Information
and the player's entity itself. Then, inside its update method, it checks for collision
between all of those entities.
Vec2.js
Since the physics engine of this game makes extensive use of vector structures
and since JavaScript doesn't provide native vector data types, we decided to create
our own. This simple class represents a vector with two components and provides a
function to normalize the vector. This is especially useful when we want to move an
entity in whichever direction it faces.
main.js
Finally, we bring it all together in a file that we might as well call main.js . This file
looks an awful lot like me when I go to a fast food restaurant: take one of everything
and see how it all goes together. First we instantiate a canvas object, then the player
entity, an EnemyManager object, a PhysicsManager object, and finally, a game
loop object. After everything is wired up, we start the game loop and the game is set
in motion.
(function main(){
var WIDTH = document.body.offsetWidth;
var HEIGHT = document.body.offsetHeight;
var MAX_ENEMIES = 100;
// The main canvas where the game is rendered
var canvas = new Packt.Canvas(WIDTH, HEIGHT);
document.body.appendChild(canvas.getCanvas());
// The energy widget
var playerEnergy = new
Packt.Widgets.EnergyBar("energyBar");
document.body.appendChild(playerEnergy.getElement());
// The player entity, along with its required
components
Search WWH ::




Custom Search