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

Revision 115, 7.4 KB checked in by atzm, 12 years ago (diff)

moromoro

  • 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
92    that.reflectCount = 0;
93
94    that.calcNext = function() {
95        var x = this.x + this.getDeltaX();
96        var y = this.y + this.getDeltaY();
97
98        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
99            this.reflectCount++;
100            this.dir += (2 - this.dir) - (this.dir - 1);
101            this.deltaBaseX = Math.cos(this.dir);
102            this.deltaBaseY = Math.sin(this.dir);
103            x = this.x + this.getDeltaX();
104            y = this.y + this.getDeltaY();
105        }
106
107        return [x, y];
108    };
109
110    return that;
111}
112
113var AxisExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
114    var that = new LinerBullet(size, color, frame, w, h, x, y, dir, speed);
115
116    that.dx = that.speed * that.deltaBaseX / 1.5;
117    that.dy = that.speed * that.deltaBaseY / 1.5;
118
119    that.getDeltaX = function() {
120        return this.dx * this.getAxisDeltaX();
121    };
122    that.getDeltaY = function() {
123        return this.dy * this.getAxisDeltaY();
124    };
125    that.getCurrentDelta = function(count, thresh) {
126        var c = count / this.speed;
127        return c > thresh ? thresh : c;
128    }
129
130    return that;
131};
132
133var XYExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
134    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
135
136    that.i = 1;
137    that.getAxisDeltaX = function() {
138        return this.getCurrentDelta(this.i, 1.5);
139    };
140    that.getAxisDeltaY = function() {
141        return this.getCurrentDelta(this.i++, 1.5);
142    };
143
144    return that;
145};
146
147var XExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
148    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
149
150    that.i = 1;
151    that.getAxisDeltaX = function() {
152        return this.getCurrentDelta(this.i, 1.5);
153    };
154    that.getAxisDeltaY = function() {
155        return 1 / this.getCurrentDelta(this.i++, 1.5);
156    };
157
158    return that;
159};
160
161var YExtendBullet = function(size, color, frame, w, h, x, y, dir, speed) {
162    var that = new AxisExtendBullet(size, color, frame, w, h, x, y, dir, speed);
163
164    that.i = 1;
165    that.getAxisDeltaX = function() {
166        return 1 / this.getCurrentDelta(this.i, 2);
167    };
168    that.getAxisDeltaY = function() {
169        return this.getCurrentDelta(this.i++, 2);
170    };
171
172    return that;
173};
174
175var CurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
176    var that = new Bullet(size, color, frame, w, h, x, y, dir, speed);
177
178    that.delta = 1 / (that.speed * 100);
179    that.i     = 1;
180
181    that.getDir = function() {
182        return this.dir;
183    };
184    that.calcNext = function() {
185        var d = this.getDir();
186        var x = this.x + Math.cos(d) * this.i;
187        var y = this.y + Math.sin(d) * this.i;
188        this.dir += (this.delta / this.i) * this.getSign();
189        this.i   += (this.delta / this.i);
190        return [x, y];
191    };
192
193    return that;
194};
195
196var LeftCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
197    var that = new CurveBullet(size, color, frame, w, h, x, y, dir, speed);
198
199    that.getSign = function() {
200        return 1;
201    };
202
203    return that;
204};
205
206var RightCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
207    var that = new CurveBullet(size, color, frame, w, h, x, y, dir, speed);
208
209    that.getSign = function() {
210        return -1;
211    };
212
213    return that;
214};
215
216var XReflectLeftCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
217    var that = new LeftCurveBullet(size, color, frame, w, h, x, y, dir, speed);
218
219    that.reflectCount = 0;
220
221    that.getDir = function() {
222        var x = this.x + Math.cos(this.dir) * this.i;
223        var y = this.y + Math.sin(this.dir) * this.i;
224
225        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
226            this.reflectCount++;
227            this.dir += (2 - this.dir) - (this.dir - 1);
228        }
229
230        return this.dir;
231    };
232
233    return that;
234};
235
236var XReflectRightCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
237    var that = new RightCurveBullet(size, color, frame, w, h, x, y, dir, speed);
238
239    that.reflectCount = 0;
240
241    that.getDir = function() {
242        var x = this.x + Math.cos(this.dir) * this.i;
243        var y = this.y + Math.sin(this.dir) * this.i;
244
245        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
246            this.reflectCount++;
247            this.dir += (2 - this.dir) - (this.dir - 1);
248        }
249
250        return this.dir;
251    };
252
253    return that;
254};
255
256var EnemyBullets = [LinerBullet,
257                    XReflectLinerBullet,
258                    XYExtendBullet,
259                    XExtendBullet,
260                    YExtendBullet,
261                    LeftCurveBullet,
262                    RightCurveBullet,
263                    XReflectLeftCurveBullet,
264                    XReflectRightCurveBullet];
Note: See TracBrowser for help on using the repository browser.