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