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
RevLine 
[81]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
[106]16var Barrage = function(bullet, size, color, frame, interval, speed, way, dir) {
[81]17    this.bullet   = bullet;
18    this.size     = size;
19    this.color    = color;
[106]20    this.frame    = frame;
[81]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    };
[96]39    this.posX = function() {
40        return 0;
41    };
42    this.posY = function() {
43        return 0;
44    };
[81]45};
46
[106]47var LinerBarrage = function(bullet, size, color, frame, interval, speed, way, dir) {
48    var that = new Barrage(bullet, size, color, frame, interval, speed, way, dir);
[81]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,
[106]58                          this.frame, this.posX(), this.posY(),
[96]59                          dir * Math.PI, this.speed);
[81]60
[96]61        for (var i = 2; i < this.way + 1; i++) {
[81]62            var d = 0.5 / i;
63
64            trooper.addBullet(this.bullet, this.size, this.color,
[106]65                              this.frame, this.posX(), this.posY(),
[96]66                              (dir - d) * Math.PI, this.speed);
[81]67            trooper.addBullet(this.bullet, this.size, this.color,
[106]68                              this.frame, this.posX(), this.posY(),
[96]69                              (dir + d) * Math.PI, this.speed);
[81]70        }
71
72        this.state = 0;
73        return true;
74    };
75
76    return that;
77};
78
[106]79var ArchBarrage = function(bullet, size, color, frame,
[81]80                           interval, speed, way, dir, delta) {
81
[106]82    var that = new Barrage(bullet, size, color, frame, interval, speed, way, dir);
[81]83
84    that.delta = delta;
85
[106]86    if (!(that.way % 2))
[81]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
[106]97        var angle = this.angle(trooper, enemy) - (this.delta * (this.way - 1) / 2);
[81]98
[96]99        for (var i = 0; i < this.way; i++) {
[81]100            trooper.addBullet(this.bullet, this.size, this.color,
[106]101                              this.frame, this.posX(), this.posY(),
[96]102                              angle * Math.PI, this.speed);
[81]103            angle += this.delta;
104        }
105
106        this.state = 0;
107        return true;
108    };
109
110    return that;
111};
112
[106]113var CircularBarrage = function(bullet, size, color, frame,
[81]114                               interval, speed, way, dir) {
[106]115    var that = new ArchBarrage(bullet, size, color, frame,
[81]116                               interval, speed, way, dir, 2 / way);
117    return that;
[106]118};
[81]119
[106]120var DelayedArchBarrage = function(bullet, size, color, frame,
[81]121                                  interval, speed, way, dir, delta) {
122
[106]123    var that = new Barrage(bullet, size, color, frame, interval, speed, way, dir);
[81]124
125    that.delta = delta;
126
[106]127    if (!(that.way % 2))
[81]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;
[94]135    that.curAngle = null;
[81]136
137    that.update = function(trooper, enemy) {
138        if (++this.state < this.interval)
139            return false;
140
[94]141        if (this.curAngle == null)
[81]142            this.curAngle = this.angle(trooper, enemy) -
[106]143                this.delta * (this.way - 1) / 2;
[81]144
145        trooper.addBullet(this.bullet, this.size, this.color,
[106]146                          this.frame, this.posX(), this.posY(),
[96]147                          this.curAngle * Math.PI, this.speed);
[81]148
[91]149        if (--this.remain <= 0) {
[81]150            return this.reset();
151        }
152
[106]153        this.curAngle += this.delta;
154
[81]155        return false;
156    };
157    that.reset = function() {
158        this.state    = 0;
159        this.remain   = this.way;
[94]160        this.curAngle = null;
[81]161        return true;
162    };
163
164    return that;
165};
166
[106]167var DelayedCircularBarrage = function(bullet, size, color, frame,
[81]168                                      interval, speed, way, dir) {
[106]169    var that = new DelayedArchBarrage(bullet, size, color, frame,
[81]170                                      interval, speed, way, dir, 2 / way);
171    return that;
172};
173
[106]174var DelayedRecursiveArchBarrage = function(bullet, size, color, frame,
[81]175                                           interval, speed, way, dir, cnt) {
176
[106]177    var that = new DelayedArchBarrage(bullet, size, color, frame,
[81]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;
[94]194        this.curAngle = null;
[81]195        this.i        = 0;
196        return true;
197    };
198    return that;
199};
200
[106]201var DelayedRecursiveCircularBarrage = function(bullet, size, color, frame,
[81]202                                               interval, speed, way, dir, cnt) {
203
[106]204    var that = new DelayedCircularBarrage(bullet, size, color, frame,
[81]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;
[94]220        this.curAngle = null;
[81]221        this.i        = 0;
222        return true;
223    };
224    return that;
225};
226
[106]227var MultiRounderBarrage = function(bullet, size, color, frame,
[96]228                                   interval, speed, way, dir,
229                                   klass, radius, num) {
[106]230    if (way < 3)
231        way = 3;
232
[96]233    if (radius == null) {
[106]234        radius = size * way * 3;
[96]235        if (radius > 50)
236            radius = 50;
237    }
[81]238
[96]239    if (num == null)
[106]240        num = Math.ceil((1 / way) * 5);
[96]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++) {
[106]251            var b = new klass(bullet, size, color, frame,
[96]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
[106]286var MultiRounderArchBarrage = function(bullet, size, color, frame,
[96]287                                       interval, speed, way, dir) {
[106]288    var that = new MultiRounderBarrage(bullet, size, color, frame,
289                                       interval, speed, way, dir, ArchBarrage);
[96]290    return that;
291};
292
[106]293var MultiRounderDelayedArchBarrage = function(bullet, size, color, frame,
[96]294                                              interval, speed, way, dir) {
[106]295    var that = new MultiRounderBarrage(bullet, size, color, frame,
296                                       interval, speed, way, dir, DelayedArchBarrage);
[96]297    return that;
298};
299
[106]300var MultiRounderDelayedRecursiveArchBarrage = function(bullet, size, color, frame,
[96]301                                                       interval, speed, way, dir) {
[106]302    var that = new MultiRounderBarrage(bullet, size, color, frame,
303                                       interval, speed, way, dir, DelayedRecursiveArchBarrage);
[96]304    return that;
305};
306
307
[81]308var EnemyBarrages = [LinerBarrage,
309                     ArchBarrage,
310                     CircularBarrage,
311                     DelayedArchBarrage,
312                     DelayedCircularBarrage,
313                     DelayedRecursiveArchBarrage,
[96]314                     DelayedRecursiveCircularBarrage,
315                     MultiRounderArchBarrage,
316                     MultiRounderDelayedArchBarrage,
[106]317                     MultiRounderDelayedRecursiveArchBarrage]
Note: See TracBrowser for help on using the repository browser.