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 | |
---|
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(enemies) { |
---|
35 | this.size += this.speed; |
---|
36 | |
---|
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--; |
---|
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 | |
---|
73 | var Trooper = function(name, actList, image, size, hitSize, color, hitColor, |
---|
74 | x, y, w, h, life, speed, numBombs, bombColors, barrages) { |
---|
75 | |
---|
76 | this.bullets = new Array(); |
---|
77 | this.callbacks = {}; |
---|
78 | this.bomb = null; |
---|
79 | this.name = name; |
---|
80 | this.actList = actList; |
---|
81 | this.image = image; |
---|
82 | this.size = size; |
---|
83 | this.hitSize = hitSize; |
---|
84 | this.color = color; |
---|
85 | this.hitColor = hitColor; |
---|
86 | this.x = x; |
---|
87 | this.y = y; |
---|
88 | this.w = w; |
---|
89 | this.h = h; |
---|
90 | this.life = life; |
---|
91 | this.maxLife = life; |
---|
92 | this.speed = speed; |
---|
93 | this.numBombs = numBombs; |
---|
94 | this.bombColors = bombColors; |
---|
95 | this.barrages = barrages; |
---|
96 | |
---|
97 | this.move = function(x, y, area) { |
---|
98 | this.x += x; |
---|
99 | this.y += y; |
---|
100 | this.adjustPosition(area); |
---|
101 | }; |
---|
102 | |
---|
103 | this.moveAbs = function(x, y, area) { |
---|
104 | this.x = x; |
---|
105 | this.y = y; |
---|
106 | this.adjustPosition(area); |
---|
107 | }; |
---|
108 | |
---|
109 | this.adjustPosition = function(area) { |
---|
110 | if (area == null) |
---|
111 | area = 1; |
---|
112 | |
---|
113 | if (this.x > this.w) |
---|
114 | this.x = this.w; |
---|
115 | |
---|
116 | if (this.y > this.h / area) |
---|
117 | this.y = this.h / area; |
---|
118 | |
---|
119 | if (this.x < 0) |
---|
120 | this.x = 0; |
---|
121 | |
---|
122 | if (this.y < 0) |
---|
123 | this.y = 0; |
---|
124 | }; |
---|
125 | |
---|
126 | this.isDead = function() { |
---|
127 | return this.life <= 0; |
---|
128 | }; |
---|
129 | |
---|
130 | this.update = function(enemies) { |
---|
131 | // update my action |
---|
132 | this.actList.update(this, enemies); |
---|
133 | |
---|
134 | // update bomb |
---|
135 | if (this.bomb) { |
---|
136 | this.bomb.update(enemies); |
---|
137 | if (this.bomb.vanished()) |
---|
138 | this.bomb = null; |
---|
139 | } |
---|
140 | |
---|
141 | // update/delete my bullets |
---|
142 | { |
---|
143 | var delIdx = new Array(); |
---|
144 | for (var i = 0; i < this.bullets.length; i++) { |
---|
145 | this.bullets[i].next(); |
---|
146 | |
---|
147 | if (this.bullets[i].vanished(this.w, this.h)) |
---|
148 | delIdx.push(i); |
---|
149 | } |
---|
150 | this.delBullet(delIdx); |
---|
151 | } |
---|
152 | |
---|
153 | // update/delete enemies' bullets |
---|
154 | { |
---|
155 | var hit = false; |
---|
156 | |
---|
157 | for (var ei = 0; ei < enemies.length; ei++) { |
---|
158 | var enemy = enemies[ei]; |
---|
159 | var delIdx = new Array(); |
---|
160 | |
---|
161 | // judge/delete hit bullets |
---|
162 | for (var i = 0; i < enemy.bullets.length; i++) { |
---|
163 | if (touch(this.x, this.y, this.hitSize, |
---|
164 | enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size)) |
---|
165 | delIdx.push(i); |
---|
166 | } |
---|
167 | |
---|
168 | enemy.delBullet(delIdx); |
---|
169 | |
---|
170 | if (delIdx.length > 0) |
---|
171 | hit = true; |
---|
172 | } |
---|
173 | |
---|
174 | // update my life |
---|
175 | if (hit && this.life > 0) { |
---|
176 | this.life--; |
---|
177 | |
---|
178 | if (this.callbacks["damaged"]) |
---|
179 | this.callbacks["damaged"](this); |
---|
180 | } |
---|
181 | } |
---|
182 | }; |
---|
183 | |
---|
184 | this.draw = function(ctx) { |
---|
185 | // draw bomb |
---|
186 | if (this.bomb) |
---|
187 | this.bomb.draw(ctx); |
---|
188 | |
---|
189 | // draw bullets |
---|
190 | for (var i = 0; i < this.bullets.length; i++) |
---|
191 | this.bullets[i].draw(ctx); |
---|
192 | |
---|
193 | // draw trooper |
---|
194 | if (this.image) { |
---|
195 | var w = this.image.width * (this.size / this.image.height); |
---|
196 | var h = this.size; |
---|
197 | var x = this.x - (w / 2); |
---|
198 | var y = this.y - (h / 2); |
---|
199 | ctx.drawImage(this.image, x, y, w, h); |
---|
200 | } |
---|
201 | else { |
---|
202 | ctx.beginPath(); |
---|
203 | ctx.fillStyle = this.color; |
---|
204 | ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true); |
---|
205 | ctx.fill(); |
---|
206 | ctx.closePath(); |
---|
207 | } |
---|
208 | |
---|
209 | // draw hit area |
---|
210 | if (this.color != this.hitColor || this.size != this.hitSize) { |
---|
211 | ctx.beginPath(); |
---|
212 | ctx.fillStyle = this.hitColor; |
---|
213 | ctx.arc(this.x, this.y, this.hitSize, 0, Math.PI * 2.0, true); |
---|
214 | ctx.fill(); |
---|
215 | ctx.closePath(); |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | this.addBullet = function(bulletType, size, color, frame, x, y, dir, speed) { |
---|
220 | this.bullets.push( |
---|
221 | new bulletType(size, color, frame, this.w, this.h, this.x + x, this.y + y, dir, speed)); |
---|
222 | |
---|
223 | if (this.callbacks["addBullet"]) |
---|
224 | this.callbacks["addBullet"](this); |
---|
225 | }; |
---|
226 | |
---|
227 | this.delBullet = function(idx) { |
---|
228 | if (idx.length <= 0) |
---|
229 | return; |
---|
230 | |
---|
231 | var newBullets = new Array(); |
---|
232 | |
---|
233 | for (var i = 0; i < idx.length; i++) |
---|
234 | this.bullets[idx[i]] = null; |
---|
235 | |
---|
236 | for (var i = 0; i < this.bullets.length; i++) |
---|
237 | if (this.bullets[i]) |
---|
238 | newBullets.push(this.bullets[i]); |
---|
239 | |
---|
240 | this.bullets = newBullets; |
---|
241 | }; |
---|
242 | |
---|
243 | this.clearBullet = function() { |
---|
244 | this.bullets = new Array(); |
---|
245 | }; |
---|
246 | |
---|
247 | this.addBomb = function() { |
---|
248 | if (!this.bomb && this.numBombs > 0) { |
---|
249 | this.bomb = new Bomb(this.x, this.y, this.w, this.h, |
---|
250 | this.size * 2, 10, this.bombColors); |
---|
251 | this.numBombs--; |
---|
252 | |
---|
253 | if (this.callbacks["addBomb"]) |
---|
254 | this.callbacks["addBomb"](this); |
---|
255 | } |
---|
256 | }; |
---|
257 | |
---|
258 | this.registerCallback = function(name, callback) { |
---|
259 | this.callbacks[name] = callback; |
---|
260 | }; |
---|
261 | } |
---|