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