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

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

moromoro

  • 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
[114]16var Bullet = function(size, color, frame, w, h, x, y, dir, speed) {
[81]17    this.size  = size;
18    this.color = color;
[106]19    this.frame = frame;
[114]20    this.w     = w;
21    this.h     = h;
[81]22    this.x     = x;
23    this.y     = y;
24    this.dir   = dir;
25    this.speed = speed;
26
[106]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    };
[81]41    this.draw = function(ctx) {
[106]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
[81]62        ctx.beginPath();
[95]63        ctx.fillStyle = this.getColor();
64        ctx.arc(this.x, this.y, this.getSize(), 0, Math.PI * 2.0, true);
[81]65        ctx.fill();
66        ctx.closePath();
67    };
68};
69
[114]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);
[81]72
73    that.deltaBaseX = Math.cos(that.dir);
74    that.deltaBaseY = Math.sin(that.dir);
75
[106]76    that.calcNext = function() {
77        return [this.x + this.getDeltaX(), this.y + this.getDeltaY()];
[81]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
[114]89var XReflectLinerBullet = function(size, color, frame, w, h, x, y, dir, speed) {
[115]90    var that = new LinerBullet(size, color, frame, w, h, x, y, dir, speed);
[81]91
[115]92    that.reflectCount = 0;
93
[114]94    that.calcNext = function() {
95        var x = this.x + this.getDeltaX();
96        var y = this.y + this.getDeltaY();
97
[115]98        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
99            this.reflectCount++;
[114]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
[101]116    that.dx = that.speed * that.deltaBaseX / 1.5;
117    that.dy = that.speed * that.deltaBaseY / 1.5;
[81]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
[114]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);
[81]135
136    that.i = 1;
137    that.getAxisDeltaX = function() {
[101]138        return this.getCurrentDelta(this.i, 1.5);
[81]139    };
140    that.getAxisDeltaY = function() {
[101]141        return this.getCurrentDelta(this.i++, 1.5);
[81]142    };
143
144    return that;
145};
146
[114]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);
[81]149
150    that.i = 1;
151    that.getAxisDeltaX = function() {
[101]152        return this.getCurrentDelta(this.i, 1.5);
[81]153    };
154    that.getAxisDeltaY = function() {
[101]155        return 1 / this.getCurrentDelta(this.i++, 1.5);
[81]156    };
157
158    return that;
159};
160
[114]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);
[81]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
[114]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);
[81]177
178    that.delta = 1 / (that.speed * 100);
179    that.i     = 1;
180
[114]181    that.getDir = function() {
182        return this.dir;
183    };
[106]184    that.calcNext = function() {
[114]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;
[105]188        this.dir += (this.delta / this.i) * this.getSign();
[81]189        this.i   += (this.delta / this.i);
[106]190        return [x, y];
[81]191    };
192
193    return that;
194};
195
[114]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);
[81]198
199    that.getSign = function() {
200        return 1;
201    };
202
203    return that;
204};
205
[114]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);
[81]208
209    that.getSign = function() {
210        return -1;
211    };
212
213    return that;
214};
215
[114]216var XReflectLeftCurveBullet = function(size, color, frame, w, h, x, y, dir, speed) {
[115]217    var that = new LeftCurveBullet(size, color, frame, w, h, x, y, dir, speed);
[81]218
[115]219    that.reflectCount = 0;
220
[114]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
[115]225        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
226            this.reflectCount++;
[114]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) {
[115]237    var that = new RightCurveBullet(size, color, frame, w, h, x, y, dir, speed);
[114]238
[115]239    that.reflectCount = 0;
240
[114]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
[115]245        if ((0 >= x || x >= this.w) && this.reflectCount < 5) {
246            this.reflectCount++;
[114]247            this.dir += (2 - this.dir) - (this.dir - 1);
248        }
249
250        return this.dir;
251    };
252
253    return that;
254};
255
[81]256var EnemyBullets = [LinerBullet,
[114]257                    XReflectLinerBullet,
[81]258                    XYExtendBullet,
259                    XExtendBullet,
260                    YExtendBullet,
261                    LeftCurveBullet,
[114]262                    RightCurveBullet,
263                    XReflectLeftCurveBullet,
264                    XReflectRightCurveBullet];
Note: See TracBrowser for help on using the repository browser.