Add crashing

This commit is contained in:
asraelite 2018-03-07 14:51:18 +00:00
parent bd97945e66
commit 60f77c36a9
9 changed files with 67 additions and 10 deletions

BIN
dist/audio/crash2.mp3 vendored Normal file

Binary file not shown.

View file

@ -40,7 +40,9 @@ export const audio = {
endEdit: 'release1.mp3',
newPlanet: 'up2.mp3',
engine: 'rocket2.ogg',
music: 'music2.mp3'
music: 'music2.mp3',
toss: 'thunk1.mp3',
crash: 'crash2.mp3'
};
export async function init() {
@ -52,7 +54,7 @@ export async function init() {
parse(images, str => {
let img = new Image();
img.src = 'img/' + str;
promises.push(new Promise((res, rej) => {
promises.push(new Promise((res) => {
img.addEventListener('load', res);
}));
return img;
@ -61,7 +63,7 @@ export async function init() {
let audio = new Howl({
src: ['audio/' + str]
});
promises.push(new Promise((res, rej) => {
promises.push(new Promise((res) => {
audio.once('load', res);
}));
return audio;

View file

@ -22,6 +22,7 @@ export const ZOOM_SPEED = 0.01;
// Ship landing. Angle in radians.
export const TIP_ANGLE = 0.25;
export const TIP_SPEED = 0.03;
export const CRASH_SPEED = 0.7;
// Ship flight mechanics. Speed measured in units per tick.
export const FUEL_BURN_RATE = 0.3;
export const THRUST_POWER = 0.004;

View file

@ -26,7 +26,7 @@ export function tick() {
if (state.editing) {
tickEditing();
} else if (state.playing) {
} else if (state.playing && !state.gameOver) {
tickPlaying();
}
@ -36,6 +36,10 @@ export function tick() {
}
}
if (state.gameOver) {
audio.stop('engine');
}
if (pressed[mapping.toggleMusic]) {
audio.toggle('music');
}

View file

@ -32,7 +32,7 @@ function notify(message, time = 80) {
export function tick() {
if (notification === null) return;
if (notLife-- <= 0)
if (notLife-- <= 0 || game.state.gameOver)
notification.text = '';
}
@ -70,6 +70,12 @@ export function launchShip() {
game.state.landed = false;
}
export function crash() {
audio.play('crash');
particle.createCrash(world.playerShip)
game.state.gameOver = true;
}
export function toggleEdit() {
if (game.state.editing) {
endEditing();
@ -122,6 +128,7 @@ export function tilePlacement() {
export function tossItem() {
particle.createItemToss(world.playerShip);
audio.play('toss');
}
export function collectItem(type, id, name) {

View file

@ -16,7 +16,8 @@ export async function init() {
playing: false,
editing: false,
paused: false,
inventory: false
inventory: false,
gameOver: false
};
graphics.init();
@ -24,8 +25,8 @@ export async function init() {
gui.init();
input.init();
//events.playMusic();
events.startGame();
events.playMusic();
//events.startGame();
//tick(); return;

View file

@ -46,6 +46,7 @@ function renderEntity(entity) {
}
function renderShip(ship) {
if (ship.crashed) return;
context.save();
context.translate(...ship.com);
context.rotate(ship.r);

View file

@ -27,6 +27,37 @@ export function createEndEditBurst(ship) {
}
}
export function createCrash(ship) {
for (let i = 0; i < ship.mass + 3; i++) {
particles.add(new Particle(...ship.poc, {
color: '#f2e860',
lifetime: Math.random() * 50 + 40,
size: Math.random() * 0.2 + 0.2,
spray: 0.9,
gravity: true
}));
}
for (let i = 0; i < ship.mass + 3; i++) {
particles.add(new Particle(...ship.poc, {
color: '#f75722',
lifetime: Math.random() * 50 + 40,
size: Math.random() * 0.2 + 0.2,
spray: 0.5,
gravity: true
}));
}
for (let i = 0; i < ship.mass * 2 + 3; i++) {
particles.add(new Particle(...ship.poc, {
color: '#888',
lifetime: Math.random() * 30 + 55,
size: Math.random() * 0.5 + 0.4,
spray: 2,
friction: 0.9,
gravity: false
}));
}
}
export function createPickupBurst(ship, point) {
for (let i = 0; i < 20; i++) {
particles.add(new Particle(...point, {

View file

@ -22,6 +22,7 @@ export default class Ship extends Body {
this.rotationPower = 0;
this.cargoCapacity = 0;
this.thrust = 0;
this.crashed = false;
}
get com() {
@ -48,7 +49,7 @@ export default class Ship extends Body {
}
tick() {
window.q = [];
if (this.crashed) return;
if (!state.editing) this.tickMotion();
if (!this.landed) this.tickGravity(world.celestials);
if (!state.editing) this.resolveCollisions();
@ -151,10 +152,19 @@ export default class Ship extends Body {
let dis = body.distanceTo({ com: p });
if (dis < body.radius + 0.5 + consts.EPSILON) {
this.approach(body, dis - (body.radius + 0.5));
this.moduleCollided(module);
this.halt();
this.resolveCelestialCollision(p, body, module);
this.lastContactModule = module;
this.poc = p;
this.lastContactModule = module;
}
}
moduleCollided(module) {
let speed = Math.sqrt(this.xvel ** 2 + this.yvel ** 2);
if (module.type !== 'thruster' || speed > consts.CRASH_SPEED) {
events.crash();
this.crashed = true;
}
}