diff --git a/js/game/events.mjs b/js/game/events.mjs index 4a54fba..4717b0f 100644 --- a/js/game/events.mjs +++ b/js/game/events.mjs @@ -9,12 +9,19 @@ import * as consts from '../consts.mjs'; export let shipLanded = false; export let score = 0; +export let gameOverReason = ''; +export let scoreText = ''; let notification = null; let notLife = 0; let landedPlanets = new Set(); +export function init() { + score = 0; + shipLanded = false; +} + export function playMusic() { audio.start('music'); audio.volume('music', 0.4); @@ -41,7 +48,10 @@ export function setNotificationElement(el) { } export function startGame() { + init(); + game.state.gameOver = false; game.changeView('game'); + graphics.perspective.reset(); graphics.perspective.focusPlayer(); } @@ -84,9 +94,29 @@ export function launchShip() { } export function crash() { + gameOver('You crashed'); audio.play('crash'); - particle.createCrash(world.playerShip) + particle.createCrash(world.playerShip); + +} + +export function gameOver(reason) { + gameOverReason = reason; game.state.gameOver = true; + game.state.inventory = false; + game.state.editing = false; + graphics.perspective.changeZoom(consts.MIN_ZOOM, 0.99); + let massScore = world.playerShip.mass * 100; + let fuelScore = world.playerShip.fuel * 50 | 0; + let finalScore = massScore + fuelScore + score; + scoreText = 'Ship mass: ' + + ' '.repeat(5 - ('' + massScore).length) + massScore + '\n' + + 'Remaining fuel: ' + + ' '.repeat(5 - ('' + fuelScore).length) + fuelScore + '\n' + + 'Score: ' + + ' '.repeat(5 - ('' + score).length) + score + '\n\n' + + 'Final score: ' + + ' '.repeat(5 - ('' + finalScore).length) + finalScore; } export function toggleEdit() { diff --git a/js/game/index.mjs b/js/game/index.mjs index fc32b69..2f1b845 100644 --- a/js/game/index.mjs +++ b/js/game/index.mjs @@ -52,6 +52,7 @@ export function changeView(view) { gui.changeView('instructions'); } else if (view === 'menu') { gui.changeView('menu'); + world.clear(); } } diff --git a/js/graphics/index.mjs b/js/graphics/index.mjs index 9db6106..762dadc 100644 --- a/js/graphics/index.mjs +++ b/js/graphics/index.mjs @@ -101,6 +101,10 @@ class Perspective { this.shiftY = 0; this.zoom = 0; this.bounds = [0, 0, canvas.width, canvas.height]; + this.reset(); + } + + reset() { this.rotationMode = 'universe'; this.targetZoom = consts.DEFAULT_ZOOM; this.oldTarget = 0; @@ -109,7 +113,12 @@ class Perspective { this.transition = 0; this.zoomTransition = 0; this.zoomTransitionSpeed = 0.9; - this.reset(); + this.rotation = 0; + this.targetRotation = 0; + this.zoom = consts.DEFAULT_ZOOM; + this.targetZoom = this.zoom; + this.focus = null; + this.rotationFocus = null; } changeRotationMode(mode) { @@ -187,15 +196,6 @@ class Perspective { this.zoomTransition *= this.zoomTransitionSpeed; } - reset() { - this.rotation = 0; - this.targetRotation = 0; - this.zoom = consts.DEFAULT_ZOOM; - this.targetZoom = this.zoom; - this.focus = null; - this.rotationFocus = null; - } - focusPlayer() { this.focus = world.playerShip; this.rotationFocus = world.playerShip; diff --git a/js/gui/modules.mjs b/js/gui/modules.mjs index 5c09a50..cef07a9 100644 --- a/js/gui/modules.mjs +++ b/js/gui/modules.mjs @@ -211,5 +211,51 @@ export function game() { notification.y += 10; events.setNotificationElement(notification); + + let gameOver = root(); + shadow.append(gameOver); + gameOver.posRelative({x: 0.2, y: 0.2, w: 0.6, h: 0.6}); + + let gameOverMain = new GuiText('Game over', 0, 0, 0, 0, { + size: 48, + align: 'center', + valign: 'top' + }); + gameOver.append(gameOverMain); + gameOverMain.posRelative({x: 0.5}); + gameOverMain.y += 10; + gameOver.tick = () => { + gameOver.options.drawChildren = state.gameOver; + }; + + let gameOverReason = new GuiText('', 0, 0, 0, 0, { + size: 14, + align: 'center', + valign: 'top' + }); + gameOver.append(gameOverReason); + gameOverReason.posRelative({x: 0.5}); + gameOverReason.y += 100; + gameOverReason.tick = () => { + gameOverReason.text = events.gameOverReason; + }; + + let gameOverScore = new GuiText('', 0, 0, 0, 0, { + size: 14, + align: 'center', + valign: 'top' + }); + gameOver.append(gameOverScore); + gameOverScore.posRelative({x: 0.5}); + gameOverScore.y += 200; + gameOverScore.tick = () => { + gameOverScore.text = events.scoreText; + }; + + let gameOverExit = new GuiButton('Main menu', events.toMenu, 0, 0, 200); + gameOver.append(gameOverExit); + gameOverExit.posRelative({ x: 0.5, xc: 0.5, y: 1 }); + gameOverExit.y -= 10; + return shadow; } diff --git a/js/world/index.mjs b/js/world/index.mjs index bb95857..3bbc327 100644 --- a/js/world/index.mjs +++ b/js/world/index.mjs @@ -14,15 +14,19 @@ export function setPlayerShip(ship) { } export function init() { + clear(); + spawn.player(); + let p = spawn.startPlanet(); + spawn.testEntity(p); + spawn.tick(); +} + +export function clear() { entities.clear(); celestials.clear(); ships.clear(); particles.clear(); tracers.clear(); - spawn.player(); - let p = spawn.startPlanet(); - spawn.testEntity(p); - spawn.tick(); } export function remove(object) {