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

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

chousei

  • 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            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, enemy) {
257        return [trooper.x, trooper.y];
258    };
259    that.getPoint1 = function(trooper, enemy) {
260        return [trooper.x - (trooper.speed * 10),
261                trooper.y + (trooper.speed * 10)];
262    };
263    that.getPoint2 = function(trooper, enemy) {
264        return [trooper.x + (trooper.speed * 10),
265                trooper.y + (trooper.speed * 10)];
266    };
267    that.getPoint3 = function(trooper, enemy) {
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, enemy) {
278        return [trooper.x, trooper.y];
279    };
280    that.getPoint1 = function(trooper, enemy) {
281        return [Math.floor(Math.random() * trooper.w),
282                Math.floor(Math.random() * trooper.h)];
283    };
284    that.getPoint2 = function(trooper, enemy) {
285        return [Math.floor(Math.random() * trooper.w),
286                Math.floor(Math.random() * trooper.h)];
287    };
288    that.getPoint3 = function(trooper, enemy) {
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, enemy) {
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, enemy) {
310        return [enemy.x, Math.floor(Math.random() * trooper.h)];
311    };
312
313    return that;
314};
315
316var RandomStokingReturnAction = function(shot, resolution) {
317    var that = new RandomAction(shot, resolution);
318
319    that.getPoint3 = function(trooper, enemy) {
320        return [enemy.x, trooper.y];
321    };
322
323    return that;
324};
325
326
327/*
328 *  ActionList
329 *    - call / switch actions
330 */
331var ActionList = function(list) {
332    this.list   = list;
333    this.index  = 0;
334    this.update = function(trooper, enemy) {
335        this.updateIdx(this.list[this.index].update(trooper, enemy),
336                       trooper, enemy);
337    };
338    this.updateIdx = function(end, trooper, enemy) {
339        this.index = 0;
340    };
341};
342
343var RandomActionList = function(list) {
344    var that = new ActionList(list);
345
346    that.updateIdx = function(end, trooper, enemy) {
347        if (end)
348            this.index = Math.floor(Math.random() * this.list.length);
349    };
350
351    return that;
352};
353
354var RotateActionList = function(list, count) {
355    var that = new ActionList(list);
356
357    if (!count)
358        count = 0;
359
360    that.i         = 0;
361    that.updateIdx = function(end, trooper, enemy) {
362        if (end) {
363            if (++this.i > count) {
364                this.index++;
365                this.i = 0;
366            }
367            if (this.index >= this.list.length)
368                this.index = 0;
369        }
370    };
371
372    return that;
373};
374
375var RotateActionList5 = function(list) {
376    return new RotateActionList(list, 5);
377};
378
379var RotateActionList10 = function(list) {
380    return new RotateActionList(list, 10);
381};
382
383var DamageRandomActionList = function(list) {
384    var that = new ActionList(list);
385
386    that.life      = 0;
387    that.updateIdx = function(end, trooper, enemy) {
388        if (!this.life)
389            this.life = trooper.life;
390
391        if (end && trooper.life != this.life) {
392            this.index = Math.floor(Math.random() * this.list.length);
393            this.life  = trooper.life;
394        }
395    };
396
397    return that;
398};
399
400var DamageRotateActionList = function(list) {
401    var that = new ActionList(list);
402
403    that.life      = 0;
404    that.updateIdx = function(end, trooper, enemy) {
405        if (!this.life)
406            this.life = trooper.life;
407
408        if (end && trooper.life != this.life) {
409            if (++this.index >= this.list.length)
410                this.index = 0;
411
412            this.life = trooper.life;
413        }
414    };
415
416    return that;
417};
418
419
420var EnemyShots       = [BlazeRandomShot, BlazeRotateShot,
421                        BlazeRotateShot5, BlazeRotateShot10];
422var EnemyActions     = [TriangleAction, RandomAction,
423                        RandomReturnAction, RandomStokingAction,
424                        RandomStokingReturnAction];
425var EnemyActionLists = [RandomActionList, RotateActionList,
426                        RotateActionList5, RotateActionList10,
427                        DamageRandomActionList, DamageRotateActionList];
Note: See TracBrowser for help on using the repository browser.