source: pycodeshooter/trunk/shooter/barrage.js @ 96

Revision 96, 10.2 KB checked in by atzm, 13 years ago (diff)
  • add barrage patterns
  • fixed a trivial bug
  • 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
16var Barrage = function(bullet, size, color, interval, speed, way, dir) {
17    this.bullet   = bullet;
18    this.size     = size;
19    this.color    = color;
20    this.interval = interval;
21    this.speed    = speed;
22    this.way      = way;
23    this.dir      = dir;
24
25    this.aim = function(trooper, enemy) {
26        var dx = enemy.x - trooper.x;
27        var dy = enemy.y - trooper.y;
28        return Math.atan2(dy, dx);
29    };
30    this.angle = function(trooper, enemy) {
31        if (this.dir != null)
32            return this.dir;
33        return this.aim(trooper, enemy) / Math.PI;
34    };
35    this.update = function(trooper, enemy) {
36        return false;
37    };
38    this.posX = function() {
39        return 0;
40    };
41    this.posY = function() {
42        return 0;
43    };
44};
45
46var LinerBarrage = function(bullet, size, color, interval, speed, way, dir) {
47    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
48
49    that.state  = 0;
50    that.update = function(trooper, enemy) {
51        if (++this.state < this.interval)
52            return false;
53
54        var dir = this.angle(trooper, enemy);
55
56        trooper.addBullet(this.bullet, this.size, this.color,
57                          this.posX(), this.posY(),
58                          dir * Math.PI, this.speed);
59
60        for (var i = 2; i < this.way + 1; i++) {
61            var d = 0.5 / i;
62
63            trooper.addBullet(this.bullet, this.size, this.color,
64                              this.posX(), this.posY(),
65                              (dir - d) * Math.PI, this.speed);
66            trooper.addBullet(this.bullet, this.size, this.color,
67                              this.posX(), this.posY(),
68                              (dir + d) * Math.PI, this.speed);
69        }
70
71        this.state = 0;
72        return true;
73    };
74
75    return that;
76};
77
78var ArchBarrage = function(bullet, size, color,
79                           interval, speed, way, dir, delta) {
80
81    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
82
83    that.delta = delta;
84
85    if (that.way % 2)
86        that.way++;
87
88    if (that.delta == null)
89        that.delta = 0.5 / that.way;
90
91    that.state  = 0;
92    that.update = function(trooper, enemy) {
93        if (++this.state < this.interval)
94            return false;
95
96        var angle = this.angle(trooper, enemy) - (this.delta * this.way / 2);
97
98        for (var i = 0; i < this.way; i++) {
99            trooper.addBullet(this.bullet, this.size, this.color,
100                              this.posX(), this.posY(),
101                              angle * Math.PI, this.speed);
102            angle += this.delta;
103        }
104
105        this.state = 0;
106        return true;
107    };
108
109    return that;
110};
111
112var CircularBarrage = function(bullet, size, color,
113                               interval, speed, way, dir) {
114    if (way % 2)
115        way++;
116
117    var that = new ArchBarrage(bullet, size, color,
118                               interval, speed, way, dir, 2 / way);
119    return that;
120}
121
122var DelayedArchBarrage = function(bullet, size, color,
123                                  interval, speed, way, dir, delta) {
124
125    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
126
127    that.delta = delta;
128
129    if (that.way % 2)
130        that.way++;
131
132    if (that.delta == null)
133        that.delta = 0.5 / that.way;
134
135    that.state    = 0;
136    that.remain   = that.way;
137    that.curAngle = null;
138
139    that.update = function(trooper, enemy) {
140        if (++this.state < this.interval)
141            return false;
142
143        if (this.curAngle == null)
144            this.curAngle = this.angle(trooper, enemy) -
145                this.delta * (this.way / 2);
146
147        trooper.addBullet(this.bullet, this.size, this.color,
148                          this.posX(), this.posY(),
149                          this.curAngle * Math.PI, this.speed);
150
151        this.curAngle += this.delta;
152
153        if (--this.remain <= 0) {
154            return this.reset();
155        }
156
157        return false;
158    };
159    that.reset = function() {
160        this.state    = 0;
161        this.remain   = this.way;
162        this.curAngle = null;
163        return true;
164    };
165
166    return that;
167};
168
169var DelayedCircularBarrage = function(bullet, size, color,
170                                      interval, speed, way, dir) {
171    if (way % 2)
172        way++;
173
174    var that = new DelayedArchBarrage(bullet, size, color,
175                                      interval, speed, way, dir, 2 / way);
176    return that;
177};
178
179var DelayedRecursiveArchBarrage = function(bullet, size, color,
180                                           interval, speed, way, dir, cnt) {
181
182    var that = new DelayedArchBarrage(bullet, size, color,
183                                      interval, speed, way, dir);
184
185    that.i   = 0;
186    that.cnt = cnt;
187
188    if (that.cnt == null)
189        that.cnt = that.way / that.speed;
190
191    that.reset = function() {
192        if (++this.i < this.cnt) {
193            this.remain  = this.way;
194            this.delta  *= -1;
195            return false;
196        }
197        this.state    = 0;
198        this.remain   = this.way;
199        this.curAngle = null;
200        this.i        = 0;
201        return true;
202    };
203    return that;
204};
205
206var DelayedRecursiveCircularBarrage = function(bullet, size, color,
207                                               interval, speed, way, dir, cnt) {
208
209    var that = new DelayedCircularBarrage(bullet, size, color,
210                                          interval, speed, way, dir);
211
212    that.i   = 0;
213    that.cnt = cnt;
214
215    if (that.cnt == null)
216        that.cnt = that.way / that.speed;
217
218    that.reset = function() {
219        if (++this.i < this.cnt) {
220            this.remain = this.way;
221            return false;
222        }
223        this.state    = 0;
224        this.remain   = this.way;
225        this.curAngle = null;
226        this.i        = 0;
227        return true;
228    };
229    return that;
230};
231
232var MultiRounderBarrage = function(bullet, size, color,
233                                   interval, speed, way, dir,
234                                   klass, radius, num) {
235    if (radius == null) {
236        radius = size * way;
237        if (radius > 50)
238            radius = 50;
239    }
240
241    if (num == null)
242        num = Math.ceil((1 / way) * 50);
243
244    var start = 0.25;
245
246    this.initBarrage = function() {
247        this.brrg = new Array();
248        this.flag = new Array();
249
250        var w = Math.ceil(way / num);
251
252        for (var i = 0; i < num; i++) {
253            var b = new klass(bullet, size, color,
254                              interval, speed, w, dir);
255
256            b.r = radius;
257            b.posX = function() {
258                start += 1 / (num * 2);
259                this.a = i * (1 / num) + start;
260                return this.r * Math.cos(2 * Math.PI * this.a);
261            };
262
263            b.posY = function() {
264                return this.r * Math.sin(2 * Math.PI * this.a);
265            };
266
267            this.brrg.push(b);
268            this.flag.push(false);
269        }
270    };
271
272    this.update = function(trooper, enemy) {
273        for (var i = 0; i < this.brrg.length; i++) {
274            if (!this.flag[i])
275                this.flag[i] = this.brrg[i].update(trooper, enemy);
276        }
277        for (var i = 0; i < this.flag.length; i++) {
278            if (!this.flag[i])
279                return false;
280        }
281        this.initBarrage();
282        return true;
283    };
284
285    this.initBarrage();
286};
287
288var MultiRounderArchBarrage = function(bullet, size, color,
289                                       interval, speed, way, dir) {
290    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
291                                       way, dir, ArchBarrage);
292    return that;
293};
294
295var MultiRounderCircularBarrage = function(bullet, size, color,
296                                           interval, speed, way, dir) {
297    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
298                                       way, dir, CircularBarrage);
299    return that;
300};
301
302var MultiRounderDelayedArchBarrage = function(bullet, size, color,
303                                              interval, speed, way, dir) {
304    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
305                                       way, dir, DelayedArchBarrage);
306    return that;
307};
308
309var MultiRounderDelayedCircularBarrage = function(bullet, size, color,
310                                                  interval, speed, way, dir) {
311    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
312                                       way, dir, DelayedCircularBarrage);
313    return that;
314};
315
316var MultiRounderDelayedRecursiveArchBarrage = function(bullet, size, color,
317                                                       interval, speed, way, dir) {
318    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
319                                       way, dir, DelayedRecursiveArchBarrage);
320    return that;
321};
322
323var MultiRounderDelayedRecursiveCircularBarrage = function(bullet, size, color,
324                                                           interval, speed, way, dir) {
325    var that = new MultiRounderBarrage(bullet, size, color, interval, speed,
326                                       way, dir, DelayedRecursiveCircularBarrage);
327    return that;
328};
329
330
331var EnemyBarrages = [LinerBarrage,
332                     ArchBarrage,
333                     CircularBarrage,
334                     DelayedArchBarrage,
335                     DelayedCircularBarrage,
336                     DelayedRecursiveArchBarrage,
337                     DelayedRecursiveCircularBarrage,
338                     MultiRounderArchBarrage,
339                     MultiRounderCircularBarrage,
340                     MultiRounderDelayedArchBarrage,
341                     MultiRounderDelayedCircularBarrage,
342                     MultiRounderDelayedRecursiveArchBarrage,
343                     MultiRounderDelayedRecursiveCircularBarrage];
Note: See TracBrowser for help on using the repository browser.