HTMLファイルとコンパイルされたjsファイルを示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<!DOCTYPE html> <html> <head> <title>3Dの縦スクロールシューティングゲーム</title> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script> <style> #container { position:relative; height: 500px; } #HeaderCanvas { position: absolute; top: 0px; left: 0px; height:60px; } #MainCanvas { position: absolute; top: 60px; left: 0px; height:400px; } #FooterCanvas { position: absolute; top: 460px; left: 0px; height:40px; } </style> </head> <body> <div id = "container"> <canvas id="HeaderCanvas"></canvas> <canvas id="MainCanvas"></canvas> <canvas id="FooterCanvas"></canvas> <div> <script src="./data-url.js"></script> <script src="./sound.js"></script> <script src="./game-character.js"></script> <script src="./explosion.js"></script> <script src="./jiki.js"></script> <script src="./enemy.js"></script> <script src="./app.js"></script> </body> </html> |
data-url.js
ここを参照してください。
sound.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class PlaySoundeffect { static Explosion() { let audioElm = new Audio('./sound/explosion.mp3'); audioElm.play(); } static Damage() { let audioElm = new Audio('./sound/damage.mp3'); audioElm.play(); } static BigExplosion() { let audioElm = new Audio('./sound/big-explosion.mp3'); audioElm.play(); } } class PlayBattleBGM { static Start() { PlayBattleBGM.audioElm.currentTime = 0.5; PlayBattleBGM.audioElm.play(); } static Stop() { PlayBattleBGM.audioElm.pause(); } static Check() { if (PlayBattleBGM.audioElm.currentTime >= 21.4) { PlayBattleBGM.Start(); } } } PlayBattleBGM.audioElm = new Audio('./sound/battle.mp3'); class PlayBattleBossBGM { static Start() { PlayBattleBossBGM.audioElm.currentTime = 0; PlayBattleBossBGM.audioElm.play(); } static Restart() { PlayBattleBossBGM.audioElm.currentTime = 3.5; PlayBattleBossBGM.audioElm.play(); } static Stop() { PlayBattleBossBGM.audioElm.pause(); } static Check() { if (PlayBattleBossBGM.audioElm.currentTime >= 44.5) { PlayBattleBossBGM.Restart(); } } } PlayBattleBossBGM.audioElm = new Audio('./sound/boss.mp3'); |
game-character.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
class GameCharacter { constructor() { this.life = 1; this.size = 1; this.sprite = null; this.VX = 0; this.VY = 0; this.VZ = 0; } GetMaterial(dataURL) { let img = new Image(); img.src = dataURL; var texture = new THREE.Texture(img); texture.needsUpdate = true; return new THREE.SpriteMaterial({ map: texture }); } AddScene(scene) { scene.add(this.sprite); } RemoveScene(scene) { scene.remove(this.sprite); } get X() { return this.sprite.position.x; } get Y() { return this.sprite.position.y; } get Z() { return this.sprite.position.z; } set X(value) { this.sprite.position.x = value; } set Y(value) { this.sprite.position.y = value; } set Z(value) { this.sprite.position.z = value; } Move() { this.X += this.VX; this.Y += this.VY; this.Z += this.VZ; } } |
explosion.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
class Explosion extends GameCharacter { constructor(x, y, vx, vy, vz, size) { super(); this.moveCount = 0; if (Explosion.explosionMaterial0 == null) Explosion.explosionMaterial0 = this.GetMaterial(DataURL.dataURL_Explosion0); if (Explosion.explosionMaterial1 == null) Explosion.explosionMaterial1 = this.GetMaterial(DataURL.dataURL_Explosion1); if (Explosion.explosionMaterial2 == null) Explosion.explosionMaterial2 = this.GetMaterial(DataURL.dataURL_Explosion2); if (Explosion.explosionMaterial3 == null) Explosion.explosionMaterial3 = this.GetMaterial(DataURL.dataURL_Explosion3); if (Explosion.explosionMaterial4 == null) Explosion.explosionMaterial4 = this.GetMaterial(DataURL.dataURL_Explosion4); if (Explosion.explosionMaterial5 == null) Explosion.explosionMaterial5 = this.GetMaterial(DataURL.dataURL_Explosion5); this.sprite = new THREE.Sprite(Explosion.explosionMaterial0); if (typeof size !== 'undefined') this.sprite.scale.set(size, size, size); this.life = 1; this.X = x; this.Y = y; this.Z = 0; this.VX = vx; this.VY = vy; this.VZ = vz; } Move() { this.moveCount++; let i = this.moveCount % 60; if (i < 4) this.sprite.material = Explosion.explosionMaterial0; else if (i < 8) this.sprite.material = Explosion.explosionMaterial1; else if (i < 12) this.sprite.material = Explosion.explosionMaterial2; else if (i < 16) this.sprite.material = Explosion.explosionMaterial3; else if (i < 20) this.sprite.material = Explosion.explosionMaterial4; else if (i < 24) this.sprite.material = Explosion.explosionMaterial5; else this.life = 0; super.Move(); } } Explosion.explosionMaterial0 = null; Explosion.explosionMaterial1 = null; Explosion.explosionMaterial2 = null; Explosion.explosionMaterial3 = null; Explosion.explosionMaterial4 = null; Explosion.explosionMaterial5 = null; |
jiki.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
class Jiki { constructor() { this.LifeMax = 10; this.life = 0; this.size = 1.0; this.MoveLeft = false; this.MoveRight = false; this.MoveFront = false; this.MoveBack = false; let img = new Image(); img.src = DataURL.dataURL_Jiki; let texture = new THREE.Texture(img); texture.needsUpdate = true; const material = new THREE.MeshStandardMaterial({ map: texture, transparent: true }); let geometry = new THREE.PlaneGeometry(this.size, this.size); this.Mesh = new THREE.Mesh(geometry, material); this.life = this.LifeMax; } Move() { if (this.MoveLeft) { if (this.X > -4) { this.X += -0.1; this.Mesh.rotation.set(0, -Math.PI * 2 * 20 / 360, 0); return; } } if (this.MoveFront) { if (this.Y < 4) { this.Y += 0.1; } } if (this.MoveRight) { if (this.X < 4) { this.X += 0.1; this.Mesh.rotation.set(0, Math.PI * 2 * 20 / 360, 0); return; } } if (this.MoveBack) { if (this.Y > 0) { this.Y += -0.1; } } this.Mesh.rotation.set(0, 0, 0); } Shot() { let burret = new JikiBurret(this.X, this.Y, this.Z, 0, 0.3, 0); JikiBurrets.push(burret); burret.AddScene(scene); } AddScene(scene) { scene.add(this.Mesh); } RemoveScene(scene) { scene.remove(this.Mesh); } get X() { return this.Mesh.position.x; } get Y() { return this.Mesh.position.y; } get Z() { return this.Mesh.position.z; } set X(value) { this.Mesh.position.x = value; } set Y(value) { this.Mesh.position.y = value; } set Z(value) { this.Mesh.position.z = value; } } class JikiBurret extends GameCharacter { constructor(x, y, z, vx, vy, vz) { super(); if (JikiBurret.BurretMaterial == null) { JikiBurret.BurretMaterial = this.GetMaterial(DataURL.dataURL_JikiBurret); } this.sprite = new THREE.Sprite(JikiBurret.BurretMaterial); this.size = 0.3; this.sprite.scale.set(this.size, this.size, this.size); this.life = 1; this.sprite.position.set(x, y, z); this.VX = vx; this.VY = vy; this.VZ = vz; } } JikiBurret.BurretMaterial = null; |
enemy.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
class Enemy extends GameCharacter { constructor() { super(...arguments); this.score = 40; } } class Enemy1 extends Enemy { constructor() { super(); this.moveCount = 0; this.moveRight = false; if (Enemy1.enemyMaterial10 == null) Enemy1.enemyMaterial10 = this.GetMaterial(DataURL.dataURL_Enemy10); if (Enemy1.enemyMaterial11 == null) Enemy1.enemyMaterial11 = this.GetMaterial(DataURL.dataURL_Enemy11); if (Enemy1.enemyMaterial12 == null) Enemy1.enemyMaterial12 = this.GetMaterial(DataURL.dataURL_Enemy12); if (Enemy1.enemyMaterial13 == null) Enemy1.enemyMaterial13 = this.GetMaterial(DataURL.dataURL_Enemy13); this.sprite = new THREE.Sprite(Enemy1.enemyMaterial10); this.life = 3; this.score = 40; this.X = Math.random() * 12 - 6; this.Y = 30; this.Z = 0; if (Math.random() > 0.5) this.moveRight = true; } Move() { this.moveCount++; let additions = Stage; if (GameStatus == Status.GameOver) additions = 0; this.VY = -0.1 - 0.01 * additions; if (this.moveRight && this.X > 8) this.moveRight = false; else if (!this.moveRight && this.X < -8) this.moveRight = true; this.VX = 0.1 + 0.02 * additions; if (!this.moveRight) this.VX *= -1; let a = 60 - 5 * additions; if (a < 20) a = 20; if (this.moveCount % a == 0 && Math.random() > 0.5 && this.Y > 2) this.Shot(); let i = this.moveCount % 32; if (i < 8) this.sprite.material = Enemy1.enemyMaterial10; else if (i < 16) this.sprite.material = Enemy1.enemyMaterial11; else if (i < 24) this.sprite.material = Enemy1.enemyMaterial12; else this.sprite.material = Enemy1.enemyMaterial13; super.Move(); } Shot() { if (GameStatus == Status.GameOver) return; let x = jiki.X - this.X; let y = jiki.Y - this.Y; let angle1 = Math.atan2(y, x); let speed = 0.15 + 0.01 * Stage; let burret = new EnemyBurret(this.X, this.Y, this.Z, Math.cos(angle1) * speed, Math.sin(angle1) * speed, 0); EnemyBurrets.push(burret); burret.AddScene(scene); } } Enemy1.enemyMaterial10 = null; Enemy1.enemyMaterial11 = null; Enemy1.enemyMaterial12 = null; Enemy1.enemyMaterial13 = null; class Enemy2 extends Enemy { constructor() { super(); this.away = false; this.moveRight = false; this.moveCount = 0; if (Enemy2.enemyMaterial20 == null) Enemy2.enemyMaterial20 = this.GetMaterial(DataURL.dataURL_Enemy20); if (Enemy2.enemyMaterial21 == null) Enemy2.enemyMaterial21 = this.GetMaterial(DataURL.dataURL_Enemy21); if (Enemy2.enemyMaterial22 == null) Enemy2.enemyMaterial22 = this.GetMaterial(DataURL.dataURL_Enemy22); if (Enemy2.enemyMaterial23 == null) Enemy2.enemyMaterial23 = this.GetMaterial(DataURL.dataURL_Enemy23); this.sprite = new THREE.Sprite(Enemy2.enemyMaterial20); this.life = 1; this.score = 40; this.X = Math.random() * 12 - 6; this.Y = 30; this.Z = 0; if (Math.random() > 0.5) this.moveRight = true; } Move() { this.moveCount++; if (this.Y < 8) { this.away = true; this.Shot(); } if (this.away) this.VY = 0.3; else this.VY = -0.3; let additions = Stage; if (GameStatus == Status.GameOver) additions = 0; if (this.moveRight && this.X > 8) this.moveRight = false; else if (!this.moveRight && this.X < -8) this.moveRight = true; this.VX = 0.1 + 0.02 * additions; if (!this.moveRight) this.VX *= -1; let i = this.moveCount % 32; if (i < 8) this.sprite.material = Enemy2.enemyMaterial20; else if (i < 16) this.sprite.material = Enemy2.enemyMaterial21; else if (i < 24) this.sprite.material = Enemy2.enemyMaterial22; else this.sprite.material = Enemy2.enemyMaterial23; super.Move(); } Shot() { if (GameStatus == Status.GameOver) return; let x = jiki.X - this.X; let y = jiki.Y - this.Y; let angle1 = Math.atan2(y, x); let angle2 = angle1 + 0.3; let angle3 = angle1 - 0.3; let speed = 0.1 + 0.01 * Stage; let burret1 = new EnemyBurret(this.X, this.Y, this.Z, Math.cos(angle1) * speed, Math.sin(angle1) * speed, 0); EnemyBurrets.push(burret1); burret1.AddScene(scene); let burret2 = new EnemyBurret(this.X, this.Y, this.Z, Math.cos(angle2) * speed, Math.sin(angle2) * speed, 0); EnemyBurrets.push(burret2); burret2.AddScene(scene); let burret3 = new EnemyBurret(this.X, this.Y, this.Z, Math.cos(angle3) * speed, Math.sin(angle3) * speed, 0); EnemyBurrets.push(burret3); burret3.AddScene(scene); } } Enemy2.enemyMaterial20 = null; Enemy2.enemyMaterial21 = null; Enemy2.enemyMaterial22 = null; Enemy2.enemyMaterial23 = null; class EnemyBurret extends GameCharacter { constructor(x, y, z, vx, vy, vz) { super(); this.moveCount = 0; if (EnemyBurret.enemyBurretMaterial0 == null) EnemyBurret.enemyBurretMaterial0 = this.GetMaterial(DataURL.dataURL_EnemyBurret0); if (EnemyBurret.enemyBurretMaterial1 == null) EnemyBurret.enemyBurretMaterial1 = this.GetMaterial(DataURL.dataURL_EnemyBurret1); this.sprite = new THREE.Sprite(EnemyBurret.enemyBurretMaterial0); this.life = 1; this.size = 0.3; this.X = x; this.Y = y; this.Z = z; this.VX = vx; this.VY = vy; this.VZ = vz; } Move() { this.moveCount++; super.Move(); let i = this.moveCount % 16; if (i < 8) { this.sprite.material = EnemyBurret.enemyBurretMaterial0; this.sprite.scale.set(this.size, this.size, this.size); } else { this.sprite.material = EnemyBurret.enemyBurretMaterial1; this.sprite.scale.set(this.size * 1.5, this.size * 1.5, this.size * 1.5); } } } EnemyBurret.enemyBurretMaterial0 = null; EnemyBurret.enemyBurretMaterial1 = null; class Boss extends Enemy { constructor() { super(); this.moveCount = 0; this.moveRight = false; if (Boss.bossSplite == null) { let img = new Image(); img.src = DataURL.dataURL_Boss; var texture = new THREE.Texture(img); texture.needsUpdate = true; const material = new THREE.SpriteMaterial({ map: texture }); Boss.bossSplite = new THREE.Sprite(material); } this.size = 8.0; this.sprite = Boss.bossSplite; this.sprite.scale.set(this.size, this.size, this.size); this.life = Boss.LifeMax; this.score = 5000; this.X = 0; this.Y = 25; this.Z = 10; // 2 if (Math.random() > 0.5) this.moveRight = true; Boss.isLock = true; setTimeout(this.UnLock, 3000); } UnLock() { Boss.isLock = false; } Move() { if (this.Z > 2) this.Z -= 0.05; else this.Z = 2; if (Boss.isLock) return; this.moveCount++; this.VY = 0; if (this.moveRight && this.X > 8) this.moveRight = false; else if (!this.moveRight && this.X < -8) this.moveRight = true; this.VX = 0.1 + 0.02 * Stage; if (!this.moveRight) this.VX *= -1; if (this.moveCount % 40 == 0) this.Shot(); super.Move(); } Shot() { let speed = 0.15 + 0.01 * Stage; for (let i = 0; i < 48; i++) { let angle = 2 * Math.PI * i * 7.5 / 360; let startX = this.X + 8 * Math.cos(angle); let startY = this.Y + 8 * Math.sin(angle); let burret1 = new EnemyBurret(startX, startY, 0, Math.cos(angle) * speed, Math.sin(angle) * speed, 0); EnemyBurrets.push(burret1); burret1.AddScene(scene); } } } Boss.bossSplite = null; Boss.isLock = true; Boss.LifeMax = 100; |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
let scene = new THREE.Scene(); let renderer; let light; let camera; const RendererWidth = 600; const RendererHeight = 400; let HeaderContext; let FooterContext; let Stage = 1; var Status; (function (Status) { Status[Status["Battle"] = 0] = "Battle"; Status[Status["Boss"] = 1] = "Boss"; Status[Status["GameOver"] = 2] = "GameOver"; })(Status || (Status = {})); let GameStatus = Status.GameOver; let jiki; let JikiBurrets = []; let Enemies = []; let EnemyBurrets = []; let Explosions = []; let CanvasBackColor = "black"; window.addEventListener("DOMContentLoaded", init); document.onkeydown = OnKeyDown; document.onkeyup = OnKeyUp; function init() { let HeaderCanvas = document.getElementById('HeaderCanvas'); HeaderCanvas.width = RendererWidth; HeaderCanvas.height = 60; HeaderContext = HeaderCanvas.getContext("2d"); let FooterCanvas = document.getElementById('FooterCanvas'); FooterCanvas.width = RendererWidth; FooterCanvas.height = 40; FooterContext = FooterCanvas.getContext("2d"); // レンダラーを作成 renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('MainCanvas') }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(RendererWidth, RendererHeight); // カメラを作成 camera = new THREE.PerspectiveCamera(45, RendererWidth / RendererHeight, 0.01, 60); camera.position.set(0, -6, 5); camera.lookAt(new THREE.Vector3(0, 7, 0)); // 光源 light = new THREE.AmbientLight(0xFFFFFF, 1.0); light.position.set(0, -1, 1); light.intensity = 2; scene.add(light); // 縦横の直線を描画して3Dっぽさを演出する for (let y = -50; y < 50; y += 2) AddLineEW(y); for (let x = -50; x < 50; x += 2) AddLineNS(x); ShowStringGameOver(); scene.remove(GameOverSprite); jiki = new Jiki(); tick(); } // 縦横の直線を描画して3Dっぽさを演出する // LineEWsは直線の描画位置を変更するときに必要 let LineEWs = []; function AddLineEW(y) { const line_material = new THREE.LineBasicMaterial({ color: 0x008000 }); //geometryの宣言と生成 //@ts-ignore var line_geometry = new THREE.Geometry(); //頂点座標の追加 line_geometry.vertices.push(new THREE.Vector3(-50, y, -2), new THREE.Vector3(50, y, -2)); //線オブジェクトの生成 let line = new THREE.Line(line_geometry, line_material); //sceneにlineを追加 scene.add(line); LineEWs.push(line); } function AddLineNS(x) { const line_material = new THREE.LineBasicMaterial({ color: 0x008000 }); //geometryの宣言と生成 //@ts-ignore var line_geometry = new THREE.Geometry(); //頂点座標の追加 line_geometry.vertices.push(new THREE.Vector3(x, -50, -2), new THREE.Vector3(x, 50, -2)); //線オブジェクトの生成 let line = new THREE.Line(line_geometry, line_material); //sceneにlineを追加 scene.add(line); } let tickCount = 0; function tick() { //console.log("scene.children.length = " + scene.children.length); tickCount++; requestAnimationFrame(tick); // フィールドに描画されている縦横の直線を手前に向かって移動させ自機が前に移動しているように見せかける MoveLines(); if (GameStatus != Status.GameOver) { // 当たり判定(ゲームオーバーになったあとは考えなくてよい) // 自機から発射された弾丸は敵に命中したか? CheckMyBurretsHit(); // 敵機から発射された弾丸は自機に命中したか? CheckEnemyBurretsHit(); // 自機が敵そのものと衝突してしまったか? CheckJikiCollidedEnemis(); // 自機を移動させる jiki.Move(); } // ゲームオーバーになった弾丸の動きが止まってしまわないようにGameStatus == Status.Noneであっても処理は必要 // 自機の弾丸を移動させる JikiBurretsMove(); // 敵を移動させる EnemieMove(); // 敵の弾丸を移動させる EnemyBurretsMove(); // 爆発を移動させる ExplosionsMove(); // 必要なら新しい敵をつくる CreateNewEnemyIfNeed(); // レンダリング renderer.render(scene, camera); // 得点やボスのLifeなどを表示する ShowEtc(); } // フィールドに描画されている縦横の直線を手前に向かって移動させ自機が前に移動しているように見せかける function MoveLines() { LineEWs.forEach(x => { x.translateY(-0.1); }); LineEWs.forEach(x => { if (x.position.y < -2) x.translateY(50); }); } // 自機から発射された弾丸は敵に命中したか? function CheckMyBurretsHit() { for (let i = 0; i < Enemies.length; i++) { if (Enemies[i].life <= 0) continue; for (let j = 0; j < JikiBurrets.length; j++) { if (JikiBurrets[j].life <= 0) continue; let a = Math.pow((Enemies[i].size * 0.5 + JikiBurrets[j].size * 0.5), 2); let distance = Math.pow((Enemies[i].X - JikiBurrets[j].X), 2) + Math.pow((Enemies[i].Y - JikiBurrets[j].Y), 2); if (a > distance) { if (Enemies[i].constructor === Boss && Boss.isLock) break; Enemies[i].life--; BurretHit(Enemies[i]); JikiBurrets[j].life = 0; break; } } } } // 敵機から発射された弾丸は自機に命中したか? function CheckEnemyBurretsHit() { for (let i = 0; i < EnemyBurrets.length; i++) { if (EnemyBurrets[i].life <= 0) continue; let radis2 = Math.pow((EnemyBurrets[i].size * 0.5 + jiki.size * 0.5), 2); let distance2 = Math.pow((EnemyBurrets[i].X - jiki.X), 2) + Math.pow((EnemyBurrets[i].Y - jiki.Y), 2); if (radis2 > distance2) { EnemyBurrets[i].life = 0; JikiDamage(); } } } // 自機が敵そのものと衝突してしまったか? function CheckJikiCollidedEnemis() { for (let i = 0; i < Enemies.length; i++) { if (Enemies[i].life <= 0) continue; let radis2 = Math.pow((Enemies[i].size * 0.5 + jiki.size * 0.5), 2); let distance2 = Math.pow((Enemies[i].X - jiki.X), 2) + Math.pow((Enemies[i].Y - jiki.Y), 2); if (radis2 > distance2) { Enemies[i].life = 0; JikiDamage(); } } } // 自機の弾丸を移動させる function JikiBurretsMove() { // 離れた位置まで飛びすぎた弾丸やすでに何かに命中した弾丸は描画対象からはずす JikiBurrets.forEach(x => x.Move()); let noNeedBurrets = JikiBurrets.filter(x => !(x.life >= 1 && x.Y < 40)); JikiBurrets = JikiBurrets.filter(x => x.life >= 1 && x.Y < 40); noNeedBurrets.forEach(x => x.RemoveScene(scene)); } // 敵を移動させる function EnemieMove() { // 離れた位置まで移動しすぎた敵や撃墜された敵は描画対象からはずす Enemies.forEach(x => x.Move()); let noNeedEnemies = Enemies.filter(x => !(x.life >= 1 && x.Y > -1 && x.Y < 40)); Enemies = Enemies.filter(x => x.life >= 1 && x.Y > -1 && x.Y < 40); noNeedEnemies.forEach(x => x.RemoveScene(scene)); } // 敵の弾丸を移動させる function EnemyBurretsMove() { // 離れた位置まで飛びすぎた弾丸やすでに何かに命中した弾丸は描画対象からはずす EnemyBurrets.forEach(x => x.Move()); let noNeedEnemyBurrets = EnemyBurrets.filter(x => !(x.life >= 1 && x.Y > -1 && x.Y < 50 && x.X < 50 && x.X > -50)); EnemyBurrets = EnemyBurrets.filter(x => x.life >= 1 && x.Y > -1 && x.Y < 50 && x.X < 50 && x.X > -50); noNeedEnemyBurrets.forEach(x => x.RemoveScene(scene)); } // 爆発を移動させる function ExplosionsMove() { Explosions.forEach(x => x.Move()); let noNeedExplosions = Explosions.filter(x => x.life < 1); Explosions = Explosions.filter(x => !(x.life < 1)); noNeedExplosions.forEach(x => x.RemoveScene(scene)); } // 必要なら新しい敵をつくる。ただしisInterval == trueのときは生成しない let isInterval = false; function CreateNewEnemyIfNeed() { // ステージクリア直後などisInterval=trueになる。 // このフラグが立っているときは新しい敵はつくらない if (isInterval) return; // 新しいステージがはじまって一定時間するとボス戦がはじまる let BeginBossTickCount = 3000; //3000 if (tickCount < BeginBossTickCount) { CreateEnemy1(); CreateEnemy2(); if (GameStatus == Status.Battle) PlayBattleBGM.Check(); } else { // ゲームオーバー以降はザコ敵が表示されるだけでボス戦ははじまらない if (GameStatus == Status.GameOver) { tickCount = 0; // 対ボス戦の最中にゲームオーバーになった場合は // ボスはフィールド上からいなくなる if (boss != null) boss.life = 0; } else { // 新しいステージがはじまってtickCount >< BeginBossTickCountとなり、 // ザコ敵もいなくなったらボス戦がはじまる if (Enemies.length == 0) { if (GameStatus == Status.Battle) { PlayBattleBGM.Stop(); // 通常戦闘時のBGMを停止 isInterval = true; // すぐにボスを出現させずに2秒間の間をもたせる setTimeout(BeginBoss, 2000); } } } // ボスのLifeが低下してきたらザコ敵も出現させ、ゲームの難易度を上げる if (GameStatus == Status.Boss) { PlayBattleBossBGM.Check(); if (boss.life < Boss.LifeMax / 2) // 50%を切ったらタイプ2の敵を出現させる CreateEnemy2(); if (boss.life < Boss.LifeMax / 4) // 25%を切ったらさらにタイプ2の敵も出現させる CreateEnemy1(); } } } // 敵を生成して描画されるようにする function CreateEnemy1() { if (tickCount % 100 == 50) { let enemy = new Enemy1(); Enemies.push(enemy); enemy.AddScene(scene); } } function CreateEnemy2() { if (tickCount % 100 == 0) { let enemy = new Enemy2(); Enemies.push(enemy); enemy.AddScene(scene); } } // ボス戦を開始する let boss = null; function BeginBoss() { // ボスを生成、シーンに追加 boss = new Boss(); Enemies.push(boss); boss.AddScene(scene); // eStatusをStatus.Bossに変えてBGMも変える PlayBattleBossBGM.Start(); GameStatus = Status.Boss; isInterval = false; } // 得点やボスのLifeなどを表示する function ShowEtc() { HeaderContext.clearRect(0, 0, RendererWidth, 60); HeaderContext.fillStyle = CanvasBackColor; HeaderContext.fillRect(0, 0, RendererWidth, 70); ShowBossLife(); ShowScore(); ShowJikiLife(); } function ShowJikiLife() { FooterContext.fillStyle = CanvasBackColor; FooterContext.fillRect(0, 0, RendererWidth, 40); if (jiki.life > 0 && GameStatus != Status.GameOver) { FooterContext.fillStyle = "#0f0"; FooterContext.fillRect(20, 10, (RendererWidth - 40) * (jiki.life / jiki.LifeMax), 20); } } function ShowBossLife() { HeaderContext.fillStyle = CanvasBackColor; HeaderContext.fillRect(0, 40, RendererWidth, 20); if (GameStatus == Status.Boss && boss.life > 0) { HeaderContext.fillStyle = "yellow"; HeaderContext.fillRect(20, 40, (RendererWidth - 40) * (boss.life / Boss.LifeMax), 20); } } let score = 0; function ShowScore() { HeaderContext.fillStyle = "white"; HeaderContext.font = "30px 'MS ゴシック'"; let scoreText = "" + score; HeaderContext.fillText(scoreText, 10, 30); } // 自機から発射された弾丸が命中した場合の爆発の発生と点数加算の処理 function BurretHit(x) { if (x.constructor === Boss) { BurretHitBoss(x); } else { if (x.life <= 0) { // ザコ敵に命中してlifeを0にした場合は通常の爆発 Explode(x.X, x.Y); score += 50; } else { // ザコ敵に命中したがlifeが残っている場合は小爆発 SmallExplode(x.X, x.Y); } } } function BurretHitBoss(boss) { // ボスに命中してlifeを0にした場合は大爆発 if (boss.life <= 0) { BigExplode(boss.X, boss.Y); // ボス戦は終了したのでボス戦のBGMは停止する PlayBattleBossBGM.Stop(); score += boss.score; // ボスのまわりにいるザコ敵も爆発させ、点数加算 Enemies.forEach(x => { x.life = 0; Explode(x.X, x.Y); score += x.score / 2; }); // 5秒間のインターバルをおき、つぎのステージに isInterval = true; setTimeout(BeginNextStage, 5000); } else { // ボスに命中したがlifeが残っている場合は小爆発 SmallExplode(boss.X, boss.Y); score += 10; } } function BeginNextStage() { isInterval = false; tickCount = 0; GameStatus = Status.Battle; Stage++; PlayBattleBGM.Start(); } function Explode(centerX, centerY) { let baseSpeed = 0.1; for (let i = 0; i < 10; i++) { let r = Math.random(); let vx = r * baseSpeed * 2 - baseSpeed; r = Math.random(); let vy = r * baseSpeed * 2 - baseSpeed; r = Math.random(); let vz = r * baseSpeed * 2 - baseSpeed; let enp = new Explosion(centerX, centerY, vx, vy, vz, 1); enp.AddScene(scene); Explosions.push(enp); } PlaySoundeffect.Explosion(); } function SmallExplode(centerX, centerY) { let enp = new Explosion(centerX, centerY, 0, 0, 0, 1); enp.AddScene(scene); Explosions.push(enp); } function BigExplode(centerX, centerY) { let baseSpeed = 0.3; for (let i = 0; i < 30; i++) { let r = Math.random(); let vx = r * baseSpeed * 2 - baseSpeed; r = Math.random(); let vy = r * baseSpeed * 2 - baseSpeed; r = Math.random(); let vz = r * baseSpeed * 2 - baseSpeed; let enp = new Explosion(centerX, centerY, vx, vy, vz, 3); enp.AddScene(scene); Explosions.push(enp); } PlaySoundeffect.BigExplosion(); } // 被弾時の処理 // 被弾時に立て続けにやられないように一時的に「無敵状態」にする let MutekiMode = false; function JikiDamage() { if (!MutekiMode) { jiki.life--; PlaySoundeffect.Damage(); Explode(jiki.X, jiki.Y); MutekiMode = true; setTimeout(EndMutekiMode, 1000); // 被弾時は背景を一瞬赤くする BackColor("red"); setTimeout(BackColor, 100, "black"); // 自機のlifeが0になったらゲームオーバー if (jiki.life <= 0) { // 自機が描画されないようにSceneから除去 scene.remove(jiki.Mesh); // BGM停止 PlayBattleBGM.Stop(); PlayBattleBossBGM.Stop(); // ゲームオーバーの表示 GameStatus = Status.GameOver; ShowStringGameOver(); } } } function EndMutekiMode() { MutekiMode = false; } function BackColor(color) { renderer.setClearColor(color, 1); CanvasBackColor = color; } // ゲームオーバー時はその旨表示する let GameOverSprite = null; let PressSKeySprite = null; function ShowStringGameOver() { if (GameOverSprite == null) { let canvasTexture = new THREE.CanvasTexture(CreateCanvasForTexture(500, 500, 'GAME OVER', 50)); let scaleMaster = 5; GameOverSprite = CreateSprite(canvasTexture, { x: scaleMaster, y: scaleMaster, z: scaleMaster, }, { x: 0, y: 0, z: 4 }); } if (PressSKeySprite == null) { let canvasTexture2 = new THREE.CanvasTexture(CreateCanvasForTexture(500, 500, 'START PRESS S KEY', 50)); let scaleMaster2 = 3; PressSKeySprite = CreateSprite(canvasTexture2, { x: scaleMaster2, y: scaleMaster2, z: scaleMaster2, }, { x: 0, y: 0, z: 3 }); } scene.add(GameOverSprite); scene.add(PressSKeySprite); } function CreateSprite(texture, scale, position) { const spriteMaterial = new THREE.SpriteMaterial({ map: texture }); const sprite = new THREE.Sprite(spriteMaterial); sprite.scale.set(scale.x, scale.y, scale.z); sprite.position.set(position.x, position.y, position.z); return sprite; } ; function CreateCanvasForTexture(canvasWidth, canvasHeight, text, fontSize) { // 貼り付けるcanvasを作成。 const canvasForText = document.createElement('canvas'); const ctx = canvasForText.getContext('2d'); ctx.canvas.width = canvasWidth; // 小さいと文字がぼやける ctx.canvas.height = canvasHeight; // 小さいと文字がぼやける // 透過する背景を描く ctx.fillStyle = 'rgba(0, 0, 0, 0.0)'; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.fillStyle = 'white'; ctx.font = `${fontSize}px 'MS ゴシック'`; ctx.fillText(text, // x方向の余白/2をx方向開始時の始点とすることで、横方向の中央揃えをしている。 (canvasWidth - ctx.measureText(text).width) / 2, // y方向のcanvasの中央に文字の高さの半分を加えることで、縦方向の中央揃えをしている。 canvasHeight / 2 + ctx.measureText(text).actualBoundingBoxAscent / 2); return canvasForText; } ; // キー操作に関する処理 // 方向キーが押されたらフラグをセットし、離されたらクリアする // スペースキーは弾丸発射、Sキーはゲームスタート function OnKeyDown(e) { if (e.keyCode == 37) { // ← jiki.MoveLeft = true; } if (e.keyCode == 38) { // ↑ jiki.MoveFront = true; } if (e.keyCode == 39) { // → jiki.MoveRight = true; } if (e.keyCode == 40) { // ↓ jiki.MoveBack = true; } if (e.keyCode == 32 && GameStatus != Status.GameOver) { // Space キー jiki.Shot(); } if (e.keyCode == 83) { // S キー GameStart(); } } function OnKeyUp(e) { if (e.keyCode == 37) { jiki.MoveLeft = false; } if (e.keyCode == 38) { jiki.MoveFront = false; } if (e.keyCode == 39) { jiki.MoveRight = false; } if (e.keyCode == 40) { jiki.MoveBack = false; } } function GameStart() { Enemies.forEach(x => x.life = 0); EnemyBurrets.forEach(x => x.life = 0); ; JikiBurrets.forEach(x => x.life = 0); ; Explosions.forEach(x => x.life = 0); ; jiki.AddScene(scene); jiki.X = 0; jiki.life = jiki.LifeMax; score = 0; Stage = 1; GameStatus = Status.Battle; tickCount = 0; PlayBattleBGM.Start(); if (GameOverSprite != null) scene.remove(GameOverSprite); if (PressSKeySprite != null) scene.remove(PressSKeySprite); } |