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

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