Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Factor out Missiles class using Projectiles superclass.

  • Loading branch information...
commit fd6960e34c667747d7e794ae8fcef3548e488bd0 1 parent 60e164b
William Taysom authored
80 game03/boss.js
@@ -18,13 +18,13 @@ function drawBoss() {
18 18
19 19 /** Missiles **/
20 20
21   -var missiles = [];
22   -var maxMissiles = 30;
23   -var maxMissileCoolDown = 20;
24   -var missileCoolDown = maxMissileCoolDown;
  21 +function Missiles() {
  22 + Projectiles.call(this);
  23 +}
  24 +Missiles.prototype = new Projectiles();
25 25
26   -function addMissile() {
27   - missiles.push({
  26 +Missiles.prototype.newMember = function() {
  27 + return {
28 28 x: boss.x,
29 29 y: sides(boss).bottom,
30 30 dx: randomBetween(-1, 2),
@@ -33,56 +33,42 @@ function addMissile() {
33 33 height: 16,
34 34 speed: 400,
35 35 color: '#8C4'
36   - });
37   -}
38   -
39   -function removeMissile(missile) {
40   - var i = missiles.indexOf(missile);
41   - if (i !== -1) {
42   - missiles.splice(i, 1);
43 36 }
44 37 }
45 38
46   -function maybeAddMissile() {
47   - if (missiles.length >= maxMissiles) {
48   - return;
49   - }
  39 +Missiles.prototype.updateMember = function(missile) {
  40 + missile.y += missile.speed / 4 / fps;
50 41
51   - if (missileCoolDown > 0) {
52   - --missileCoolDown;
53   - return;
54   - }
  42 + missile.x += missile.dx;
  43 + missile.y += missile.dy;
  44 +
  45 + // Wobbles.
  46 + missile.x += missile.speed / 3 / fps * randomBetween(-1, 2);
55 47
56   - missileCoolDown = maxMissileCoolDown;
57   - addMissile();
  48 + //missile.x += missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1;
  49 + //missile.y += missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1;
  50 +
  51 + missile.dx += 0.2 * (missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1);
  52 + missile.dy += 0.2 * (missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1);
58 53 }
59 54
60   -function updateMissiles() {
61   - for (var i = 0; i < missiles.length; ++i) {
62   - var missile = missiles[i];
63   - missile.y += missile.speed / 4 / fps;
64   -
65   - missile.x += missile.dx;
66   - missile.y += missile.dy;
67   -
68   - // Wobbles.
69   - missile.x += missile.speed / 3 / fps * randomBetween(-1, 2);
70   -
71   - //missile.x += missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1;
72   - //missile.y += missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1;
73   -
74   - missile.dx += 0.2 * (missile.x == player.x ? 0 : missile.x < player.x ? 1 : -1);
75   - missile.dy += 0.2 * (missile.y == player.y ? 0 : missile.y < player.y ? 1 : -1);
  55 +Missiles.prototype.saveState = function() {
  56 + return {
  57 + __type: 'Missiles',
  58 + members: this.members,
  59 + max: this.max,
  60 + maxCoolDown: this.maxCoolDown,
  61 + coolDown: this.coolDown
76 62 }
77 63 }
78 64
79   -function maybeRemoveMissiles() {
80   - reject(missiles, ifHasRunOffTheBottom);
  65 +Missiles.restoreFromState = function(state) {
  66 + var p = new Missiles();
  67 + p.members = state.members;
  68 + p.max = state.max;
  69 + p.maxCoolDown = state.maxCoolDown;
  70 + p.coolDown = state.coolDown
  71 + return p;
81 72 }
82 73
83   -function drawMissiles() {
84   - for (var i = 0; i < missiles.length; ++i) {
85   - var missile = missiles[i];
86   - fillPiece(missile);
87   - }
88   -}
  74 +var missiles = new Missiles();
8 game03/main.js
@@ -9,9 +9,9 @@ function update() {
9 9 bullets.update();
10 10 bullets.maybeRemove();
11 11
12   - maybeAddMissile();
13   - updateMissiles();
14   - maybeRemoveMissiles();
  12 + missiles.maybeAdd();
  13 + missiles.update();
  14 + missiles.maybeRemove();
15 15
16 16 // maybeAddPowerup();
17 17 // updatePowerups();
@@ -30,7 +30,7 @@ function redraw() {
30 30 drawPowerups();
31 31 drawBadGuys();
32 32 bullets.draw();
33   - drawMissiles();
  33 + missiles.draw();
34 34 drawBoss();
35 35 drawPlayer();
36 36 drawScore();
7 game03/projectiles.js
@@ -39,9 +39,10 @@ projectiles = {
39 39 this.add();
40 40 },
41 41 update: function() {
42   - forEach(this.members, function(member) {
43   - member.y += member.speed / fps;
44   - });
  42 + forEach(this.members, this.updateMember);
  43 + },
  44 + updateMember: function(member) {
  45 + member.y += member.speed / fps;
45 46 },
46 47 maybeRemove: function() {
47 48 reject(this.members, this.shouldBeRemoved);
7 notes.md
Source Rendered
@@ -219,8 +219,11 @@ How to refactor:
219 219
220 220 * Separate concerns by factoring out a `reject` function that handles removing items from a list.
221 221 * Factor out Bullets class and Projectiles superclass.
222   -* !! Factor out Missiles class using Projectiles superclass.
223   -* !! Factor out Aggregate superclass.
  222 +* Factor out Missiles class using Projectiles superclass.
  223 +
  224 +## Assignment
  225 +
  226 +* Factor out Aggregate superclass of Projectiles, badGuys, and powerups.
224 227
225 228 # Fun For Later
226 229

0 comments on commit fd6960e

Please sign in to comment.
Something went wrong with that request. Please try again.