Changeset 92 for pycodeshooter/trunk/shooter/trooper.js
- Timestamp:
- 01/12/11 18:39:31 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pycodeshooter/trunk/shooter/trooper.js
r81 r92 14 14 */ 15 15 16 function touch(x1, y1, s1, x2, y2, s2) { 17 var dx = Math.pow(x2 - x1, 2); 18 var dy = Math.pow(y2 - y1, 2); 19 if (Math.sqrt(dx + dy) < s1 + s2) 20 return true; 21 return false; 22 } 23 24 var Bomb = function(x, y, w, h, size, speed, colors) { 25 this.x = x; 26 this.y = y; 27 this.w = w; 28 this.h = h; 29 this.size = size; 30 this.speed = speed; 31 this.colors = colors; 32 this.colIdx = 0; 33 34 this.update = function(enemy) { 35 this.size += this.speed; 36 37 // judge bullet 38 var delIdx = new Array(); 39 for (i = 0; i < enemy.bullets.length; i++) { 40 if (touch(this.x, this.y, this.size, 41 enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size)) 42 delIdx.push(i); 43 } 44 enemy.delBullet(delIdx); 45 46 // judge enemy 47 if (touch(this.x, this.y, this.size, enemy.x, enemy.y, enemy.size) && enemy.life > 0) 48 enemy.life--; 49 }; 50 51 this.draw = function(ctx) { 52 ctx.beginPath(); 53 ctx.fillStyle = this.colors[this.colIdx]; 54 ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true); 55 ctx.fill(); 56 ctx.closePath(); 57 58 if (++this.colIdx >= this.colors.length) 59 this.colIdx = 0; 60 }; 61 62 this.vanished = function() { 63 if (this.size > this.w && this.size > this.h) 64 return true; 65 return false; 66 }; 67 }; 68 16 69 var Trooper = function(name, actList, size, color, x, y, w, h, 17 life, speed, barrages) {70 life, speed, numBombs, barrages) { 18 71 19 72 this.bullets = new Array(); 73 this.bomb = null; 20 74 this.name = name; 21 75 this.actList = actList; … … 28 82 this.life = life; 29 83 this.speed = speed; 84 this.numBombs = numBombs; 30 85 this.barrages = barrages; 31 86 … … 66 121 this.actList.update(this, enemy); 67 122 123 // update bomb 124 if (this.bomb) { 125 this.bomb.update(enemy); 126 if (this.bomb.vanished()) 127 this.bomb = null; 128 } 129 68 130 // update/delete my bullets 69 131 delIdx = new Array(); … … 79 141 delIdx = new Array(); 80 142 for (i = 0; i < enemy.bullets.length; i++) { 81 var dx = Math.pow(enemy.bullets[i].x - this.x, 2); 82 var dy = Math.pow(enemy.bullets[i].y - this.y, 2); 83 84 if (Math.sqrt(dx + dy) < this.size + enemy.bullets[i].size) 143 if (touch(this.x, this.y, this.size, 144 enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size)) 85 145 delIdx.push(i); 86 146 } 87 enemy.delBullet(delIdx) 147 enemy.delBullet(delIdx); 88 148 89 149 // update life … … 93 153 94 154 this.draw = function(ctx) { 155 // draw bomb 156 if (this.bomb) 157 this.bomb.draw(ctx); 158 95 159 // draw trooper 96 160 ctx.beginPath(); … … 125 189 this.bullets = newBullets; 126 190 }; 191 192 this.addBomb = function() { 193 if (!this.bomb && this.numBombs > 0) { 194 this.bomb = new Bomb(this.x, this.y, this.w, this.h, this.size * 2, 10, 195 ["rgba(255,0,0,0.3)", "rgba(0,0,255,0.3)"]); 196 this.numBombs--; 197 } 198 }; 127 199 }
Note: See TracChangeset
for help on using the changeset viewer.