[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 | |
---|
| 16 | var Barrage = function(bullet, size, color, interval, speed, way, dir) { |
---|
| 17 | this.bullet = bullet; |
---|
| 18 | this.size = size; |
---|
| 19 | this.color = color; |
---|
| 20 | this.interval = interval; |
---|
| 21 | this.speed = speed; |
---|
| 22 | this.way = way; |
---|
| 23 | this.dir = dir; |
---|
| 24 | |
---|
| 25 | this.aim = function(trooper, enemy) { |
---|
| 26 | var dx = enemy.x - trooper.x; |
---|
| 27 | var dy = enemy.y - trooper.y; |
---|
| 28 | return Math.atan2(dy, dx); |
---|
| 29 | }; |
---|
| 30 | this.angle = function(trooper, enemy) { |
---|
| 31 | if (this.dir != null) |
---|
| 32 | return this.dir; |
---|
| 33 | return this.aim(trooper, enemy) / Math.PI; |
---|
| 34 | }; |
---|
| 35 | this.update = function(trooper, enemy) { |
---|
| 36 | return false; |
---|
| 37 | }; |
---|
| 38 | }; |
---|
| 39 | |
---|
| 40 | var LinerBarrage = function(bullet, size, color, interval, speed, way, dir) { |
---|
| 41 | var that = new Barrage(bullet, size, color, interval, speed, way, dir); |
---|
| 42 | |
---|
| 43 | that.state = 0; |
---|
| 44 | that.update = function(trooper, enemy) { |
---|
| 45 | if (++this.state < this.interval) |
---|
| 46 | return false; |
---|
| 47 | |
---|
| 48 | var dir = this.angle(trooper, enemy); |
---|
| 49 | |
---|
| 50 | trooper.addBullet(this.bullet, this.size, this.color, |
---|
| 51 | 0, 0, dir * Math.PI, this.speed); |
---|
| 52 | |
---|
| 53 | for (i = 2; i < this.way + 1; i++) { |
---|
| 54 | var d = 0.5 / i; |
---|
| 55 | |
---|
| 56 | trooper.addBullet(this.bullet, this.size, this.color, |
---|
| 57 | 0, 0, (dir - d) * Math.PI, this.speed); |
---|
| 58 | trooper.addBullet(this.bullet, this.size, this.color, |
---|
| 59 | 0, 0, (dir + d) * Math.PI, this.speed); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | this.state = 0; |
---|
| 63 | return true; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | return that; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | var ArchBarrage = function(bullet, size, color, |
---|
| 70 | interval, speed, way, dir, delta) { |
---|
| 71 | |
---|
| 72 | var that = new Barrage(bullet, size, color, interval, speed, way, dir); |
---|
| 73 | |
---|
| 74 | that.delta = delta; |
---|
| 75 | |
---|
| 76 | if (that.way % 2) |
---|
| 77 | that.way++; |
---|
| 78 | |
---|
| 79 | if (that.delta == null) |
---|
| 80 | that.delta = 0.5 / that.way; |
---|
| 81 | |
---|
| 82 | that.state = 0; |
---|
| 83 | that.update = function(trooper, enemy) { |
---|
| 84 | if (++this.state < this.interval) |
---|
| 85 | return false; |
---|
| 86 | |
---|
| 87 | var angle = this.angle(trooper, enemy) - (this.delta * this.way / 2); |
---|
| 88 | |
---|
| 89 | for (i = 0; i < this.way; i++) { |
---|
| 90 | trooper.addBullet(this.bullet, this.size, this.color, |
---|
| 91 | 0, 0, angle * Math.PI, this.speed); |
---|
| 92 | angle += this.delta; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | this.state = 0; |
---|
| 96 | return true; |
---|
| 97 | }; |
---|
| 98 | |
---|
| 99 | return that; |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | var CircularBarrage = function(bullet, size, color, |
---|
| 103 | interval, speed, way, dir) { |
---|
| 104 | |
---|
| 105 | var that = new ArchBarrage(bullet, size, color, |
---|
| 106 | interval, speed, way, dir, 2 / way); |
---|
| 107 | return that; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | var DelayedArchBarrage = function(bullet, size, color, |
---|
| 111 | interval, speed, way, dir, delta) { |
---|
| 112 | |
---|
| 113 | var that = new Barrage(bullet, size, color, interval, speed, way, dir); |
---|
| 114 | |
---|
| 115 | that.delta = delta; |
---|
| 116 | |
---|
| 117 | if (that.way % 2) |
---|
| 118 | that.way++; |
---|
| 119 | |
---|
| 120 | if (that.delta == null) |
---|
| 121 | that.delta = 0.5 / that.way; |
---|
| 122 | |
---|
| 123 | that.state = 0; |
---|
| 124 | that.remain = that.way; |
---|
| 125 | that.curAngle = 0; |
---|
| 126 | |
---|
| 127 | that.update = function(trooper, enemy) { |
---|
| 128 | if (++this.state < this.interval) |
---|
| 129 | return false; |
---|
| 130 | |
---|
| 131 | if (!this.curAngle) |
---|
| 132 | this.curAngle = this.angle(trooper, enemy) - |
---|
| 133 | this.delta * this.way / 2; |
---|
| 134 | |
---|
| 135 | trooper.addBullet(this.bullet, this.size, this.color, |
---|
| 136 | 0, 0, this.curAngle * Math.PI, this.speed); |
---|
| 137 | |
---|
| 138 | this.curAngle += this.delta; |
---|
| 139 | |
---|
| 140 | if (--this.remain < 0) { |
---|
| 141 | return this.reset(); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | return false; |
---|
| 145 | }; |
---|
| 146 | that.reset = function() { |
---|
| 147 | this.state = 0; |
---|
| 148 | this.remain = this.way; |
---|
| 149 | this.curAngle = 0; |
---|
| 150 | return true; |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | return that; |
---|
| 154 | }; |
---|
| 155 | |
---|
| 156 | var DelayedCircularBarrage = function(bullet, size, color, |
---|
| 157 | interval, speed, way, dir) { |
---|
| 158 | |
---|
| 159 | var that = new DelayedArchBarrage(bullet, size, color, |
---|
| 160 | interval, speed, way, dir, 2 / way); |
---|
| 161 | return that; |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | var DelayedRecursiveArchBarrage = function(bullet, size, color, |
---|
| 165 | interval, speed, way, dir, cnt) { |
---|
| 166 | |
---|
| 167 | var that = new DelayedArchBarrage(bullet, size, color, |
---|
| 168 | interval, speed, way, dir); |
---|
| 169 | |
---|
| 170 | that.i = 0; |
---|
| 171 | that.cnt = cnt; |
---|
| 172 | |
---|
| 173 | if (that.cnt == null) |
---|
| 174 | that.cnt = that.way / that.speed; |
---|
| 175 | |
---|
| 176 | that.reset = function() { |
---|
| 177 | if (++this.i < this.cnt) { |
---|
| 178 | this.remain = this.way; |
---|
| 179 | this.delta *= -1; |
---|
| 180 | return false; |
---|
| 181 | } |
---|
| 182 | this.state = 0; |
---|
| 183 | this.remain = this.way; |
---|
| 184 | this.curAngle = 0; |
---|
| 185 | this.i = 0; |
---|
| 186 | return true; |
---|
| 187 | }; |
---|
| 188 | return that; |
---|
| 189 | }; |
---|
| 190 | |
---|
| 191 | var DelayedRecursiveCircularBarrage = function(bullet, size, color, |
---|
| 192 | interval, speed, way, dir, cnt) { |
---|
| 193 | |
---|
| 194 | var that = new DelayedCircularBarrage(bullet, size, color, |
---|
| 195 | interval, speed, way, dir); |
---|
| 196 | |
---|
| 197 | that.i = 0; |
---|
| 198 | that.cnt = cnt; |
---|
| 199 | |
---|
| 200 | if (that.cnt == null) |
---|
| 201 | that.cnt = that.way / that.speed; |
---|
| 202 | |
---|
| 203 | that.reset = function() { |
---|
| 204 | if (++this.i < this.cnt) { |
---|
| 205 | this.remain = this.way; |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | this.state = 0; |
---|
| 209 | this.remain = this.way; |
---|
| 210 | this.curAngle = 0; |
---|
| 211 | this.i = 0; |
---|
| 212 | return true; |
---|
| 213 | }; |
---|
| 214 | return that; |
---|
| 215 | }; |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | var EnemyBarrages = [LinerBarrage, |
---|
| 219 | ArchBarrage, |
---|
| 220 | CircularBarrage, |
---|
| 221 | DelayedArchBarrage, |
---|
| 222 | DelayedCircularBarrage, |
---|
| 223 | DelayedRecursiveArchBarrage, |
---|
| 224 | DelayedRecursiveCircularBarrage]; |
---|