[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 | const ACTION_UP = 1; |
---|
| 17 | const ACTION_DOWN = 2; |
---|
| 18 | const ACTION_LEFT = 4; |
---|
| 19 | const ACTION_RIGHT = 8; |
---|
| 20 | const ACTION_SHOT = 16; |
---|
| 21 | const ACTION_DELAY = 32; |
---|
| 22 | const SWITCH_BULLET = 1; |
---|
| 23 | var currentAction = 0; |
---|
| 24 | var currentSwitch = 0; |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | /* |
---|
| 28 | * Functions |
---|
| 29 | */ |
---|
| 30 | function getActionByKeyCode(keyCode) { |
---|
| 31 | switch(keyCode) { |
---|
| 32 | case 37: // left |
---|
| 33 | case 72: // h |
---|
| 34 | return ACTION_LEFT; |
---|
| 35 | |
---|
| 36 | case 40: // down |
---|
| 37 | case 74: // j |
---|
| 38 | return ACTION_DOWN; |
---|
| 39 | |
---|
| 40 | case 38: // up |
---|
| 41 | case 75: // k |
---|
| 42 | return ACTION_UP; |
---|
| 43 | |
---|
| 44 | case 39: // right |
---|
| 45 | case 76: // l |
---|
| 46 | return ACTION_RIGHT; |
---|
| 47 | |
---|
| 48 | case 88: // x |
---|
| 49 | return ACTION_DELAY; |
---|
| 50 | |
---|
| 51 | case 90: // z |
---|
| 52 | return ACTION_SHOT; |
---|
| 53 | } |
---|
| 54 | return 0; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | function getSwitchByCharCode(charCode) { |
---|
| 58 | switch(charCode) { |
---|
| 59 | case 99: // c |
---|
| 60 | return SWITCH_BULLET; |
---|
| 61 | } |
---|
| 62 | return 0; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | function setKeyDown(keyCode) { |
---|
| 66 | currentAction |= getActionByKeyCode(keyCode); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function setKeyUp(keyCode) { |
---|
| 70 | currentAction &= (0xffff - getActionByKeyCode(keyCode)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | function isInAction(act) { |
---|
| 74 | return currentAction & act; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | function setKeyPress(charCode) { |
---|
| 78 | currentSwitch |= getSwitchByCharCode(charCode); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | function unsetSwitch(sw) { |
---|
| 82 | currentSwitch &= (0xffff - sw); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | function isSetSwitch(sw) { |
---|
| 86 | return currentSwitch & sw; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | /* |
---|
| 91 | * Shot |
---|
| 92 | * - update / switch barrages |
---|
| 93 | */ |
---|
| 94 | var Shot = function() { |
---|
| 95 | this.shot = function() { |
---|
| 96 | }; |
---|
| 97 | }; |
---|
| 98 | |
---|
| 99 | var ManualShot = function() { |
---|
| 100 | var that = new Shot(); |
---|
| 101 | |
---|
| 102 | that.index = 0; |
---|
| 103 | that.shot = function(trooper, enemy) { |
---|
| 104 | if (isSetSwitch(SWITCH_BULLET)) { |
---|
| 105 | if (++this.index >= trooper.barrages.length) |
---|
| 106 | this.index = 0; |
---|
| 107 | unsetSwitch(SWITCH_BULLET); |
---|
| 108 | } |
---|
| 109 | if (isInAction(ACTION_SHOT)) |
---|
| 110 | trooper.barrages[this.index].update(trooper, enemy); |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | return that; |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | var BlazeRandomShot = function() { |
---|
| 117 | var that = new Shot(); |
---|
| 118 | |
---|
| 119 | that.index = 0; |
---|
| 120 | that.shot = function(trooper, enemy) { |
---|
| 121 | if (trooper.barrages[this.index].update(trooper, enemy)) |
---|
| 122 | this.index = Math.floor(Math.random() * trooper.barrages.length); |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | return that; |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | var BlazeRotateShot = function(count) { |
---|
| 129 | var that = new BlazeRandomShot(); |
---|
| 130 | |
---|
| 131 | if (!count) |
---|
| 132 | count = 0; |
---|
| 133 | |
---|
| 134 | that.i = 0; |
---|
| 135 | that.shot = function(trooper, enemy) { |
---|
| 136 | if (trooper.barrages[this.index].update(trooper, enemy)) { |
---|
| 137 | if (++this.i > count) { |
---|
| 138 | this.index++; |
---|
| 139 | this.i = 0; |
---|
| 140 | } |
---|
| 141 | if (this.index >= trooper.barrages.length) |
---|
| 142 | this.index = 0; |
---|
| 143 | } |
---|
| 144 | }; |
---|
| 145 | |
---|
| 146 | return that; |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | var BlazeRotateShot5 = function() { |
---|
| 150 | return new BlazeRotateShot(5); |
---|
| 151 | }; |
---|
| 152 | |
---|
| 153 | var BlazeRotateShot10 = function() { |
---|
| 154 | return new BlazeRotateShot(10); |
---|
| 155 | }; |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | /* |
---|
| 159 | * Action |
---|
| 160 | * - call shot / move trooper |
---|
| 161 | */ |
---|
| 162 | var Action = function(shot) { |
---|
| 163 | this.shot = shot; |
---|
| 164 | this.update = function(trooper, enemy) { |
---|
| 165 | if (trooper.isDead()) |
---|
| 166 | return; |
---|
| 167 | this.shot.shot(trooper, enemy); |
---|
| 168 | return this.move(trooper, enemy); |
---|
| 169 | }; |
---|
| 170 | this.move = function(trooper, enemy) { |
---|
| 171 | return true; |
---|
| 172 | }; |
---|
| 173 | }; |
---|
| 174 | |
---|
| 175 | var ManualAction = function(shot) { |
---|
| 176 | var that = new Action(shot); |
---|
| 177 | |
---|
| 178 | that.move = function(trooper, enemy) { |
---|
| 179 | var x = 0; |
---|
| 180 | var y = 0; |
---|
| 181 | var s = trooper.speed; |
---|
| 182 | |
---|
| 183 | if (isInAction(ACTION_DELAY)) |
---|
| 184 | s /= 2; |
---|
| 185 | if (isInAction(ACTION_RIGHT)) |
---|
| 186 | x += s; |
---|
| 187 | if (isInAction(ACTION_LEFT)) |
---|
| 188 | x -= s; |
---|
| 189 | if (isInAction(ACTION_UP)) |
---|
| 190 | y -= s; |
---|
| 191 | if (isInAction(ACTION_DOWN)) |
---|
| 192 | y += s; |
---|
| 193 | |
---|
| 194 | trooper.move(x, y); |
---|
| 195 | |
---|
| 196 | return true; |
---|
| 197 | }; |
---|
| 198 | |
---|
| 199 | return that; |
---|
| 200 | }; |
---|
| 201 | |
---|
| 202 | var BezierAction = function(shot, resolution) { |
---|
| 203 | that = new Action(shot); |
---|
| 204 | |
---|
| 205 | if (!resolution) |
---|
| 206 | resolution = 1500; |
---|
| 207 | |
---|
| 208 | that.count = 0; |
---|
| 209 | that.resolution = 0; |
---|
| 210 | |
---|
| 211 | that.move = function(trooper, enemy) { |
---|
| 212 | if (this.count >= this.resolution) { |
---|
| 213 | this.count = 0; |
---|
| 214 | this.resolution = resolution / trooper.speed; |
---|
| 215 | |
---|
| 216 | this.p0 = this.getPoint0(trooper, enemy); |
---|
| 217 | this.p1 = this.getPoint1(trooper, enemy); |
---|
| 218 | this.p2 = this.getPoint2(trooper, enemy); |
---|
| 219 | this.p3 = this.getPoint3(trooper, enemy); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | var t = this.count++ / this.resolution; |
---|
| 223 | var t1 = 1 - t; |
---|
| 224 | var p0 = Math.pow(t1, 3); |
---|
| 225 | var p1 = 3 * t * Math.pow(t1, 2); |
---|
| 226 | var p2 = 3 * Math.pow(t, 2) * t1; |
---|
| 227 | var p3 = Math.pow(t, 3); |
---|
| 228 | |
---|
| 229 | trooper.moveAbs( |
---|
| 230 | this.p0[0] * p0 + this.p1[0] * p1 + |
---|
| 231 | this.p2[0] * p2 + this.p3[0] * p3, |
---|
| 232 | this.p0[1] * p0 + this.p1[1] * p1 + |
---|
| 233 | this.p2[1] * p2 + this.p3[1] * p3 |
---|
| 234 | ); |
---|
| 235 | |
---|
| 236 | if (this.count >= this.resolution) |
---|
| 237 | return true; |
---|
| 238 | |
---|
| 239 | return false; |
---|
| 240 | }; |
---|
| 241 | |
---|
| 242 | return that; |
---|
| 243 | }; |
---|
| 244 | |
---|
| 245 | var TriangleAction = function(shot, resolution) { |
---|
| 246 | var that = new BezierAction(shot, resolution); |
---|
| 247 | |
---|
| 248 | that.getPoint0 = function(trooper, enemy) { |
---|
| 249 | return [trooper.x, trooper.y]; |
---|
| 250 | }; |
---|
| 251 | that.getPoint1 = function(trooper, enemy) { |
---|
| 252 | return [trooper.x - (trooper.speed * 10), |
---|
| 253 | trooper.y + (trooper.speed * 10)]; |
---|
| 254 | }; |
---|
| 255 | that.getPoint2 = function(trooper, enemy) { |
---|
| 256 | return [trooper.x + (trooper.speed * 10), |
---|
| 257 | trooper.y + (trooper.speed * 10)]; |
---|
| 258 | }; |
---|
| 259 | that.getPoint3 = function(trooper, enemy) { |
---|
| 260 | return [trooper.x, trooper.y]; |
---|
| 261 | }; |
---|
| 262 | |
---|
| 263 | return that; |
---|
| 264 | }; |
---|
| 265 | |
---|
| 266 | var RandomAction = function(shot, resolution) { |
---|
| 267 | var that = new BezierAction(shot, resolution); |
---|
| 268 | |
---|
| 269 | that.getPoint0 = function(trooper, enemy) { |
---|
| 270 | return [trooper.x, trooper.y]; |
---|
| 271 | }; |
---|
| 272 | that.getPoint1 = function(trooper, enemy) { |
---|
| 273 | return [Math.floor(Math.random() * trooper.w), |
---|
| 274 | Math.floor(Math.random() * trooper.h)]; |
---|
| 275 | }; |
---|
| 276 | that.getPoint2 = function(trooper, enemy) { |
---|
| 277 | return [Math.floor(Math.random() * trooper.w), |
---|
| 278 | Math.floor(Math.random() * trooper.h)]; |
---|
| 279 | }; |
---|
| 280 | that.getPoint3 = function(trooper, enemy) { |
---|
| 281 | return [Math.floor(Math.random() * trooper.w), |
---|
| 282 | Math.floor(Math.random() * trooper.h)]; |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | return that; |
---|
| 286 | }; |
---|
| 287 | |
---|
| 288 | var RandomReturnAction = function(shot, resolution) { |
---|
| 289 | var that = new RandomAction(shot, resolution); |
---|
| 290 | |
---|
| 291 | that.getPoint3 = function(trooper, enemy) { |
---|
| 292 | return [Math.floor(Math.random() * trooper.w), trooper.y]; |
---|
| 293 | }; |
---|
| 294 | |
---|
| 295 | return that; |
---|
| 296 | }; |
---|
| 297 | |
---|
| 298 | var RandomStokingAction = function(shot, resolution) { |
---|
| 299 | var that = new RandomAction(shot, resolution); |
---|
| 300 | |
---|
| 301 | that.getPoint3 = function(trooper, enemy) { |
---|
| 302 | return [enemy.x, Math.floor(Math.random() * trooper.h)]; |
---|
| 303 | }; |
---|
| 304 | |
---|
| 305 | return that; |
---|
| 306 | }; |
---|
| 307 | |
---|
| 308 | var RandomStokingReturnAction = function(shot, resolution) { |
---|
| 309 | var that = new RandomAction(shot, resolution); |
---|
| 310 | |
---|
| 311 | that.getPoint3 = function(trooper, enemy) { |
---|
| 312 | return [enemy.x, trooper.y]; |
---|
| 313 | }; |
---|
| 314 | |
---|
| 315 | return that; |
---|
| 316 | }; |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | /* |
---|
| 320 | * ActionList |
---|
| 321 | * - call / switch actions |
---|
| 322 | */ |
---|
| 323 | var ActionList = function(list) { |
---|
| 324 | this.list = list; |
---|
| 325 | this.index = 0; |
---|
| 326 | this.update = function(trooper, enemy) { |
---|
| 327 | this.updateIdx(this.list[this.index].update(trooper, enemy), |
---|
| 328 | trooper, enemy); |
---|
| 329 | }; |
---|
| 330 | this.updateIdx = function(end, trooper, enemy) { |
---|
| 331 | this.index = 0; |
---|
| 332 | }; |
---|
| 333 | }; |
---|
| 334 | |
---|
| 335 | var RandomActionList = function(list) { |
---|
| 336 | var that = new ActionList(list); |
---|
| 337 | |
---|
| 338 | that.updateIdx = function(end, trooper, enemy) { |
---|
| 339 | if (end) |
---|
| 340 | this.index = Math.floor(Math.random() * this.list.length); |
---|
| 341 | }; |
---|
| 342 | |
---|
| 343 | return that; |
---|
| 344 | }; |
---|
| 345 | |
---|
| 346 | var RotateActionList = function(list, count) { |
---|
| 347 | var that = new ActionList(list); |
---|
| 348 | |
---|
| 349 | if (!count) |
---|
| 350 | count = 0; |
---|
| 351 | |
---|
| 352 | that.i = 0; |
---|
| 353 | that.updateIdx = function(end, trooper, enemy) { |
---|
| 354 | if (end) { |
---|
| 355 | if (++this.i > count) { |
---|
| 356 | this.index++; |
---|
| 357 | this.i = 0; |
---|
| 358 | } |
---|
| 359 | if (this.index >= this.list.length) |
---|
| 360 | this.index = 0; |
---|
| 361 | } |
---|
| 362 | }; |
---|
| 363 | |
---|
| 364 | return that; |
---|
| 365 | }; |
---|
| 366 | |
---|
| 367 | var RotateActionList5 = function(list) { |
---|
| 368 | return new RotateActionList(list, 5); |
---|
| 369 | }; |
---|
| 370 | |
---|
| 371 | var RotateActionList10 = function(list) { |
---|
| 372 | return new RotateActionList(list, 10); |
---|
| 373 | }; |
---|
| 374 | |
---|
| 375 | var DamageRandomActionList = function(list) { |
---|
| 376 | var that = new ActionList(list); |
---|
| 377 | |
---|
| 378 | that.life = 0; |
---|
| 379 | that.updateIdx = function(end, trooper, enemy) { |
---|
| 380 | if (!this.life) |
---|
| 381 | this.life = trooper.life; |
---|
| 382 | |
---|
| 383 | if (end && trooper.life != this.life) { |
---|
| 384 | this.index = Math.floor(Math.random() * this.list.length); |
---|
| 385 | this.life = trooper.life; |
---|
| 386 | } |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | return that; |
---|
| 390 | }; |
---|
| 391 | |
---|
| 392 | var DamageRotateActionList = function(list) { |
---|
| 393 | var that = new ActionList(list); |
---|
| 394 | |
---|
| 395 | that.life = 0; |
---|
| 396 | that.updateIdx = function(end, trooper, enemy) { |
---|
| 397 | if (!this.life) |
---|
| 398 | this.life = trooper.life; |
---|
| 399 | |
---|
| 400 | if (end && trooper.life != this.life) { |
---|
| 401 | if (++this.index >= this.list.length) |
---|
| 402 | this.index = 0; |
---|
| 403 | |
---|
| 404 | this.life = trooper.life; |
---|
| 405 | } |
---|
| 406 | }; |
---|
| 407 | |
---|
| 408 | return that; |
---|
| 409 | }; |
---|
| 410 | |
---|
| 411 | |
---|
| 412 | var EnemyShots = [BlazeRandomShot, BlazeRotateShot, |
---|
| 413 | BlazeRotateShot5, BlazeRotateShot10]; |
---|
| 414 | var EnemyActions = [TriangleAction, RandomAction, |
---|
| 415 | RandomReturnAction, RandomStokingAction, |
---|
| 416 | RandomStokingReturnAction]; |
---|
| 417 | var EnemyActionLists = [RandomActionList, RotateActionList, |
---|
| 418 | RotateActionList5, RotateActionList10, |
---|
| 419 | DamageRandomActionList, DamageRotateActionList]; |
---|