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

Revision 108, 10.2 KB checked in by atzm, 12 years ago (diff)
  • multi enemies 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, enemies) {
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, enemies);
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, enemies) {
128        if (trooper.barrages[this.index].update(trooper, enemies))
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, enemies) {
143        if (trooper.barrages[this.index].update(trooper, enemies)) {
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, enemies) {
172        if (trooper.isDead())
173            return;
174        this.shot.shot(trooper, enemies);
175        return this.move(trooper, enemies);
176    };
177    this.move = function(trooper, enemies) {
178        return true;
179    };
180};
181
182var ManualAction = function(shot) {
183    var that = new Action(shot);
184
185    that.move = function(trooper, enemies) {
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, enemies) {
219        if (this.count >= this.resolution) {
220            this.count      = 0;
221            this.resolution = resolution / trooper.speed;
222
223            this.p0 = this.getPoint0(trooper, enemies);
224            this.p1 = this.getPoint1(trooper, enemies);
225            this.p2 = this.getPoint2(trooper, enemies);
226            this.p3 = this.getPoint3(trooper, enemies);
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            3
242        );
243
244        if (this.count >= this.resolution)
245            return true;
246
247        return false;
248    };
249
250    return that;
251};
252
253var TriangleAction = function(shot, resolution) {
254    var that = new BezierAction(shot, resolution);
255
256    that.getPoint0 = function(trooper, enemies) {
257        return [trooper.x, trooper.y];
258    };
259    that.getPoint1 = function(trooper, enemies) {
260        return [trooper.x - (trooper.speed * 10),
261                trooper.y + (trooper.speed * 10)];
262    };
263    that.getPoint2 = function(trooper, enemies) {
264        return [trooper.x + (trooper.speed * 10),
265                trooper.y + (trooper.speed * 10)];
266    };
267    that.getPoint3 = function(trooper, enemies) {
268        return [trooper.x, trooper.y];
269    };
270
271    return that;
272};
273
274var RandomAction = function(shot, resolution) {
275    var that = new BezierAction(shot, resolution);
276
277    that.getPoint0 = function(trooper, enemies) {
278        return [trooper.x, trooper.y];
279    };
280    that.getPoint1 = function(trooper, enemies) {
281        return [Math.floor(Math.random() * trooper.w),
282                Math.floor(Math.random() * trooper.h)];
283    };
284    that.getPoint2 = function(trooper, enemies) {
285        return [Math.floor(Math.random() * trooper.w),
286                Math.floor(Math.random() * trooper.h)];
287    };
288    that.getPoint3 = function(trooper, enemies) {
289        return [Math.floor(Math.random() * trooper.w),
290                Math.floor(Math.random() * trooper.h)];
291    };
292
293    return that;
294};
295
296var RandomReturnAction = function(shot, resolution) {
297    var that = new RandomAction(shot, resolution);
298
299    that.getPoint3 = function(trooper, enemies) {
300        return [Math.floor(Math.random() * trooper.w), trooper.y];
301    };
302
303    return that;
304};
305
306var RandomStokingAction = function(shot, resolution) {
307    var that = new RandomAction(shot, resolution);
308
309    that.getPoint3 = function(trooper, enemies) {
310        var x = enemies.length ? enemies[0].x : 0;
311        return [x, Math.floor(Math.random() * trooper.h)];
312    };
313
314    return that;
315};
316
317var RandomStokingReturnAction = function(shot, resolution) {
318    var that = new RandomAction(shot, resolution);
319
320    that.getPoint3 = function(trooper, enemies) {
321        var x = enemies.length ? enemies[0].x : 0;
322        return [x, trooper.y];
323    };
324
325    return that;
326};
327
328
329/*
330 *  ActionList
331 *    - call / switch actions
332 */
333var ActionList = function(list) {
334    this.list   = list;
335    this.index  = 0;
336    this.update = function(trooper, enemies) {
337        this.updateIdx(this.list[this.index].update(trooper, enemies),
338                       trooper, enemies);
339    };
340    this.updateIdx = function(end, trooper, enemies) {
341        this.index = 0;
342    };
343};
344
345var RandomActionList = function(list) {
346    var that = new ActionList(list);
347
348    that.updateIdx = function(end, trooper, enemies) {
349        if (end)
350            this.index = Math.floor(Math.random() * this.list.length);
351    };
352
353    return that;
354};
355
356var RotateActionList = function(list, count) {
357    var that = new ActionList(list);
358
359    if (!count)
360        count = 0;
361
362    that.i         = 0;
363    that.updateIdx = function(end, trooper, enemies) {
364        if (end) {
365            if (++this.i > count) {
366                this.index++;
367                this.i = 0;
368            }
369            if (this.index >= this.list.length)
370                this.index = 0;
371        }
372    };
373
374    return that;
375};
376
377var RotateActionList5 = function(list) {
378    return new RotateActionList(list, 5);
379};
380
381var RotateActionList10 = function(list) {
382    return new RotateActionList(list, 10);
383};
384
385var DamageRandomActionList = function(list) {
386    var that = new ActionList(list);
387
388    that.life      = 0;
389    that.updateIdx = function(end, trooper, enemies) {
390        if (!this.life)
391            this.life = trooper.life;
392
393        if (end && trooper.life != this.life) {
394            this.index = Math.floor(Math.random() * this.list.length);
395            this.life  = trooper.life;
396        }
397    };
398
399    return that;
400};
401
402var DamageRotateActionList = function(list) {
403    var that = new ActionList(list);
404
405    that.life      = 0;
406    that.updateIdx = function(end, trooper, enemies) {
407        if (!this.life)
408            this.life = trooper.life;
409
410        if (end && trooper.life != this.life) {
411            if (++this.index >= this.list.length)
412                this.index = 0;
413
414            this.life = trooper.life;
415        }
416    };
417
418    return that;
419};
420
421
422var EnemyShots       = [BlazeRandomShot, BlazeRotateShot,
423                        BlazeRotateShot5, BlazeRotateShot10];
424var EnemyActions     = [TriangleAction, RandomAction,
425                        RandomReturnAction, RandomStokingAction,
426                        RandomStokingReturnAction];
427var EnemyActionLists = [RandomActionList, RotateActionList,
428                        RotateActionList5, RotateActionList10,
429                        DamageRandomActionList, DamageRotateActionList];
Note: See TracBrowser for help on using the repository browser.