From b4defa324a330217f0139f1240bcf53c0b1f996a Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Thu, 2 Jun 2022 23:13:07 +0200 Subject: v1.0.0 --- src/game.ts | 214 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 136 insertions(+), 78 deletions(-) (limited to 'src/game.ts') diff --git a/src/game.ts b/src/game.ts index 4e1db38..07988fa 100644 --- a/src/game.ts +++ b/src/game.ts @@ -7,7 +7,7 @@ import Straight from "./enemies/Straight"; import Red1 from "./enemies/Red1"; import Red2 from "./enemies/Red2"; import Enemy from "./enemies/Enemy"; -import { clearCtx, sleep } from "./helpers"; +import { clearCtx, randomInteger, sleep } from "./helpers"; import Player from "./Player"; import { TopBar } from "./drawables/TopBar"; import { keys } from "./Events"; @@ -19,25 +19,33 @@ import White from "./enemies/White"; import Startscreen from "./drawables/Startscreen"; import { getHighscore } from "./Storage"; import AnimStart from "./drawables/AnimStart"; +import { IMGS, SOUNDS } from "./Images"; +import Powerup from "./drawables/Powerup"; let p = new Player() let pLifes = 3; -let score = 25004; +// pLifes = 1; +let score = 0; let highscore = getHighscore(); -let rolls = 3; +export const rolls = { r: 3 } +// let rolls = 3; let bullets = [] as Bullet[] const playerBullets = () => bullets.filter(b => b.players) const enemyBullets = () => bullets.filter(b => !b.players) let enemies = [] as Enemy[] +let enemShooted = 0; let animations = [] as Anime[] let intervalId = 0; let rafId = 0; -let showMenu = false +let showMenu = true let showAnimStart = true -const bg = new Background() -const tb = new TopBar() -const hg = new Highscore() -const animStart = new AnimStart(0, TOPBAR_HEIGHT) +// showMenu = false +// showAnimStart = false +let bg = new Background() +let tb = new TopBar() +let hg = new Highscore() +let animStart = new AnimStart(0, TOPBAR_HEIGHT) +let pu: Powerup | null = null // const ss = new Startscreen(); export async function start() { @@ -45,43 +53,53 @@ export async function start() { bullets = [] as Bullet[] enemies = [] as Enemy[] animations = [] as Anime[] - // enemies.push(new BigCircle(25, -20)) - // enemies.push(new Straight(20, 0)) - // enemies.push(new Red1(0, 51)) - // enemies.push(new Green1(115, canvas.height, true), new Green1(170, canvas.height, false)) - // enemies.push(new Strange(160, canvas.height)) - // enemies.push(new White(60, 0, true), new White(230, 0, false)) - // enemies.push(new Red2(100, 0, true), new Red2(180, 0, false)) - // enemies.push(new Green2(100, 0, true), new Green2(180, 0, false)) newTimeout() rafId = requestAnimationFrame(gpuLoop) } export function cpuLoop() { if (showMenu) { + SOUNDS.mainTheme.play() cpuMenu() newTimeout() return } + if (showAnimStart) { + return + } if (hg.show) { - hg.move(); - tb.setData(score, pLifes, rolls, highscore) - newTimeout() + SOUNDS.mainTheme.pause() + SOUNDS.name.play() + tb.setData(score, pLifes, rolls.r, bg.delta, enemShooted, highscore) + if (hg.move()) newTimeout() return; } + // hg.show = true + if (bg.delta > 2375) { p.autoPilot = true; p.stopAtBorder = false; } + if (bg.delta === 2580) { + SOUNDS.mainTheme.pause() + SOUNDS.win.play() + p.landed = true + } p.move() p.shoot(bullets) bg.move() spawnEnemies() - tb.setData(score, pLifes, rolls, highscore) + tb.setData(score, pLifes, rolls.r, bg.delta, enemShooted, highscore) // enemies = enemies.filter(e => !bullets.some(b => b.collides(e))) enemies.forEach(e => e.shoot(bullets)) enemies = enemies.filter(e => !e.move()) bullets = bullets.filter(b => !b.move()) + if (pu && pu.move()) pu = null const enemCollPlay = enemies.filter(e => p.collides([e])); const bullCollPlay = enemyBullets().filter(b => p.collides([b])); + if (pu && p.collides([pu])) { + pu = null + p.power = 3 + SOUNDS.powerup.play() + } bullets = bullets.filter(b => !bullCollPlay.includes(b)) // const enemiesToDie = enemies.filter(e => e.collides(bullets)) @@ -94,8 +112,12 @@ export function cpuLoop() { if (e.health < 1) { animations.push(e.deathAnim) score += e.points + enemShooted++; + SOUNDS.enemyDie.play() + if (e instanceof Straight && e.drop) pu = new Powerup(e.x, e.y) return false - } + } else if (e instanceof BigCircle) score += 100; + else if (e instanceof Strange) score += 100; } return true; }) @@ -110,15 +132,20 @@ export function cpuLoop() { export function gpuLoop() { rafId = requestAnimationFrame(gpuLoop) - clearCtx() + clearCtx(); + if (showMenu) { + gpuMenu(); + return; + } if (showAnimStart) { + SOUNDS.mainTheme.pause() + SOUNDS.startLevel.play() + SOUNDS.startLevel.onended = () => { SOUNDS.mainTheme.currentTime = 4.0; SOUNDS.mainTheme.play() } + tb.draw() showAnimStart = animStart.draw() - return - } - if (showMenu) { - gpuMenu() - return + if (showAnimStart) newTimeout() + return; } if (hg.show) { hg.drawHg(bg, score) @@ -127,48 +154,52 @@ export function gpuLoop() { } bg.draw() + if (pu) pu.draw() bullets.forEach(b => b.draw()) - bullets.forEach(b => b.squares.forEach(s => s.draw())) - p.squares.forEach(s => s.draw()) p.draw() - enemies.forEach(e => { - e.draw() - e.squares.forEach(s => s.draw()) - if (e instanceof BigCircle) { - e.squares.forEach(s => s.draw()) - } - }) + enemies.forEach(e => e.draw()) animations = animations.filter(a => a.draw()) tb.draw() } -export async function playerDied(_enemy: Enemy[]) { - animations.push(p.deathAnimation) +export function playerDied(_enemy: Enemy[]) { p.alive = false p.squares = [] - // clearTimeout(intervalId) - // cancelAnimationFrame(rafId) - // pLifes -= 1; - // if (pLifes !== 0) { - // await drawRestartScreen(); - // start() - // } else { - // hg.show = true - // newTimeout() - // requestAnimationFrame(gpuLoop) - // } + SOUNDS.playerDeath.play() + const da = p.deathAnimation; + da.cb = () => afterPlayerDeathAnim() + animations.push(p.deathAnimation) +} +export async function afterPlayerDeathAnim() { + clearTimeout(intervalId) + cancelAnimationFrame(rafId) + pLifes -= 1; + rolls.r = 3 + if (pLifes !== 0) { + await drawRestartScreen(); + start() + } else { + hg.show = true + newTimeout() + requestAnimationFrame(gpuLoop) + } } export async function drawRestartScreen() { clearCtx() - ctx.fillStyle = "yellow" - ctx.fillRect(0, 0, canvas.width, canvas.height) + tb.setData(score, pLifes, rolls.r, bg.delta, enemShooted, highscore) + tb.draw() + ctx.drawImage(IMGS.restartScreen, 0, TOPBAR_HEIGHT) + ctx.drawImage(IMGS.font.white[pLifes], 245, 29 + TOPBAR_HEIGHT, 15, 8) + Player.startx = 143 + Player.starty = 98 + TOPBAR_HEIGHT await sleep(2000) + bg.delta = 150 } -const startscreen = new Startscreen() -tb.setData(score, pLifes, rolls, highscore) +let startscreen = new Startscreen() +tb.setData(score, pLifes, rolls.r, 0, 0, highscore) function cpuMenu() { startscreen.move() if (keys.fire === true) showMenu = false; @@ -177,35 +208,62 @@ function gpuMenu() { tb.draw() startscreen.draw() } +export function startAgain() { + clearTimeout(intervalId) + cancelAnimationFrame(rafId) + Player.startx = 144; + Player.starty = 122; + bg.delta = 0 + pLifes = 3; + score = 0 + highscore = getHighscore(); + rolls.r = 3 + showMenu = true + showAnimStart = true + bg = new Background() + tb = new TopBar() + hg = new Highscore() + animStart = new AnimStart(0, TOPBAR_HEIGHT) + startscreen = new Startscreen() + whichStraight = randomInteger(0, 2) + tb.setData(score, pLifes, rolls.r, 0, 0, highscore) + SOUNDS.mainTheme.currentTime = 0 + SOUNDS.win.currentTime = 0 + SOUNDS.startLevel.currentTime = 0 + keys.fire = false + start() +} const newTimeout = (f = cpuLoop) => { clearTimeout(intervalId); intervalId = setTimeout(f, 1000 / keys.fps) } +let whichStraight = randomInteger(0, 2) function spawnEnemies() { - if (bg.delta === 180) enemies.push(new Straight(80, 0)); - if (bg.delta === 180 + 20) enemies.push(new Straight(110, 0)); - if (bg.delta === 180 + 20 + 30) enemies.push(new Straight(160, 0)); - if (bg.delta === 180 + 20 + 30 + 25) enemies.push(new Straight(210, 0)); - if (bg.delta === 180 + 20 + 30 + 25 + 80) enemies.push(new Straight(130, 0)); - if (bg.delta === 180 + 20 + 30 + 25 + 80 + 50) enemies.push(new Straight(90, 0)); - if (bg.delta === 180 + 20 + 30 + 25 + 80 + 50 + 32) enemies.push(new Straight(220, 0)); - if (bg.delta === 630) enemies.push(new BigCircle(16, TOPBAR_HEIGHT - 40)) - if (bg.delta === 630 + 38) enemies.push(new Straight(90, 0), new Straight(266, 0)) - if (bg.delta === 630 + 38 + 38) enemies.push(new Straight(140, 0)) - if (bg.delta === 630 + 38 + 38 + 70) enemies.push(new Straight(30, 0)) - if (bg.delta === 630 + 38 + 38 + 70 + 30) enemies.push(new Straight(266, 0)) - if (bg.delta === 1016 + 21 * 0) enemies.push(new Red1(-20, 52)) - if (bg.delta === 1016 + 21 * 1) enemies.push(new Red1(-20, 52)) - if (bg.delta === 1016 + 21 * 2) enemies.push(new Red1(-20, 52)) - if (bg.delta === 1016 + 21 * 3) enemies.push(new Red1(-20, 52)) - if (bg.delta === 1016 + 21 * 4) enemies.push(new Red1(-20, 52)) - if (bg.delta === 1120) enemies.push(new Green1(110, canvas.height, true), new Green1(170, canvas.height, false)) - if (bg.delta === 1498) enemies.push(new Strange(160, canvas.height)) - if (bg.delta === 1498 + 45) enemies.push(new Red2(100, 0, true), new Red2(180, 0, false)) - if (bg.delta === 1498 + 45 + 90) enemies.push(new Green2(100, 0, true), new Green2(180, 0, false)) - if (bg.delta === 2000 + 85 * 0) enemies.push(new White(60, 0, true), new White(230, 0, false)) - if (bg.delta === 2000 + 85 * 1) enemies.push(new White(60, 0, true), new White(230, 0, false)) - if (bg.delta === 2000 + 85 * 2) enemies.push(new White(60, 0, true), new White(230, 0, false)) - if (bg.delta === 2000 + 85 * 3) enemies.push(new White(60, 0, true)) + + ;;;; if (bg.delta === 190) enemies.push(new Straight(80, TOPBAR_HEIGHT)); + else if (bg.delta === 190 + 20) enemies.push(new Straight(110, TOPBAR_HEIGHT)); + else if (bg.delta === 190 + 20 + 30) enemies.push(new Straight(160, TOPBAR_HEIGHT)); + else if (bg.delta === 190 + 20 + 30 + 25) enemies.push(new Straight(210, TOPBAR_HEIGHT)); + else if (bg.delta === 190 + 20 + 30 + 25 + 80) enemies.push(new Straight(130, TOPBAR_HEIGHT, whichStraight === 0)); + else if (bg.delta === 190 + 20 + 30 + 25 + 80 + 50) enemies.push(new Straight(90, TOPBAR_HEIGHT, whichStraight === 1)); + else if (bg.delta === 190 + 20 + 30 + 25 + 80 + 50 + 32) enemies.push(new Straight(220, TOPBAR_HEIGHT, whichStraight === 2)); + else if (bg.delta === 640) enemies.push(new BigCircle(16, TOPBAR_HEIGHT - 40)) + else if (bg.delta === 640 + 38) enemies.push(new Straight(90, TOPBAR_HEIGHT), new Straight(266, TOPBAR_HEIGHT)) + else if (bg.delta === 640 + 38 + 38) enemies.push(new Straight(140, TOPBAR_HEIGHT)) + else if (bg.delta === 640 + 38 + 38 + 70) enemies.push(new Straight(30, TOPBAR_HEIGHT)) + else if (bg.delta === 640 + 38 + 38 + 70 + 30) enemies.push(new Straight(266, TOPBAR_HEIGHT)) + else if (bg.delta === 1026 + 21 * 0) enemies.push(new Red1(-24, 52)) + else if (bg.delta === 1026 + 21 * 1) enemies.push(new Red1(-24, 52)) + else if (bg.delta === 1026 + 21 * 2) enemies.push(new Red1(-24, 52)) + else if (bg.delta === 1026 + 21 * 3) enemies.push(new Red1(-24, 52)) + else if (bg.delta === 1026 + 21 * 4) enemies.push(new Red1(-24, 52)) + else if (bg.delta === 1120) enemies.push(new Green1(110, canvas.height, true), new Green1(170, canvas.height, false)) + else if (bg.delta === 1498) enemies.push(new Strange(160, canvas.height)) + else if (bg.delta === 1508 + 45) enemies.push(new Red2(100, TOPBAR_HEIGHT, true), new Red2(180, TOPBAR_HEIGHT, false)) + else if (bg.delta === 1508 + 45 + 90) enemies.push(new Green2(100, TOPBAR_HEIGHT, true), new Green2(180, TOPBAR_HEIGHT, false)) + else if (bg.delta === 2010 + 85 * 0) enemies.push(new White(60, TOPBAR_HEIGHT, true), new White(230, TOPBAR_HEIGHT, false)) + else if (bg.delta === 2010 + 85 * 1) enemies.push(new White(60, TOPBAR_HEIGHT, true), new White(230, TOPBAR_HEIGHT, false)) + else if (bg.delta === 2010 + 85 * 2) enemies.push(new White(60, TOPBAR_HEIGHT, true), new White(230, TOPBAR_HEIGHT, false)) + else if (bg.delta === 2010 + 85 * 3) enemies.push(new White(60, TOPBAR_HEIGHT, true)) // if (bg.delta > 2200) // console.log(bg.delta); -- cgit v1.3.1