Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Factor out Missiles class using Projectiles superclass.
  • Loading branch information
wtaysom committed Jun 7, 2012
1 parent 60e164b commit fd6960e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 56 deletions.
80 changes: 33 additions & 47 deletions game03/boss.js
Expand Up @@ -18,13 +18,13 @@ function drawBoss() {


/** Missiles **/ /** Missiles **/


var missiles = []; function Missiles() {
var maxMissiles = 30; Projectiles.call(this);
var maxMissileCoolDown = 20; }
var missileCoolDown = maxMissileCoolDown; Missiles.prototype = new Projectiles();


function addMissile() { Missiles.prototype.newMember = function() {
missiles.push({ return {
x: boss.x, x: boss.x,
y: sides(boss).bottom, y: sides(boss).bottom,
dx: randomBetween(-1, 2), dx: randomBetween(-1, 2),
Expand All @@ -33,56 +33,42 @@ function addMissile() {
height: 16, height: 16,
speed: 400, speed: 400,
color: '#8C4' color: '#8C4'
});
}

function removeMissile(missile) {
var i = missiles.indexOf(missile);
if (i !== -1) {
missiles.splice(i, 1);
} }
} }


function maybeAddMissile() { Missiles.prototype.updateMember = function(missile) {
if (missiles.length >= maxMissiles) { missile.y += missile.speed / 4 / fps;
return;
}


if (missileCoolDown > 0) { missile.x += missile.dx;
--missileCoolDown; missile.y += missile.dy;
return;
} // Wobbles.
missile.x += missile.speed / 3 / fps * randomBetween(-1, 2);


missileCoolDown = maxMissileCoolDown; //missile.x += missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1;
addMissile(); //missile.y += missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1;

missile.dx += 0.2 * (missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1);
missile.dy += 0.2 * (missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1);
} }


function updateMissiles() { Missiles.prototype.saveState = function() {
for (var i = 0; i < missiles.length; ++i) { return {
var missile = missiles[i]; __type: 'Missiles',
missile.y += missile.speed / 4 / fps; members: this.members,

max: this.max,
missile.x += missile.dx; maxCoolDown: this.maxCoolDown,
missile.y += missile.dy; coolDown: this.coolDown

// Wobbles.
missile.x += missile.speed / 3 / fps * randomBetween(-1, 2);

//missile.x += missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1;
//missile.y += missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1;

missile.dx += 0.2 * (missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1);
missile.dy += 0.2 * (missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1);
} }
} }


function maybeRemoveMissiles() { Missiles.restoreFromState = function(state) {
reject(missiles, ifHasRunOffTheBottom); var p = new Missiles();
p.members = state.members;
p.max = state.max;
p.maxCoolDown = state.maxCoolDown;
p.coolDown = state.coolDown
return p;
} }


function drawMissiles() { var missiles = new Missiles();
for (var i = 0; i < missiles.length; ++i) {
var missile = missiles[i];
fillPiece(missile);
}
}
8 changes: 4 additions & 4 deletions game03/main.js
Expand Up @@ -9,9 +9,9 @@ function update() {
bullets.update(); bullets.update();
bullets.maybeRemove(); bullets.maybeRemove();


maybeAddMissile(); missiles.maybeAdd();
updateMissiles(); missiles.update();
maybeRemoveMissiles(); missiles.maybeRemove();


// maybeAddPowerup(); // maybeAddPowerup();
// updatePowerups(); // updatePowerups();
Expand All @@ -30,7 +30,7 @@ function redraw() {
drawPowerups(); drawPowerups();
drawBadGuys(); drawBadGuys();
bullets.draw(); bullets.draw();
drawMissiles(); missiles.draw();
drawBoss(); drawBoss();
drawPlayer(); drawPlayer();
drawScore(); drawScore();
Expand Down
7 changes: 4 additions & 3 deletions game03/projectiles.js
Expand Up @@ -39,9 +39,10 @@ projectiles = {
this.add(); this.add();
}, },
update: function() { update: function() {
forEach(this.members, function(member) { forEach(this.members, this.updateMember);
member.y += member.speed / fps; },
}); updateMember: function(member) {
member.y += member.speed / fps;
}, },
maybeRemove: function() { maybeRemove: function() {
reject(this.members, this.shouldBeRemoved); reject(this.members, this.shouldBeRemoved);
Expand Down
7 changes: 5 additions & 2 deletions notes.md
Expand Up @@ -219,8 +219,11 @@ How to refactor:


* Separate concerns by factoring out a `reject` function that handles removing items from a list. * Separate concerns by factoring out a `reject` function that handles removing items from a list.
* Factor out Bullets class and Projectiles superclass. * Factor out Bullets class and Projectiles superclass.
* !! Factor out Missiles class using Projectiles superclass. * Factor out Missiles class using Projectiles superclass.
* !! Factor out Aggregate superclass.
## Assignment

* Factor out Aggregate superclass of Projectiles, badGuys, and powerups.


# Fun For Later # Fun For Later


Expand Down

0 comments on commit fd6960e

Please sign in to comment.