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

Revision 94, 6.2 KB checked in by atzm, 13 years ago (diff)

fixed 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};
39
40var LinerBarrage = function(bullet, size, color, interval, speed, way, dir) {
41    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
42
43    that.state  = 0;
44    that.update = function(trooper, enemy) {
45        if (++this.state < this.interval)
46            return false;
47
48        var dir = this.angle(trooper, enemy);
49
50        trooper.addBullet(this.bullet, this.size, this.color,
51                          0, 0, dir * Math.PI, this.speed);
52
53        for (i = 2; i < this.way + 1; i++) {
54            var d = 0.5 / i;
55
56            trooper.addBullet(this.bullet, this.size, this.color,
57                              0, 0, (dir - d) * Math.PI, this.speed);
58            trooper.addBullet(this.bullet, this.size, this.color,
59                              0, 0, (dir + d) * Math.PI, this.speed);
60        }
61
62        this.state = 0;
63        return true;
64    };
65
66    return that;
67};
68
69var ArchBarrage = function(bullet, size, color,
70                           interval, speed, way, dir, delta) {
71
72    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
73
74    that.delta = delta;
75
76    if (that.way % 2)
77        that.way++;
78
79    if (that.delta == null)
80        that.delta = 0.5 / that.way;
81
82    that.state  = 0;
83    that.update = function(trooper, enemy) {
84        if (++this.state < this.interval)
85            return false;
86
87        var angle = this.angle(trooper, enemy) - (this.delta * this.way / 2);
88
89        for (i = 0; i < this.way; i++) {
90            trooper.addBullet(this.bullet, this.size, this.color,
91                              0, 0, angle * Math.PI, this.speed);
92            angle += this.delta;
93        }
94
95        this.state = 0;
96        return true;
97    };
98
99    return that;
100};
101
102var CircularBarrage = function(bullet, size, color,
103                               interval, speed, way, dir) {
104    if (way % 2)
105        way++;
106
107    var that = new ArchBarrage(bullet, size, color,
108                               interval, speed, way, dir, 2 / way);
109    return that;
110}
111
112var DelayedArchBarrage = function(bullet, size, color,
113                                  interval, speed, way, dir, delta) {
114
115    var that = new Barrage(bullet, size, color, interval, speed, way, dir);
116
117    that.delta = delta;
118
119    if (that.way % 2)
120        that.way++;
121
122    if (that.delta == null)
123        that.delta = 0.5 / that.way;
124
125    that.state    = 0;
126    that.remain   = that.way;
127    that.curAngle = null;
128
129    that.update = function(trooper, enemy) {
130        if (++this.state < this.interval)
131            return false;
132
133        if (this.curAngle == null)
134            this.curAngle = this.angle(trooper, enemy) -
135                this.delta * (this.way / 2);
136
137        trooper.addBullet(this.bullet, this.size, this.color,
138                          0, 0, this.curAngle * Math.PI, this.speed);
139
140        this.curAngle += this.delta;
141
142        if (--this.remain <= 0) {
143            return this.reset();
144        }
145
146        return false;
147    };
148    that.reset = function() {
149        this.state    = 0;
150        this.remain   = this.way;
151        this.curAngle = null;
152        return true;
153    };
154
155    return that;
156};
157
158var DelayedCircularBarrage = function(bullet, size, color,
159                                      interval, speed, way, dir) {
160    if (way % 2)
161        way++;
162
163    var that = new DelayedArchBarrage(bullet, size, color,
164                                      interval, speed, way, dir, 2 / way);
165    return that;
166};
167
168var DelayedRecursiveArchBarrage = function(bullet, size, color,
169                                           interval, speed, way, dir, cnt) {
170
171    var that = new DelayedArchBarrage(bullet, size, color,
172                                      interval, speed, way, dir);
173
174    that.i   = 0;
175    that.cnt = cnt;
176
177    if (that.cnt == null)
178        that.cnt = that.way / that.speed;
179
180    that.reset = function() {
181        if (++this.i < this.cnt) {
182            this.remain  = this.way;
183            this.delta  *= -1;
184            return false;
185        }
186        this.state    = 0;
187        this.remain   = this.way;
188        this.curAngle = null;
189        this.i        = 0;
190        return true;
191    };
192    return that;
193};
194
195var DelayedRecursiveCircularBarrage = function(bullet, size, color,
196                                               interval, speed, way, dir, cnt) {
197
198    var that = new DelayedCircularBarrage(bullet, size, color,
199                                          interval, speed, way, dir);
200
201    that.i   = 0;
202    that.cnt = cnt;
203
204    if (that.cnt == null)
205        that.cnt = that.way / that.speed;
206
207    that.reset = function() {
208        if (++this.i < this.cnt) {
209            this.remain = this.way;
210            return false;
211        }
212        this.state    = 0;
213        this.remain   = this.way;
214        this.curAngle = null;
215        this.i        = 0;
216        return true;
217    };
218    return that;
219};
220
221
222var EnemyBarrages = [LinerBarrage,
223                     ArchBarrage,
224                     CircularBarrage,
225                     DelayedArchBarrage,
226                     DelayedCircularBarrage,
227                     DelayedRecursiveArchBarrage,
228                     DelayedRecursiveCircularBarrage];
Note: See TracBrowser for help on using the repository browser.