Add game over screen

This commit is contained in:
asraelite 2018-03-07 17:01:14 +00:00
parent f91febfd3c
commit a11dfb9352
5 changed files with 96 additions and 15 deletions

View file

@ -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() {

View file

@ -52,6 +52,7 @@ export function changeView(view) {
gui.changeView('instructions');
} else if (view === 'menu') {
gui.changeView('menu');
world.clear();
}
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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) {