[81] | 1 | /* -*- coding: utf-8 -*- |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2010 by Atzm WATANABE <atzm@atzm.org> |
---|
| 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify it |
---|
| 6 | * under the terms of the GNU General Public License (version 2) as |
---|
| 7 | * published by the Free Software Foundation. It is distributed in the |
---|
| 8 | * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
---|
| 9 | * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
| 10 | * PURPOSE. See the GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * $Id$ |
---|
| 13 | * |
---|
| 14 | */ |
---|
| 15 | |
---|
[92] | 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 | |
---|
[108] | 34 | this.update = function(enemies) { |
---|
[92] | 35 | this.size += this.speed; |
---|
| 36 | |
---|
[108] | 37 | for (var ei = 0; ei < enemies.length; ei++) { |
---|
| 38 | var enemy = enemies[ei]; |
---|
| 39 | var delIdx = new Array(); |
---|
| 40 | |
---|
| 41 | // judge bullet |
---|
| 42 | for (var i = 0; i < enemy.bullets.length; i++) { |
---|
| 43 | if (touch(this.x, this.y, this.size, |
---|
| 44 | enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size)) |
---|
| 45 | delIdx.push(i); |
---|
| 46 | } |
---|
| 47 | enemy.delBullet(delIdx); |
---|
| 48 | |
---|
| 49 | // judge enemy |
---|
| 50 | if (touch(this.x, this.y, this.size, enemy.x, enemy.y, enemy.size) && enemy.life > 0) |
---|
| 51 | enemy.life--; |
---|
[92] | 52 | } |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | this.draw = function(ctx) { |
---|
| 56 | ctx.beginPath(); |
---|
| 57 | ctx.fillStyle = this.colors[this.colIdx]; |
---|
| 58 | ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true); |
---|
| 59 | ctx.fill(); |
---|
| 60 | ctx.closePath(); |
---|
| 61 | |
---|
| 62 | if (++this.colIdx >= this.colors.length) |
---|
| 63 | this.colIdx = 0; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | this.vanished = function() { |
---|
| 67 | if (this.size > this.w && this.size > this.h) |
---|
| 68 | return true; |
---|
| 69 | return false; |
---|
| 70 | }; |
---|
| 71 | }; |
---|
| 72 | |
---|
[97] | 73 | var Trooper = function(name, actList, size, hitSize, color, hitColor, |
---|
[98] | 74 | x, y, w, h, life, speed, numBombs, bombColors, barrages) { |
---|
[81] | 75 | |
---|
[98] | 76 | this.bullets = new Array(); |
---|
| 77 | this.bomb = null; |
---|
| 78 | this.name = name; |
---|
| 79 | this.actList = actList; |
---|
| 80 | this.size = size; |
---|
| 81 | this.hitSize = hitSize; |
---|
| 82 | this.color = color; |
---|
| 83 | this.hitColor = hitColor; |
---|
| 84 | this.x = x; |
---|
| 85 | this.y = y; |
---|
| 86 | this.w = w; |
---|
| 87 | this.h = h; |
---|
| 88 | this.life = life; |
---|
| 89 | this.speed = speed; |
---|
| 90 | this.numBombs = numBombs; |
---|
| 91 | this.bombColors = bombColors; |
---|
| 92 | this.barrages = barrages; |
---|
[81] | 93 | |
---|
[103] | 94 | this.move = function(x, y, area) { |
---|
[81] | 95 | this.x += x; |
---|
| 96 | this.y += y; |
---|
[103] | 97 | this.adjustPosition(area); |
---|
[81] | 98 | }; |
---|
| 99 | |
---|
[103] | 100 | this.moveAbs = function(x, y, area) { |
---|
[81] | 101 | this.x = x; |
---|
| 102 | this.y = y; |
---|
[103] | 103 | this.adjustPosition(area); |
---|
[81] | 104 | }; |
---|
| 105 | |
---|
[103] | 106 | this.adjustPosition = function(area) { |
---|
| 107 | if (area == null) |
---|
| 108 | area = 1; |
---|
| 109 | |
---|
[81] | 110 | if (this.x > this.w) |
---|
| 111 | this.x = this.w; |
---|
| 112 | |
---|
[103] | 113 | if (this.y > this.h / area) |
---|
| 114 | this.y = this.h / area; |
---|
[81] | 115 | |
---|
| 116 | if (this.x < 0) |
---|
| 117 | this.x = 0; |
---|
| 118 | |
---|
| 119 | if (this.y < 0) |
---|
| 120 | this.y = 0; |
---|
| 121 | }; |
---|
| 122 | |
---|
| 123 | this.isDead = function() { |
---|
| 124 | return this.life <= 0; |
---|
| 125 | }; |
---|
| 126 | |
---|
[108] | 127 | this.update = function(enemies) { |
---|
[81] | 128 | // update my action |
---|
[108] | 129 | this.actList.update(this, enemies); |
---|
[81] | 130 | |
---|
[92] | 131 | // update bomb |
---|
| 132 | if (this.bomb) { |
---|
[108] | 133 | this.bomb.update(enemies); |
---|
[92] | 134 | if (this.bomb.vanished()) |
---|
| 135 | this.bomb = null; |
---|
| 136 | } |
---|
| 137 | |
---|
[81] | 138 | // update/delete my bullets |
---|
[108] | 139 | { |
---|
| 140 | var delIdx = new Array(); |
---|
| 141 | for (var i = 0; i < this.bullets.length; i++) { |
---|
| 142 | this.bullets[i].next(); |
---|
[81] | 143 | |
---|
[108] | 144 | if (this.bullets[i].vanished()) |
---|
| 145 | delIdx.push(i); |
---|
| 146 | } |
---|
| 147 | this.delBullet(delIdx); |
---|
[81] | 148 | } |
---|
| 149 | |
---|
[108] | 150 | // update/delete enemies' bullets |
---|
| 151 | { |
---|
| 152 | var hit = false; |
---|
| 153 | |
---|
| 154 | for (var ei = 0; ei < enemies.length; ei++) { |
---|
| 155 | var enemy = enemies[ei]; |
---|
| 156 | var delIdx = new Array(); |
---|
| 157 | |
---|
| 158 | // judge/delete hit bullets |
---|
| 159 | for (var i = 0; i < enemy.bullets.length; i++) { |
---|
| 160 | if (touch(this.x, this.y, this.hitSize, |
---|
| 161 | enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size)) |
---|
| 162 | delIdx.push(i); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | enemy.delBullet(delIdx); |
---|
| 166 | |
---|
| 167 | if (delIdx.length > 0) |
---|
| 168 | hit = true; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // update my life |
---|
| 172 | if (hit && this.life > 0) |
---|
| 173 | this.life--; |
---|
[81] | 174 | } |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | this.draw = function(ctx) { |
---|
[92] | 178 | // draw bomb |
---|
| 179 | if (this.bomb) |
---|
| 180 | this.bomb.draw(ctx); |
---|
| 181 | |
---|
[106] | 182 | // draw bullets |
---|
| 183 | for (var i = 0; i < this.bullets.length; i++) |
---|
| 184 | this.bullets[i].draw(ctx); |
---|
| 185 | |
---|
[81] | 186 | // draw trooper |
---|
| 187 | ctx.beginPath(); |
---|
| 188 | ctx.fillStyle = this.color; |
---|
| 189 | ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true); |
---|
| 190 | ctx.fill(); |
---|
| 191 | ctx.closePath(); |
---|
| 192 | |
---|
[97] | 193 | if (this.color != this.hitColor || this.size != this.hitSize) { |
---|
| 194 | ctx.beginPath(); |
---|
| 195 | ctx.fillStyle = this.hitColor; |
---|
| 196 | ctx.arc(this.x, this.y, this.hitSize, 0, Math.PI * 2.0, true); |
---|
| 197 | ctx.fill(); |
---|
| 198 | ctx.closePath(); |
---|
| 199 | } |
---|
[81] | 200 | }; |
---|
| 201 | |
---|
[106] | 202 | this.addBullet = function(bulletType, size, color, frame, x, y, dir, speed) { |
---|
[81] | 203 | this.bullets.push( |
---|
[106] | 204 | new bulletType(size, color, frame, this.x + x, this.y + y, dir, speed)); |
---|
[81] | 205 | }; |
---|
| 206 | |
---|
| 207 | this.delBullet = function(idx) { |
---|
| 208 | if (idx.length <= 0) |
---|
| 209 | return; |
---|
| 210 | |
---|
| 211 | var newBullets = new Array(); |
---|
| 212 | |
---|
[96] | 213 | for (var i = 0; i < idx.length; i++) |
---|
[81] | 214 | this.bullets[idx[i]] = null; |
---|
| 215 | |
---|
[96] | 216 | for (var i = 0; i < this.bullets.length; i++) |
---|
[81] | 217 | if (this.bullets[i]) |
---|
| 218 | newBullets.push(this.bullets[i]); |
---|
| 219 | |
---|
| 220 | this.bullets = newBullets; |
---|
| 221 | }; |
---|
[92] | 222 | |
---|
[95] | 223 | this.clearBullet = function() { |
---|
| 224 | this.bullets = new Array(); |
---|
| 225 | }; |
---|
| 226 | |
---|
[92] | 227 | this.addBomb = function() { |
---|
| 228 | if (!this.bomb && this.numBombs > 0) { |
---|
[98] | 229 | this.bomb = new Bomb(this.x, this.y, this.w, this.h, |
---|
| 230 | this.size * 2, 10, this.bombColors); |
---|
[92] | 231 | this.numBombs--; |
---|
| 232 | } |
---|
| 233 | }; |
---|
[81] | 234 | } |
---|