source: pycodeshooter/trunk/shooter/action.js @ 92

Revision 92, 10.0 KB checked in by atzm, 13 years ago (diff)

bomb support

  • Property svn:keywords set to Id
Line 
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
16const ACTION_UP     = 1;
17const ACTION_DOWN   = 2;
18const ACTION_LEFT   = 4;
19const ACTION_RIGHT  = 8;
20const ACTION_SHOT   = 16;
21const ACTION_DELAY  = 32;
22const SWITCH_BULLET = 1;
23const SWITCH_BOMB   = 2;
24var   currentAction = 0;
25var   currentSwitch = 0;
26
27
28/*
29 *  Functions
30 */
31function 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
58function 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
68function setKeyDown(keyCode) {
69    currentAction |= getActionByKeyCode(keyCode);
70}
71
72function setKeyUp(keyCode) {
73    currentAction &= (0xffff - getActionByKeyCode(keyCode));
74}
75
76function isInAction(act) {
77    return currentAction & act;
78}
79
80function setKeyPress(charCode) {
81    currentSwitch |= getSwitchByCharCode(charCode);
82}
83
84function unsetSwitch(sw) {
85    currentSwitch &= (0xffff - sw);
86}
87
88function isSetSwitch(sw) {
89    return currentSwitch & sw;
90}
91
92
93/*
94 *  Shot
95 *    - update / switch barrages
96 */
97var Shot = function() {
98    this.shot = function() {
99    };
100};
101
102var 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
123var 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
135var 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
156var BlazeRotateShot5 = function() {
157    return new BlazeRotateShot(5);
158};
159
160var BlazeRotateShot10 = function() {
161    return new BlazeRotateShot(10);
162};
163
164
165/*
166 *  Action
167 *    - call shot / move trooper
168 */
169var 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
182var 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
209var 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
252var 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
273var 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
295var 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
305var 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
315var 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 */
330var 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
342var 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
353var 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
374var RotateActionList5 = function(list) {
375    return new RotateActionList(list, 5);
376};
377
378var RotateActionList10 = function(list) {
379    return new RotateActionList(list, 10);
380};
381
382var 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
399var 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
419var EnemyShots       = [BlazeRandomShot, BlazeRotateShot,
420                        BlazeRotateShot5, BlazeRotateShot10];
421var EnemyActions     = [TriangleAction, RandomAction,
422                        RandomReturnAction, RandomStokingAction,
423                        RandomStokingReturnAction];
424var EnemyActionLists = [RandomActionList, RotateActionList,
425                        RotateActionList5, RotateActionList10,
426                        DamageRandomActionList, DamageRotateActionList];
Note: See TracBrowser for help on using the repository browser.