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