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