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

Revision 95, 4.3 KB checked in by atzm, 13 years ago (diff)

tiun tiun

  • 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, x, y, dir, speed) {
17    this.size  = size;
18    this.color = color;
19    this.x     = x;
20    this.y     = y;
21    this.dir   = dir;
22    this.speed = speed;
23
24    this.draw = function(ctx) {
25        ctx.beginPath();
26        ctx.fillStyle = this.getColor();
27        ctx.arc(this.x, this.y, this.getSize(), 0, Math.PI * 2.0, true);
28        ctx.fill();
29        ctx.closePath();
30    };
31    this.vanished = function(width, height) {
32        return (width < this.x || height < this.y || this.x < 0 || this.y < 0);
33    };
34    this.getSize = function() {
35        return this.size;
36    };
37    this.getColor = function() {
38        return this.color;
39    };
40};
41
42var LinerBullet = function(size, color, x, y, dir, speed) {
43    var that = new Bullet(size, color, x, y, dir, speed);
44
45    that.deltaBaseX = Math.cos(that.dir);
46    that.deltaBaseY = Math.sin(that.dir);
47
48    that.next = function() {
49        this.x += this.getDeltaX();
50        this.y += this.getDeltaY();
51    };
52    that.getDeltaX = function() {
53        return this.speed * this.deltaBaseX;
54    };
55    that.getDeltaY = function() {
56        return this.speed * this.deltaBaseY;
57    };
58
59    return that;
60};
61
62var AxisExtendBullet = function(size, color, x, y, dir, speed) {
63    var that = new LinerBullet(size, color, x, y, dir, speed);
64
65    that.dx = that.speed * that.deltaBaseX / 2;
66    that.dy = that.speed * that.deltaBaseY / 2;
67
68    that.getDeltaX = function() {
69        return this.dx * this.getAxisDeltaX();
70    };
71    that.getDeltaY = function() {
72        return this.dy * this.getAxisDeltaY();
73    };
74    that.getCurrentDelta = function(count, thresh) {
75        var c = count / this.speed;
76        return c > thresh ? thresh : c;
77    }
78
79    return that;
80};
81
82var XYExtendBullet = function(size, color, x, y, dir, speed) {
83    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
84
85    that.i = 1;
86    that.getAxisDeltaX = function() {
87        return this.getCurrentDelta(this.i, 2);
88    };
89    that.getAxisDeltaY = function() {
90        return this.getCurrentDelta(this.i++, 2);
91    };
92
93    return that;
94};
95
96var XExtendBullet = function(size, color, x, y, dir, speed) {
97    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
98
99    that.i = 1;
100    that.getAxisDeltaX = function() {
101        return this.getCurrentDelta(this.i, 2);
102    };
103    that.getAxisDeltaY = function() {
104        return 1 / this.getCurrentDelta(this.i++, 2);
105    };
106
107    return that;
108};
109
110var YExtendBullet = function(size, color, x, y, dir, speed) {
111    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
112
113    that.i = 1;
114    that.getAxisDeltaX = function() {
115        return 1 / this.getCurrentDelta(this.i, 2);
116    };
117    that.getAxisDeltaY = function() {
118        return this.getCurrentDelta(this.i++, 2);
119    };
120
121    return that;
122};
123
124var CurveBullet = function(size, color, x, y, dir, speed) {
125    var that = new Bullet(size, color, x, y, dir, speed);
126
127    that.delta = 1 / (that.speed * 100);
128    that.i     = 1;
129
130    that.next = function() {
131        this.x   += Math.cos(this.dir) * this.i * this.getSign();
132        this.y   += Math.sin(this.dir) * this.i;
133        this.dir += (this.delta / this.i);
134        this.i   += (this.delta / this.i);
135    };
136
137    return that;
138};
139
140var LeftCurveBullet = function(size, color, x, y, dir, speed) {
141    var that = new CurveBullet(size, color, x, y, dir, speed);
142
143    that.getSign = function() {
144        return 1;
145    };
146
147    return that;
148};
149
150var RightCurveBullet = function(size, color, x, y, dir, speed) {
151    var that = new CurveBullet(size, color, x, y, dir, speed);
152
153    that.getSign = function() {
154        return -1;
155    };
156
157    return that;
158};
159
160
161var EnemyBullets = [LinerBullet,
162                    XYExtendBullet,
163                    XExtendBullet,
164                    YExtendBullet,
165                    LeftCurveBullet,
166                    RightCurveBullet];
Note: See TracBrowser for help on using the repository browser.