[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 | System = { |
---|
| 17 | "screen": { |
---|
| 18 | "canvas": null, |
---|
| 19 | "ctx": null, |
---|
| 20 | "width": 0, |
---|
| 21 | "height": 0 |
---|
| 22 | }, |
---|
[84] | 23 | "message": null, |
---|
[81] | 24 | "boss": null, |
---|
| 25 | "player": null, |
---|
| 26 | "backgroundObject": new Array(), |
---|
[95] | 27 | "deathPieces": new Array(), |
---|
| 28 | "gameOver": false, |
---|
[81] | 29 | "mainIntervalId": 0 |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /* |
---|
[95] | 34 | * Tiun Tiun |
---|
| 35 | */ |
---|
| 36 | var DeathPiece = function(sizes, colors, x, y, dir, speed) { |
---|
| 37 | var that = new LinerBullet(sizes[0], colors[0], x, y, dir, speed); |
---|
| 38 | |
---|
| 39 | var sizeIdx = -1; |
---|
| 40 | var colorIdx = -1; |
---|
| 41 | |
---|
| 42 | that.getSize = function() { |
---|
| 43 | if (++sizeIdx >= sizes.length) |
---|
| 44 | sizeIdx = 0; |
---|
| 45 | return sizes[sizeIdx]; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | that.getColor = function() { |
---|
| 49 | if (++colorIdx >= colors.length) |
---|
| 50 | colorIdx = 0; |
---|
| 51 | return colors[colorIdx]; |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | return that; |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /* |
---|
[81] | 59 | * Utility Functions |
---|
| 60 | */ |
---|
| 61 | function updateBackground(ctx, width, height, size, color, max) { |
---|
| 62 | if (System.backgroundObject.length < max) { |
---|
| 63 | var x = Math.ceil(Math.random() * width); |
---|
| 64 | var s = Math.ceil(Math.random() * 5); |
---|
| 65 | System.backgroundObject.push( |
---|
| 66 | new LinerBullet(size, color, x, 0, 0.5 * Math.PI, s)); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | var newObjs = new Array(); |
---|
[96] | 70 | for (var i = 0; i < System.backgroundObject.length; i++) { |
---|
[81] | 71 | System.backgroundObject[i].next(); |
---|
| 72 | System.backgroundObject[i].draw(ctx); |
---|
| 73 | if (!System.backgroundObject[i].vanished(width, height)) |
---|
| 74 | newObjs.push(System.backgroundObject[i]); |
---|
| 75 | } |
---|
| 76 | System.backgroundObject = newObjs; |
---|
| 77 | } |
---|
| 78 | |
---|
[95] | 79 | function updateDeathPieces(ctx, width, height, x, y, sizes, colors, speed, way) { |
---|
| 80 | if (System.deathPieces.length <= 0) { |
---|
| 81 | if (way % 2) |
---|
| 82 | way++; |
---|
| 83 | |
---|
| 84 | var angle = 0; |
---|
| 85 | var delta = 2 / way; |
---|
| 86 | |
---|
| 87 | for(i = 0; i < way; i++) { |
---|
| 88 | System.deathPieces.push( |
---|
| 89 | new DeathPiece(sizes, colors, x, y, angle * Math.PI, speed)); |
---|
| 90 | angle += delta; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | var newObjs = new Array(); |
---|
[96] | 95 | for (var i = 0; i < System.deathPieces.length; i++) { |
---|
[95] | 96 | System.deathPieces[i].next(); |
---|
| 97 | System.deathPieces[i].draw(ctx); |
---|
| 98 | if (!System.deathPieces[i].vanished(width, height)) |
---|
| 99 | newObjs.push(System.deathPieces[i]); |
---|
| 100 | } |
---|
| 101 | System.deathPieces = newObjs; |
---|
| 102 | |
---|
| 103 | return !System.deathPieces.length; |
---|
| 104 | } |
---|
| 105 | |
---|
[81] | 106 | function drawScreen(ctx, op, style, width, height) { |
---|
| 107 | var c = ctx.globalCompositeOperation; |
---|
| 108 | ctx.globalCompositeOperation = op; |
---|
| 109 | ctx.beginPath(); |
---|
| 110 | ctx.fillStyle = style; |
---|
| 111 | ctx.fillRect(0, 0, width, height); |
---|
| 112 | ctx.fill(); |
---|
| 113 | ctx.closePath(); |
---|
| 114 | ctx.globalCompositeOperation = c; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | function drawLifeGauge(ctx, op, trooper, x, y, width) { |
---|
| 118 | var length = trooper.life; |
---|
| 119 | |
---|
| 120 | if (length > width - 20) |
---|
| 121 | length = width - 20; |
---|
| 122 | |
---|
| 123 | var c = ctx.globalCompositeOperation; |
---|
| 124 | ctx.globalCompositeOperation = op; |
---|
| 125 | ctx.beginPath(); |
---|
| 126 | ctx.fillStyle = trooper.color; |
---|
| 127 | ctx.fillRect(x, y, length, 10); |
---|
| 128 | ctx.fill(); |
---|
| 129 | ctx.closePath(); |
---|
| 130 | ctx.globalCompositeOperation = c; |
---|
| 131 | |
---|
| 132 | drawString(ctx, op, trooper.life, x + 2, y + 8, trooper.color, |
---|
| 133 | "6pt monospace", "left"); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | function drawString(ctx, op, string, x, y, color, font, align) { |
---|
| 137 | var a = ctx.textAlign; |
---|
| 138 | var f = ctx.font; |
---|
| 139 | var c = ctx.globalCompositeOperation; |
---|
| 140 | ctx.globalCompositeOperation = op; |
---|
| 141 | ctx.beginPath(); |
---|
| 142 | ctx.textAlign = align; |
---|
| 143 | ctx.fillStyle = color; |
---|
| 144 | ctx.font = font; |
---|
| 145 | ctx.fillText(string, x, y); |
---|
| 146 | ctx.fill(); |
---|
| 147 | ctx.textAlign = a; |
---|
| 148 | ctx.font = f; |
---|
| 149 | ctx.closePath(); |
---|
| 150 | ctx.globalCompositeOperation = c; |
---|
| 151 | } |
---|
| 152 | |
---|
[95] | 153 | function updateTrooper(trooper, enemy) { |
---|
| 154 | if (System.gameOver) { |
---|
| 155 | trooper.clearBullet(); |
---|
| 156 | enemy.clearBullet(); |
---|
| 157 | |
---|
| 158 | drawString( |
---|
| 159 | System.screen.ctx, "source-over", |
---|
| 160 | "GAME OVER", |
---|
| 161 | System.screen.width / 2, |
---|
| 162 | System.screen.height / 2, |
---|
| 163 | "#ACF", "24pt monospace", "center" |
---|
| 164 | ); |
---|
| 165 | } |
---|
| 166 | else if (trooper.isDead()) { |
---|
| 167 | trooper.clearBullet(); |
---|
| 168 | enemy.clearBullet(); |
---|
| 169 | |
---|
| 170 | System.gameOver = updateDeathPieces( |
---|
| 171 | System.screen.ctx, |
---|
| 172 | System.screen.width, |
---|
| 173 | System.screen.height, |
---|
| 174 | trooper.x, |
---|
| 175 | trooper.y, |
---|
| 176 | [6, 8, 10], ["#55F", "#AAF"], 3, 8 |
---|
| 177 | ); |
---|
| 178 | } |
---|
| 179 | else if (enemy.isDead()) { |
---|
| 180 | trooper.draw(System.screen.ctx); |
---|
| 181 | } |
---|
| 182 | else { |
---|
| 183 | trooper.update(enemy); |
---|
| 184 | trooper.draw(System.screen.ctx); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[84] | 188 | function setMessage(elem, msg) { |
---|
| 189 | if (elem) |
---|
| 190 | elem.innerHTML = msg; |
---|
| 191 | } |
---|
[81] | 192 | |
---|
[84] | 193 | function addMessage(elem, msg) { |
---|
| 194 | if (elem) |
---|
| 195 | elem.innerHTML += msg; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
[81] | 199 | /* |
---|
| 200 | * Main loop |
---|
| 201 | */ |
---|
| 202 | function mainLoop() { |
---|
| 203 | // clear screen |
---|
| 204 | drawScreen( |
---|
| 205 | System.screen.ctx, |
---|
| 206 | "source-over", |
---|
| 207 | "rgba(8,8,8,0.5)", |
---|
| 208 | System.screen.width, |
---|
| 209 | System.screen.height |
---|
| 210 | ); |
---|
| 211 | |
---|
| 212 | // update background objects |
---|
| 213 | updateBackground( |
---|
| 214 | System.screen.ctx, |
---|
| 215 | System.screen.width, |
---|
| 216 | System.screen.height, |
---|
| 217 | 1, "#CAF", 10 |
---|
| 218 | ); |
---|
| 219 | |
---|
[95] | 220 | // update/draw troopers |
---|
| 221 | updateTrooper(System.player, System.boss); |
---|
| 222 | updateTrooper(System.boss, System.player); |
---|
[81] | 223 | |
---|
| 224 | // draw player name/life |
---|
| 225 | drawString( |
---|
| 226 | System.screen.ctx, |
---|
| 227 | "source-over", |
---|
| 228 | System.player.name, |
---|
| 229 | 10, System.screen.height - 25, |
---|
| 230 | "#ACF", "9pt monospace", "left" |
---|
| 231 | ); |
---|
| 232 | drawLifeGauge( |
---|
| 233 | System.screen.ctx, |
---|
| 234 | "lighter", |
---|
| 235 | System.player, |
---|
| 236 | 10, |
---|
| 237 | System.screen.height - 20, |
---|
| 238 | System.screen.width |
---|
| 239 | ); |
---|
| 240 | |
---|
| 241 | // draw boss name/life |
---|
| 242 | drawString( |
---|
| 243 | System.screen.ctx, |
---|
| 244 | "source-over", |
---|
| 245 | System.boss.name, 10, 35, |
---|
| 246 | "#FCA", "9pt monospace", "left" |
---|
| 247 | ); |
---|
| 248 | drawLifeGauge( |
---|
| 249 | System.screen.ctx, |
---|
| 250 | "lighter", |
---|
| 251 | System.boss, |
---|
| 252 | 10, |
---|
| 253 | 10, |
---|
| 254 | System.screen.width |
---|
| 255 | ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | /* |
---|
| 260 | * Initializer |
---|
| 261 | */ |
---|
[84] | 262 | function initGame(canvas, msg, bossId, bossData, playerData) { |
---|
[81] | 263 | System.screen.canvas = canvas; |
---|
[84] | 264 | System.message = msg; |
---|
[81] | 265 | System.screen.ctx = System.screen.canvas.getContext("2d"); |
---|
| 266 | System.screen.width = System.screen.canvas.width; |
---|
| 267 | System.screen.height = System.screen.canvas.height; |
---|
[95] | 268 | System.gameOver = false; |
---|
[81] | 269 | |
---|
| 270 | System.screen.ctx.globalCompositeOperation = "lighter"; |
---|
| 271 | |
---|
| 272 | if (System.mainIntervalId) { |
---|
| 273 | clearInterval(System.mainIntervalId); |
---|
| 274 | System.mainIntervalId = 0; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | drawScreen( |
---|
| 278 | System.screen.ctx, |
---|
| 279 | "source-over", |
---|
| 280 | "rgba(0,0,0,1)", |
---|
| 281 | System.screen.width, |
---|
| 282 | System.screen.height |
---|
| 283 | ); |
---|
| 284 | |
---|
| 285 | System.player = new Trooper( |
---|
| 286 | playerData.name, |
---|
| 287 | new ActionList([new ManualAction(new ManualShot())]), |
---|
| 288 | playerData.size, |
---|
[97] | 289 | playerData.hitsize, |
---|
[81] | 290 | "#33F", |
---|
[97] | 291 | "#F33", |
---|
[81] | 292 | System.screen.width / 2, |
---|
| 293 | System.screen.height - System.screen.height / 7, |
---|
| 294 | System.screen.width, |
---|
| 295 | System.screen.height, |
---|
| 296 | playerData.hitpoint, |
---|
| 297 | playerData.speed, |
---|
[92] | 298 | playerData.numbombs, |
---|
[81] | 299 | [new LinerBarrage(YExtendBullet, |
---|
| 300 | playerData.shotsize, |
---|
| 301 | "#3FF", |
---|
| 302 | playerData.shotinterval, |
---|
| 303 | playerData.shotspeed, |
---|
| 304 | playerData.shotlevel, |
---|
| 305 | -0.5), |
---|
| 306 | new LinerBarrage(YExtendBullet, |
---|
| 307 | playerData.shotsize, |
---|
| 308 | "#3FF", |
---|
| 309 | playerData.shotinterval, |
---|
| 310 | playerData.shotspeed, |
---|
| 311 | playerData.shotlevel, |
---|
| 312 | 0.5), |
---|
| 313 | new CircularBarrage(LinerBullet, |
---|
| 314 | playerData.shotsize, |
---|
| 315 | "#3FF", |
---|
| 316 | playerData.shotinterval, |
---|
| 317 | playerData.shotspeed, |
---|
| 318 | playerData.shotlevel + 2, |
---|
| 319 | -0.5)] |
---|
| 320 | ); |
---|
| 321 | |
---|
| 322 | var actList = EnemyActionLists[bossData.mtime % EnemyActionLists.length]; |
---|
| 323 | var shot = EnemyShots[bossData.hitpoint % EnemyShots.length]; |
---|
| 324 | var numAct = bossData.agility % (EnemyActions.length - 1) + 1; |
---|
| 325 | var numBlt = bossData.skills.length % (EnemyBullets.length - 1) + 1; |
---|
| 326 | var numBrrg = bossData.skills.length % (EnemyBarrages.length - 1) + 1; |
---|
| 327 | var acts = new Array(); |
---|
| 328 | var brrgs = new Array(); |
---|
| 329 | |
---|
[84] | 330 | setMessage(System.message, "ActionsIdx:"); |
---|
[81] | 331 | |
---|
[96] | 332 | for (var i = 0; i < numAct; i++) { |
---|
[81] | 333 | var idx = (bossData.agility + i) % EnemyActions.length; |
---|
| 334 | acts.push(new EnemyActions[idx](new shot())); |
---|
| 335 | |
---|
[96] | 336 | addMessage(System.message, String(idx) + "(" + |
---|
| 337 | String(bossData.hitpoint % EnemyShots.length) + "),"); |
---|
[81] | 338 | } |
---|
| 339 | |
---|
[84] | 340 | addMessage(System.message, "<br>"); |
---|
[81] | 341 | |
---|
[96] | 342 | for (var i = 0; i < numBrrg; i++) { |
---|
| 343 | var idx = (bossData.skillpoint + i) % EnemyBarrages.length; |
---|
[81] | 344 | var brrgCls = EnemyBarrages[idx]; |
---|
| 345 | |
---|
[84] | 346 | addMessage(System.message, |
---|
| 347 | "BarragesIdx:" + String(idx) + " / BulletsIdx:"); |
---|
[81] | 348 | |
---|
[96] | 349 | for (var k = 0; k < numBlt; k++) { |
---|
[81] | 350 | var iidx = (bossData.skills.length + i + k) % EnemyBullets.length; |
---|
[96] | 351 | var ss = Math.ceil(bossData.luck / 5); |
---|
[81] | 352 | brrgs.push( |
---|
| 353 | new brrgCls( |
---|
| 354 | EnemyBullets[iidx], |
---|
| 355 | ss < 3 ? 3 : ss, |
---|
| 356 | "#FF3", |
---|
| 357 | 200 * 1 / Math.log(bossData.skillpoint + 0.1), |
---|
| 358 | Math.log(bossData.strength / 15 + 0.1), |
---|
| 359 | Math.ceil(bossData.concentration / 5) |
---|
| 360 | ) |
---|
| 361 | ); |
---|
| 362 | |
---|
[84] | 363 | addMessage(System.message, String(iidx) + ","); |
---|
[81] | 364 | } |
---|
| 365 | |
---|
[84] | 366 | addMessage(System.message, "<br>"); |
---|
[81] | 367 | } |
---|
| 368 | |
---|
| 369 | System.boss = new Trooper( |
---|
| 370 | bossData.name, |
---|
| 371 | new actList(acts), |
---|
| 372 | Math.ceil(50 * (1 / bossData.defense)), |
---|
[97] | 373 | Math.ceil(50 * (1 / bossData.defense)), |
---|
[81] | 374 | "#F33", |
---|
[97] | 375 | "#F33", |
---|
[81] | 376 | System.screen.width / 2, |
---|
| 377 | System.screen.height / 7, |
---|
| 378 | System.screen.width, |
---|
| 379 | System.screen.height, |
---|
| 380 | bossData.hitpoint, |
---|
| 381 | Math.log(bossData.agility + 0.1) * 3, |
---|
[92] | 382 | 0, |
---|
[81] | 383 | brrgs |
---|
| 384 | ); |
---|
| 385 | |
---|
| 386 | System.backgroundObject = new Array(); |
---|
| 387 | System.mainIntervalId = setInterval(mainLoop, 20); |
---|
| 388 | } |
---|