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 | const ACTION_UP = 1; |
---|
17 | const ACTION_DOWN = 2; |
---|
18 | const ACTION_LEFT = 4; |
---|
19 | const ACTION_RIGHT = 8; |
---|
20 | const ACTION_SHOT = 16; |
---|
21 | const ACTION_DELAY = 32; |
---|
22 | const SWITCH_BULLET = 1; |
---|
23 | const SWITCH_BOMB = 2; |
---|
24 | var currentAction = 0; |
---|
25 | var currentSwitch = 0; |
---|
26 | |
---|
27 | |
---|
28 | /* |
---|
29 | * Functions |
---|
30 | */ |
---|
31 | function getActionByKeyCode(keyCode) { |
---|
32 | switch(keyCode) { |
---|
33 | case 37: // left |
---|
34 | case 72: // h |
---|
35 | return ACTION_LEFT; |
---|
36 | |
---|
37 | case 40: // down |
---|
38 | case 74: // j |
---|
39 | return ACTION_DOWN; |
---|
40 | |
---|
41 | case 38: // up |
---|
42 | case 75: // k |
---|
43 | return ACTION_UP; |
---|
44 | |
---|
45 | case 39: // right |
---|
46 | case 76: // l |
---|
47 | return ACTION_RIGHT; |
---|
48 | |
---|
49 | case 88: // x |
---|
50 | return ACTION_DELAY; |
---|
51 | |
---|
52 | case 90: // z |
---|
53 | return ACTION_SHOT; |
---|
54 | } |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | function getSwitchByCharCode(charCode) { |
---|
59 | switch(charCode) { |
---|
60 | case 97: // a |
---|
61 | return SWITCH_BOMB; |
---|
62 | case 99: // c |
---|
63 | return SWITCH_BULLET; |
---|
64 | } |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | function setKeyDown(keyCode) { |
---|
69 | currentAction |= getActionByKeyCode(keyCode); |
---|
70 | } |
---|
71 | |
---|
72 | function setKeyUp(keyCode) { |
---|
73 | currentAction &= (0xffff - getActionByKeyCode(keyCode)); |
---|
74 | } |
---|
75 | |
---|
76 | function isInAction(act) { |
---|
77 | return currentAction & act; |
---|
78 | } |
---|
79 | |
---|
80 | function setKeyPress(charCode) { |
---|
81 | currentSwitch |= getSwitchByCharCode(charCode); |
---|
82 | } |
---|
83 | |
---|
84 | function unsetSwitch(sw) { |
---|
85 | currentSwitch &= (0xffff - sw); |
---|
86 | } |
---|
87 | |
---|
88 | function isSetSwitch(sw) { |
---|
89 | return currentSwitch & sw; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /* |
---|
94 | * Shot |
---|
95 | * - update / switch barrages |
---|
96 | */ |
---|
97 | var Shot = function() { |
---|
98 | this.shot = function() { |
---|
99 | }; |
---|
100 | }; |
---|
101 | |
---|
102 | var ManualShot = function() { |
---|
103 | var that = new Shot(); |
---|
104 | |
---|
105 | that.index = 0; |
---|
106 | that.shot = function(trooper, enemy) { |
---|
107 | if (isSetSwitch(SWITCH_BULLET)) { |
---|
108 | if (++this.index >= trooper.barrages.length) |
---|
109 | this.index = 0; |
---|
110 | unsetSwitch(SWITCH_BULLET); |
---|
111 | } |
---|
112 | if (isSetSwitch(SWITCH_BOMB)) { |
---|
113 | trooper.addBomb(); |
---|
114 | unsetSwitch(SWITCH_BOMB); |
---|
115 | } |
---|
116 | if (isInAction(ACTION_SHOT)) |
---|
117 | trooper.barrages[this.index].update(trooper, enemy); |
---|
118 | }; |
---|
119 | |
---|
120 | return that; |
---|
121 | }; |
---|
122 | |
---|
123 | var BlazeRandomShot = function() { |
---|
124 | var that = new Shot(); |
---|
125 | |
---|
126 | that.index = 0; |
---|
127 | that.shot = function(trooper, enemy) { |
---|
128 | if (trooper.barrages[this.index].update(trooper, enemy)) |
---|
129 | this.index = Math.floor(Math.random() * trooper.barrages.length); |
---|
130 | }; |
---|
131 | |
---|
132 | return that; |
---|
133 | }; |
---|
134 | |
---|
135 | var BlazeRotateShot = function(count) { |
---|
136 | var that = new BlazeRandomShot(); |
---|
137 | |
---|
138 | if (!count) |
---|
139 | count = 0; |
---|
140 | |
---|
141 | that.i = 0; |
---|
142 | that.shot = function(trooper, enemy) { |
---|
143 | if (trooper.barrages[this.index].update(trooper, enemy)) { |
---|
144 | if (++this.i > count) { |
---|
145 | this.index++; |
---|
146 | this.i = 0; |
---|
147 | } |
---|
148 | if (this.index >= trooper.barrages.length) |
---|
149 | this.index = 0; |
---|
150 | } |
---|
151 | }; |
---|
152 | |
---|
153 | return that; |
---|
154 | }; |
---|
155 | |
---|
156 | var BlazeRotateShot5 = function() { |
---|
157 | return new BlazeRotateShot(5); |
---|
158 | }; |
---|
159 | |
---|
160 | var BlazeRotateShot10 = function() { |
---|
161 | return new BlazeRotateShot(10); |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | /* |
---|
166 | * Action |
---|
167 | * - call shot / move trooper |
---|
168 | */ |
---|
169 | var Action = function(shot) { |
---|
170 | this.shot = shot; |
---|
171 | this.update = function(trooper, enemy) { |
---|
172 | if (trooper.isDead()) |
---|
173 | return; |
---|
174 | this.shot.shot(trooper, enemy); |
---|
175 | return this.move(trooper, enemy); |
---|
176 | }; |
---|
177 | this.move = function(trooper, enemy) { |
---|
178 | return true; |
---|
179 | }; |
---|
180 | }; |
---|
181 | |
---|
182 | var ManualAction = function(shot) { |
---|
183 | var that = new Action(shot); |
---|
184 | |
---|
185 | that.move = function(trooper, enemy) { |
---|
186 | var x = 0; |
---|
187 | var y = 0; |
---|
188 | var s = trooper.speed; |
---|
189 | |
---|
190 | if (isInAction(ACTION_DELAY)) |
---|
191 | s /= 2; |
---|
192 | if (isInAction(ACTION_RIGHT)) |
---|
193 | x += s; |
---|
194 | if (isInAction(ACTION_LEFT)) |
---|
195 | x -= s; |
---|
196 | if (isInAction(ACTION_UP)) |
---|
197 | y -= s; |
---|
198 | if (isInAction(ACTION_DOWN)) |
---|
199 | y += s; |
---|
200 | |
---|
201 | trooper.move(x, y); |
---|
202 | |
---|
203 | return true; |
---|
204 | }; |
---|
205 | |
---|
206 | return that; |
---|
207 | }; |
---|
208 | |
---|
209 | var BezierAction = function(shot, resolution) { |
---|
210 | that = new Action(shot); |
---|
211 | |
---|
212 | if (!resolution) |
---|
213 | resolution = 1500; |
---|
214 | |
---|
215 | that.count = 0; |
---|
216 | that.resolution = 0; |
---|
217 | |
---|
218 | that.move = function(trooper, enemy) { |
---|
219 | if (this.count >= this.resolution) { |
---|
220 | this.count = 0; |
---|
221 | this.resolution = resolution / trooper.speed; |
---|
222 | |
---|
223 | this.p0 = this.getPoint0(trooper, enemy); |
---|
224 | this.p1 = this.getPoint1(trooper, enemy); |
---|
225 | this.p2 = this.getPoint2(trooper, enemy); |
---|
226 | this.p3 = this.getPoint3(trooper, enemy); |
---|
227 | } |
---|
228 | |
---|
229 | var t = this.count++ / this.resolution; |
---|
230 | var t1 = 1 - t; |
---|
231 | var p0 = Math.pow(t1, 3); |
---|
232 | var p1 = 3 * t * Math.pow(t1, 2); |
---|
233 | var p2 = 3 * Math.pow(t, 2) * t1; |
---|
234 | var p3 = Math.pow(t, 3); |
---|
235 | |
---|
236 | trooper.moveAbs( |
---|
237 | this.p0[0] * p0 + this.p1[0] * p1 + |
---|
238 | this.p2[0] * p2 + this.p3[0] * p3, |
---|
239 | this.p0[1] * p0 + this.p1[1] * p1 + |
---|
240 | this.p2[1] * p2 + this.p3[1] * p3 |
---|
241 | ); |
---|
242 | |
---|
243 | if (this.count >= this.resolution) |
---|
244 | return true; |
---|
245 | |
---|
246 | return false; |
---|
247 | }; |
---|
248 | |
---|
249 | return that; |
---|
250 | }; |
---|
251 | |
---|
252 | var TriangleAction = function(shot, resolution) { |
---|
253 | var that = new BezierAction(shot, resolution); |
---|
254 | |
---|
255 | that.getPoint0 = function(trooper, enemy) { |
---|
256 | return [trooper.x, trooper.y]; |
---|
257 | }; |
---|
258 | that.getPoint1 = function(trooper, enemy) { |
---|
259 | return [trooper.x - (trooper.speed * 10), |
---|
260 | trooper.y + (trooper.speed * 10)]; |
---|
261 | }; |
---|
262 | that.getPoint2 = function(trooper, enemy) { |
---|
263 | return [trooper.x + (trooper.speed * 10), |
---|
264 | trooper.y + (trooper.speed * 10)]; |
---|
265 | }; |
---|
266 | that.getPoint3 = function(trooper, enemy) { |
---|
267 | return [trooper.x, trooper.y]; |
---|
268 | }; |
---|
269 | |
---|
270 | return that; |
---|
271 | }; |
---|
272 | |
---|
273 | var RandomAction = function(shot, resolution) { |
---|
274 | var that = new BezierAction(shot, resolution); |
---|
275 | |
---|
276 | that.getPoint0 = function(trooper, enemy) { |
---|
277 | return [trooper.x, trooper.y]; |
---|
278 | }; |
---|
279 | that.getPoint1 = function(trooper, enemy) { |
---|
280 | return [Math.floor(Math.random() * trooper.w), |
---|
281 | Math.floor(Math.random() * trooper.h)]; |
---|
282 | }; |
---|
283 | that.getPoint2 = function(trooper, enemy) { |
---|
284 | return [Math.floor(Math.random() * trooper.w), |
---|
285 | Math.floor(Math.random() * trooper.h)]; |
---|
286 | }; |
---|
287 | that.getPoint3 = function(trooper, enemy) { |
---|
288 | return [Math.floor(Math.random() * trooper.w), |
---|
289 | Math.floor(Math.random() * trooper.h)]; |
---|
290 | }; |
---|
291 | |
---|
292 | return that; |
---|
293 | }; |
---|
294 | |
---|
295 | var RandomReturnAction = function(shot, resolution) { |
---|
296 | var that = new RandomAction(shot, resolution); |
---|
297 | |
---|
298 | that.getPoint3 = function(trooper, enemy) { |
---|
299 | return [Math.floor(Math.random() * trooper.w), trooper.y]; |
---|
300 | }; |
---|
301 | |
---|
302 | return that; |
---|
303 | }; |
---|
304 | |
---|
305 | var RandomStokingAction = function(shot, resolution) { |
---|
306 | var that = new RandomAction(shot, resolution); |
---|
307 | |
---|
308 | that.getPoint3 = function(trooper, enemy) { |
---|
309 | return [enemy.x, Math.floor(Math.random() * trooper.h)]; |
---|
310 | }; |
---|
311 | |
---|
312 | return that; |
---|
313 | }; |
---|
314 | |
---|
315 | var RandomStokingReturnAction = function(shot, resolution) { |
---|
316 | var that = new RandomAction(shot, resolution); |
---|
317 | |
---|
318 | that.getPoint3 = function(trooper, enemy) { |
---|
319 | return [enemy.x, trooper.y]; |
---|
320 | }; |
---|
321 | |
---|
322 | return that; |
---|
323 | }; |
---|
324 | |
---|
325 | |
---|
326 | /* |
---|
327 | * ActionList |
---|
328 | * - call / switch actions |
---|
329 | */ |
---|
330 | var ActionList = function(list) { |
---|
331 | this.list = list; |
---|
332 | this.index = 0; |
---|
333 | this.update = function(trooper, enemy) { |
---|
334 | this.updateIdx(this.list[this.index].update(trooper, enemy), |
---|
335 | trooper, enemy); |
---|
336 | }; |
---|
337 | this.updateIdx = function(end, trooper, enemy) { |
---|
338 | this.index = 0; |
---|
339 | }; |
---|
340 | }; |
---|
341 | |
---|
342 | var RandomActionList = function(list) { |
---|
343 | var that = new ActionList(list); |
---|
344 | |
---|
345 | that.updateIdx = function(end, trooper, enemy) { |
---|
346 | if (end) |
---|
347 | this.index = Math.floor(Math.random() * this.list.length); |
---|
348 | }; |
---|
349 | |
---|
350 | return that; |
---|
351 | }; |
---|
352 | |
---|
353 | var RotateActionList = function(list, count) { |
---|
354 | var that = new ActionList(list); |
---|
355 | |
---|
356 | if (!count) |
---|
357 | count = 0; |
---|
358 | |
---|
359 | that.i = 0; |
---|
360 | that.updateIdx = function(end, trooper, enemy) { |
---|
361 | if (end) { |
---|
362 | if (++this.i > count) { |
---|
363 | this.index++; |
---|
364 | this.i = 0; |
---|
365 | } |
---|
366 | if (this.index >= this.list.length) |
---|
367 | this.index = 0; |
---|
368 | } |
---|
369 | }; |
---|
370 | |
---|
371 | return that; |
---|
372 | }; |
---|
373 | |
---|
374 | var RotateActionList5 = function(list) { |
---|
375 | return new RotateActionList(list, 5); |
---|
376 | }; |
---|
377 | |
---|
378 | var RotateActionList10 = function(list) { |
---|
379 | return new RotateActionList(list, 10); |
---|
380 | }; |
---|
381 | |
---|
382 | var DamageRandomActionList = function(list) { |
---|
383 | var that = new ActionList(list); |
---|
384 | |
---|
385 | that.life = 0; |
---|
386 | that.updateIdx = function(end, trooper, enemy) { |
---|
387 | if (!this.life) |
---|
388 | this.life = trooper.life; |
---|
389 | |
---|
390 | if (end && trooper.life != this.life) { |
---|
391 | this.index = Math.floor(Math.random() * this.list.length); |
---|
392 | this.life = trooper.life; |
---|
393 | } |
---|
394 | }; |
---|
395 | |
---|
396 | return that; |
---|
397 | }; |
---|
398 | |
---|
399 | var DamageRotateActionList = function(list) { |
---|
400 | var that = new ActionList(list); |
---|
401 | |
---|
402 | that.life = 0; |
---|
403 | that.updateIdx = function(end, trooper, enemy) { |
---|
404 | if (!this.life) |
---|
405 | this.life = trooper.life; |
---|
406 | |
---|
407 | if (end && trooper.life != this.life) { |
---|
408 | if (++this.index >= this.list.length) |
---|
409 | this.index = 0; |
---|
410 | |
---|
411 | this.life = trooper.life; |
---|
412 | } |
---|
413 | }; |
---|
414 | |
---|
415 | return that; |
---|
416 | }; |
---|
417 | |
---|
418 | |
---|
419 | var EnemyShots = [BlazeRandomShot, BlazeRotateShot, |
---|
420 | BlazeRotateShot5, BlazeRotateShot10]; |
---|
421 | var EnemyActions = [TriangleAction, RandomAction, |
---|
422 | RandomReturnAction, RandomStokingAction, |
---|
423 | RandomStokingReturnAction]; |
---|
424 | var EnemyActionLists = [RandomActionList, RotateActionList, |
---|
425 | RotateActionList5, RotateActionList10, |
---|
426 | DamageRandomActionList, DamageRotateActionList]; |
---|