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

Revision 108, 9.4 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
16var Barrage = function(bullet, size, color, frame, interval, speed, way, dir) {
17    this.bullet   = bullet;
18    this.size     = size;
19    this.color    = color;
20    this.frame    = frame;
21    this.interval = interval;
22    this.speed    = speed;
23    this.way      = way;
24    this.dir      = dir;
25
26    this.aim = function(trooper, enemy) {
27        if (!trooper || !enemy)
28            return 0.5 * Math.PI;
29        var dx = enemy.x - trooper.x;
30        var dy = enemy.y - trooper.y;
31        return Math.atan2(dy, dx);
32    };
33    this.angle = function(trooper, enemy) {
34        if (this.dir != null)
35            return this.dir;
36        return this.aim(trooper, enemy) / Math.PI;
37    };
38    this.update = function(trooper, enemies) {
39        return false;
40    };
41    this.posX = function() {
42        return 0;
43    };
44    this.posY = function() {
45        return 0;
46    };
47};
48
49var LinerBarrage = function(bullet, size, color, frame, interval, speed, way, dir) {
50    var that = new Barrage(bullet, size, color, frame, interval, speed, way, dir);
51
52    that.state  = 0;
53    that.update = function(trooper, enemies) {
54        if (++this.state < this.interval)
55            return false;
56
57        var dir = this.angle(trooper, enemies[0]);
58
59        trooper.addBullet(this.bullet, this.size, this.color,
60                          this.frame, this.posX(), this.posY(),
61                          dir * Math.PI, this.speed);
62
63        for (var i = 2; i < this.way + 1; i++) {
64            var d = 0.5 / i;
65
66            trooper.addBullet(this.bullet, this.size, this.color,
67                              this.frame, this.posX(), this.posY(),
68                              (dir - d) * Math.PI, this.speed);
69            trooper.addBullet(this.bullet, this.size, this.color,
70                              this.frame, this.posX(), this.posY(),
71                              (dir + d) * Math.PI, this.speed);
72        }
73
74        this.state = 0;
75        return true;
76    };
77
78    return that;
79};
80
81var ArchBarrage = function(bullet, size, color, frame,
82                           interval, speed, way, dir, delta) {
83
84    var that = new Barrage(bullet, size, color, frame, interval, speed, way, dir);
85
86    that.delta = delta;
87
88    if (!(that.way % 2))
89        that.way++;
90
91    if (that.delta == null)
92        that.delta = 0.5 / that.way;
93
94    that.state  = 0;
95    that.update = function(trooper, enemies) {
96        if (++this.state < this.interval)
97            return false;
98
99        var angle = this.angle(trooper, enemies[0]) - (this.delta * (this.way - 1) / 2);
100
101        for (var i = 0; i < this.way; i++) {
102            trooper.addBullet(this.bullet, this.size, this.color,
103                              this.frame, this.posX(), this.posY(),
104                              angle * Math.PI, this.speed);
105            angle += this.delta;
106        }
107
108        this.state = 0;
109        return true;
110    };
111
112    return that;
113};
114
115var CircularBarrage = function(bullet, size, color, frame,
116                               interval, speed, way, dir) {
117    var that = new ArchBarrage(bullet, size, color, frame,
118                               interval, speed, way, dir, 2 / way);
119    return that;
120};
121
122var DelayedArchBarrage = function(bullet, size, color, frame,
123                                  interval, speed, way, dir, delta) {
124
125    var that = new Barrage(bullet, size, color, frame, 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, enemies) {
140        if (++this.state < this.interval)
141            return false;
142
143        if (this.curAngle == null)
144            this.curAngle = this.angle(trooper, enemies[0]) -
145                this.delta * (this.way - 1) / 2;
146
147        trooper.addBullet(this.bullet, this.size, this.color,
148                          this.frame, this.posX(), this.posY(),
149                          this.curAngle * Math.PI, this.speed);
150
151        if (--this.remain <= 0) {
152            return this.reset();
153        }
154
155        this.curAngle += this.delta;
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, frame,
170                                      interval, speed, way, dir) {
171    var that = new DelayedArchBarrage(bullet, size, color, frame,
172                                      interval, speed, way, dir, 2 / way);
173    return that;
174};
175
176var DelayedRecursiveArchBarrage = function(bullet, size, color, frame,
177                                           interval, speed, way, dir, cnt) {
178
179    var that = new DelayedArchBarrage(bullet, size, color, frame,
180                                      interval, speed, way, dir);
181
182    that.i   = 0;
183    that.cnt = cnt;
184
185    if (that.cnt == null)
186        that.cnt = that.way / that.speed;
187
188    that.reset = function() {
189        if (++this.i < this.cnt) {
190            this.remain  = this.way;
191            this.delta  *= -1;
192            return false;
193        }
194        this.state    = 0;
195        this.remain   = this.way;
196        this.curAngle = null;
197        this.i        = 0;
198        return true;
199    };
200    return that;
201};
202
203var DelayedRecursiveCircularBarrage = function(bullet, size, color, frame,
204                                               interval, speed, way, dir, cnt) {
205
206    var that = new DelayedCircularBarrage(bullet, size, color, frame,
207                                          interval, speed, way, dir);
208
209    that.i   = 0;
210    that.cnt = cnt;
211
212    if (that.cnt == null)
213        that.cnt = that.way / that.speed;
214
215    that.reset = function() {
216        if (++this.i < this.cnt) {
217            this.remain = this.way;
218            return false;
219        }
220        this.state    = 0;
221        this.remain   = this.way;
222        this.curAngle = null;
223        this.i        = 0;
224        return true;
225    };
226    return that;
227};
228
229var MultiRounderBarrage = function(bullet, size, color, frame,
230                                   interval, speed, way, dir,
231                                   klass, radius, num) {
232    if (way < 3)
233        way = 3;
234
235    if (radius == null) {
236        radius = size * way * 3;
237        if (radius > 50)
238            radius = 50;
239    }
240
241    if (num == null)
242        num = Math.ceil((1 / way) * 5);
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, frame,
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, enemies) {
273        for (var i = 0; i < this.brrg.length; i++) {
274            if (!this.flag[i])
275                this.flag[i] = this.brrg[i].update(trooper, enemies);
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, frame,
289                                       interval, speed, way, dir) {
290    var that = new MultiRounderBarrage(bullet, size, color, frame,
291                                       interval, speed, way, dir, ArchBarrage);
292    return that;
293};
294
295var MultiRounderDelayedArchBarrage = function(bullet, size, color, frame,
296                                              interval, speed, way, dir) {
297    var that = new MultiRounderBarrage(bullet, size, color, frame,
298                                       interval, speed, way, dir, DelayedArchBarrage);
299    return that;
300};
301
302var MultiRounderDelayedRecursiveArchBarrage = function(bullet, size, color, frame,
303                                                       interval, speed, way, dir) {
304    var that = new MultiRounderBarrage(bullet, size, color, frame,
305                                       interval, speed, way, dir, DelayedRecursiveArchBarrage);
306    return that;
307};
308
309
310var EnemyBarrages = [LinerBarrage,
311                     ArchBarrage,
312                     CircularBarrage,
313                     DelayedArchBarrage,
314                     DelayedCircularBarrage,
315                     DelayedRecursiveArchBarrage,
316                     DelayedRecursiveCircularBarrage,
317                     MultiRounderArchBarrage,
318                     MultiRounderDelayedArchBarrage,
319                     MultiRounderDelayedRecursiveArchBarrage]
Note: See TracBrowser for help on using the repository browser.