Skip to content

Commit

Permalink
Factor out Missiles class using Projectiles superclass.
Browse files Browse the repository at this point in the history
  • 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function drawBoss() {

/** Missiles **/

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

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

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

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

if (missileCoolDown > 0) {
--missileCoolDown;
return;
}
missile.x += missile.dx;
missile.y += missile.dy;

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

missileCoolDown = maxMissileCoolDown;
addMissile();
//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 updateMissiles() {
for (var i = 0; i < missiles.length; ++i) {
var missile = missiles[i];
missile.y += missile.speed / 4 / fps;

missile.x += missile.dx;
missile.y += missile.dy;

// 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);
Missiles.prototype.saveState = function() {
return {
__type: 'Missiles',
members: this.members,
max: this.max,
maxCoolDown: this.maxCoolDown,
coolDown: this.coolDown
}
}

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

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

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

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

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

## Assignment

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

# Fun For Later

Expand Down

0 comments on commit fd6960e

Please sign in to comment.