source: pycodeshooter/trunk/shooter/bullet.js @ 114

Revision 114, 7.3 KB checked in by atzm, 12 years ago (diff)
  • add bullet patterns, xreflect series
  • 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 Bullet = function(size, color, frame, w, h, x, y, dir, speed) {
17    this.size  = size;
18    this.color = color;
19    this.frame = frame;
20    this.w     = w;
21    this.h     = h;
22    this.x     = x;
23    this.y     = y;
24    this.dir   = dir;
25    this.speed = speed;
26
27    this.getSize = function() {
28        return this.size;
29    };
30    this.getColor = function() {
31        return this.color;
32    };
33    this.next = function() {
34        var pos = this.calcNext();
35        this.x = pos[0];
36        this.y = pos[1];
37    };
38    this.vanished = function(width, height) {
39        return (width < this.x || height < this.y || this.x < 0 || this.y < 0);
40    };
41    this.draw = function(ctx) {
42        if (this.frame) {
43            var pos = this.calcNext();
44            var dir = Math.atan2(pos[0] - this.x, this.y - pos[1]);
45
46            ctx.save();
47
48            if (this.frame.style == "rect") {
49                ctx.beginPath();
50                ctx.strokeStyle = this.frame.color;
51                ctx.translate(this.x, this.y);
52                ctx.rotate(dir);
53                ctx.rect(-this.frame.width / 2, -this.frame.height / 2,
54                         this.frame.width, this.frame.height);
55                ctx.stroke();
56                ctx.closePath();
57            }
58
59            ctx.restore();
60        }
61
62        ctx.beginPath();
63        ctx.fillStyle = this.getColor();
64        ctx.arc(this.x, this.y, this.getSize(), 0, Math.PI * 2.0, true);
65        ctx.fill();
66        ctx.closePath();
67    };
68};
69
70var LinerBullet = function(size, color, frame, w, h, x, y, dir, speed) {
71    var that = new Bullet(size, color, frame, w, h, x, y, dir, speed);
72
73    that.deltaBaseX = Math.cos(that.dir);
74    that.deltaBaseY = Math.sin(that.dir);
75
76    that.calcNext = function() {
77        return [this.x + this.getDeltaX(), this.y + this.getDeltaY()];
78    };
79    that.getDeltaX = function() {
80        return this.speed * this.deltaBaseX;
81    };
82    that.getDeltaY = function() {
83        return this.speed * this.deltaBaseY;
84    };
85
86    return that;
87};
88
89var XReflectLinerBullet = function(size, color, frame, w, h, x, y, dir, speed) {
90    var that  = new LinerBullet(size, color, frame, w, h, x, y, dir, speed);
91    var count = 0;
92
93    that.calcNext = function() {
94        var x = this.x + this.getDeltaX();
95        var y = this.y + this.getDeltaY();
96
97        if ((0 >= x || x >= this.w) && count < 5) {
98            count++;
99            this.dir += (2 - this.dir) - (this.dir - 1);
100            this.deltaBaseX = Math.cos(this.dir);
101            this.deltaBaseY = Math.sin(this.dir);
102            x = this.x + this.getDeltaX();
103            y = this.y + this.getDeltaY();
104        }
105
106        return [x, y];
107    };
108
109    return that;
110}
111
112var AxisExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
113    var that = new LinerBullet(size, color, frame, w, h, x, y, dir, speed);
114
115    that.dx = that.speed * that.deltaBaseX / 1.5;
116    that.dy = that.speed * that.deltaBaseY / 1.5;
117
118    that.getDeltaX = function() {
119        return this.dx * this.getAxisDeltaX();
120    };
121    that.getDeltaY = function() {
122        return this.dy * this.getAxisDeltaY();
123    };
124    that.getCurrentDelta = function(count, thresh) {
125        var c = count / this.speed;
126        return c > thresh ? thresh : c;
127    }
128
129    return that;
130};
131
132var XYExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
133    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
134
135    that.i = 1;
136    that.getAxisDeltaX = function() {
137        return this.getCurrentDelta(this.i, 1.5);
138    };
139    that.getAxisDeltaY = function() {
140        return this.getCurrentDelta(this.i++, 1.5);
141    };
142
143    return that;
144};
145
146var XExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
147    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
148
149    that.i = 1;
150    that.getAxisDeltaX = function() {
151        return this.getCurrentDelta(this.i, 1.5);
152    };
153    that.getAxisDeltaY = function() {
154        return 1 / this.getCurrentDelta(this.i++, 1.5);
155    };
156
157    return that;
158};
159
160var YExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
161    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
162
163    that.i = 1;
164    that.getAxisDeltaX = function() {
165        return 1 / this.getCurrentDelta(this.i, 2);
166    };
167    that.getAxisDeltaY = function() {
168        return this.getCurrentDelta(this.i++, 2);
169    };
170
171    return that;
172};
173
174var CurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
175    var that = new Bullet(size, color, frame, w, h, x, y, dir, speed);
176
177    that.delta = 1 / (that.speed * 100);
178    that.i     = 1;
179
180    that.getDir = function() {
181        return this.dir;
182    };
183    that.calcNext = function() {
184        var d = this.getDir();
185        var x = this.x + Math.cos(d) * this.i;
186        var y = this.y + Math.sin(d) * this.i;
187        this.dir += (this.delta / this.i) * this.getSign();
188        this.i   += (this.delta / this.i);
189        return [x, y];
190    };
191
192    return that;
193};
194
195var LeftCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
196    var that = new CurveBullet(size, color, frame, w, h, x, y, dir, speed);
197
198    that.getSign = function() {
199        return 1;
200    };
201
202    return that;
203};
204
205var RightCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
206    var that = new CurveBullet(size, color, frame, w, h, x, y, dir, speed);
207
208    that.getSign = function() {
209        return -1;
210    };
211
212    return that;
213};
214
215var XReflectLeftCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
216    var that  = new LeftCurveBullet(size, color, frame, w, h, x, y, dir, speed);
217    var count = 0;
218
219    that.getDir = function() {
220        var x = this.x + Math.cos(this.dir) * this.i;
221        var y = this.y + Math.sin(this.dir) * this.i;
222
223        if ((0 >= x || x >= this.w) && count < 5) {
224            count++;
225            this.dir += (2 - this.dir) - (this.dir - 1);
226        }
227
228        return this.dir;
229    };
230
231    return that;
232};
233
234var XReflectRightCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
235    var that  = new RightCurveBullet(size, color, frame, w, h, x, y, dir, speed);
236    var count = 0;
237
238    that.getDir = function() {
239        var x = this.x + Math.cos(this.dir) * this.i;
240        var y = this.y + Math.sin(this.dir) * this.i;
241
242        if ((0 >= x || x >= this.w) && count < 5) {
243            count++;
244            this.dir += (2 - this.dir) - (this.dir - 1);
245        }
246
247        return this.dir;
248    };
249
250    return that;
251};
252
253var EnemyBullets = [LinerBullet,
254                    XReflectLinerBullet,
255                    XYExtendBullet,
256                    XExtendBullet,
257                    YExtendBullet,
258                    LeftCurveBullet,
259                    RightCurveBullet,
260                    XReflectLeftCurveBullet,
261                    XReflectRightCurveBullet];
Note: See TracBrowser for help on using the repository browser.