Add audio
This commit is contained in:
parent
0101ef8d55
commit
62b8c74f57
26 changed files with 225 additions and 18 deletions
|
@ -20,6 +20,9 @@ export const images = {
|
|||
off: 'modules/light_thruster.svg',
|
||||
on: 'modules/light_thruster_on.svg',
|
||||
}
|
||||
},
|
||||
connector: {
|
||||
xheavy: 'modules/xheavy_connector.svg'
|
||||
}
|
||||
},
|
||||
celestials: {
|
||||
|
@ -29,7 +32,10 @@ export const images = {
|
|||
}
|
||||
};
|
||||
|
||||
export const audio = {};
|
||||
export const audio = {
|
||||
itemPickup: 'up1.mp3',
|
||||
endEdit: 'release1.mp3'
|
||||
};
|
||||
|
||||
export async function init() {
|
||||
let parse = (obj, convert) => Object.entries(obj).forEach(([k, v]) => {
|
||||
|
@ -46,7 +52,11 @@ export async function init() {
|
|||
return img;
|
||||
});
|
||||
parse(audio, str => {
|
||||
// TODO: Load audio.
|
||||
let audio = new Audio('audio/' + str);
|
||||
promises.push(new Promise((res, rej) => {
|
||||
audio.addEventListener('canplaythrough', res);
|
||||
}));
|
||||
return audio;
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
|
|
16
js/data.mjs
16
js/data.mjs
|
@ -5,12 +5,13 @@ export const modules = {
|
|||
small: {
|
||||
name: 'Small Capsule',
|
||||
tooltip: 'A small, simple capsule. Provides just enough ' +
|
||||
'rotational power for a small rocket.',
|
||||
'rotational power for a small rocket and has a small ' +
|
||||
'amount of storage space.',
|
||||
type: 'capsule',
|
||||
id: 'small',
|
||||
mass: 2,
|
||||
connectivity: [false, false, true, false],
|
||||
capacity: 3,
|
||||
capacity: 2,
|
||||
rotation: 0.1
|
||||
}
|
||||
},
|
||||
|
@ -37,5 +38,16 @@ export const modules = {
|
|||
thrust: 10,
|
||||
isp: 200
|
||||
}
|
||||
},
|
||||
connector: {
|
||||
xheavy: {
|
||||
name: 'Heavy 4-way Connector',
|
||||
tooltip: 'Can connect ship parts in any direction, but is quite ' +
|
||||
'heavy',
|
||||
type: 'connector',
|
||||
id: 'xheavy',
|
||||
mass: 5,
|
||||
connectivity: [true, true, true, true]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
5
js/game/audio.mjs
Normal file
5
js/game/audio.mjs
Normal file
|
@ -0,0 +1,5 @@
|
|||
import {audio} from '../assets.mjs';
|
||||
|
||||
export function play(name) {
|
||||
audio[name].cloneNode(true).play();
|
||||
}
|
|
@ -87,7 +87,9 @@ function validate() {
|
|||
if (tile.type == 'thruster') thrustersFound++;
|
||||
if (tile.type == 'fuel') fuelFound++;
|
||||
tile.neighbours.forEach(n => {
|
||||
if (unvisited.has(n)) visit(n);
|
||||
if (unvisited.has(n) && n.neighbours.indexOf(tile) > -1) {
|
||||
visit(n);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -111,7 +113,7 @@ function validate() {
|
|||
} else {
|
||||
message = reason;
|
||||
}
|
||||
|
||||
|
||||
return reason;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import * as player from './player.mjs';
|
|||
import * as inventory from './inventory.mjs';
|
||||
import * as particle from '../world/particle.mjs';
|
||||
import * as edit from './edit.mjs';
|
||||
import * as audio from './audio.mjs';
|
||||
|
||||
export let shipLanded = false;
|
||||
|
||||
|
@ -71,6 +72,7 @@ export function endEditing() {
|
|||
let {valid, reason} = edit.end();
|
||||
|
||||
if (valid) {
|
||||
audio.play('endEdit');
|
||||
graphics.changePerspective('universe');
|
||||
game.state.editing = false;
|
||||
game.state.inventory = false;
|
||||
|
@ -91,5 +93,6 @@ export function tossItem() {
|
|||
|
||||
export function collectItem(type, id) {
|
||||
inventory.addItem(type, id);
|
||||
audio.play('itemPickup');
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ let onupdate = () => {};
|
|||
|
||||
export function init() {
|
||||
items.clear();
|
||||
addItem('connector', 'xheavy');
|
||||
}
|
||||
|
||||
export function getTiles() {
|
||||
|
|
|
@ -10,6 +10,14 @@ export function render() {
|
|||
if (graphics.trace) world.tracers.forEach(renderTracer);
|
||||
world.ships.forEach(renderShip);
|
||||
world.entities.forEach(renderEntity);
|
||||
|
||||
/*
|
||||
if (typeof window.q === 'undefined') window.q = [];
|
||||
q.forEach(p => {
|
||||
context.fillStyle = p[2];
|
||||
context.fillRect(p[0] - 0.05, p[1] - 0.05, 0.1, 0.1);
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
function renderParticle(particle) {
|
||||
|
|
|
@ -88,11 +88,17 @@ export function game() {
|
|||
};
|
||||
|
||||
|
||||
let invShadow = root();
|
||||
shadow.append(invShadow);
|
||||
invShadow.posRelative({x: 0, w: 0.4, h: 0.6});
|
||||
invShadow.x += 10;
|
||||
invShadow.y += 10;
|
||||
invShadow.h += 60;
|
||||
|
||||
let inventory = new GuiInventory(0, 0, 0, 0);
|
||||
shadow.append(inventory);
|
||||
inventory.posRelative({x: 0, y: 0, w: 0.4, h: 0.6});
|
||||
inventory.x += 10;
|
||||
inventory.y += 10;
|
||||
invShadow.append(inventory);
|
||||
inventory.posRelative({w: 1, h: 1});
|
||||
inventory.h -= 60;
|
||||
|
||||
edit.guiInventory = inventory;
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ export default class Body {
|
|||
return result;
|
||||
}
|
||||
|
||||
getWorldPoint(lx, ly) {
|
||||
getWorldPoint(lx, ly, test) {
|
||||
let [cx, cy] = this.localCom;
|
||||
let [nx, ny] = this.rotateVector(lx - cx, ly - cy, this.r);
|
||||
return [nx + this.x + cx, ny + this.y + cy];
|
||||
|
@ -55,8 +55,8 @@ export default class Body {
|
|||
}
|
||||
|
||||
rotateVector(x, y, r = this.r) {
|
||||
return [(x * Math.cos(r) - y * Math.sin(r)),
|
||||
(y * Math.cos(r) - x * Math.sin(r))];
|
||||
return [(x * Math.cos(-r) + y * Math.sin(-r)),
|
||||
-(-y * Math.cos(-r) + x * Math.sin(-r))];
|
||||
}
|
||||
|
||||
// TODO: Remove and replace uses with `rotateVector`.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as sector from './sector.mjs';
|
||||
import * as spawn from './spawn.mjs';
|
||||
import * as graphics from '../graphics/index.mjs';
|
||||
|
||||
export {getSectorFromWorld, getContainedSectors} from './sector.mjs';
|
||||
|
||||
|
@ -31,5 +32,5 @@ export function tick() {
|
|||
celestials.forEach(c => c.tick());
|
||||
entities.forEach(e => e.tick());
|
||||
ships.forEach(s => s.tick());
|
||||
tracers.forEach(t => t.tick());
|
||||
if (graphics.trace) tracers.forEach(t => t.tick());
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ export default class Ship extends Body {
|
|||
}
|
||||
|
||||
tick() {
|
||||
window.q = [];
|
||||
if (!state.editing) this.tickMotion();
|
||||
if (!this.landed) this.tickGravity(world.celestials);
|
||||
if (!state.editing) this.resolveCollisions();
|
||||
|
@ -109,19 +110,29 @@ export default class Ship extends Body {
|
|||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
window.q.push([...this.com, 'green']);
|
||||
}
|
||||
|
||||
resolveCelestialCollision(pos, cel) {
|
||||
let celToCom = this.angleTo(...this.com, ...cel.com);
|
||||
let celToPoc = this.angleTo(...pos, ...cel.com);
|
||||
let pocToCom = this.angleTo(...this.com, ...pos);
|
||||
let shipAngle = this.r + Math.PI / 2;
|
||||
|
||||
window.q.push([...pos, 'blue']);
|
||||
|
||||
let turnAngle = this.angleDifference(celToPoc, pocToCom);
|
||||
let checkAngle = this.angleDifference(celToPoc, this.r + Math.PI / 2);
|
||||
let checkAngle = this.angleDifference(celToPoc, shipAngle);
|
||||
let correctionAngle = this.angleDifference(shipAngle, pocToCom);
|
||||
|
||||
let [force] = this.rotateVector(0, 1, turnAngle);
|
||||
|
||||
if (Math.abs(checkAngle) < consts.TIP_ANGLE) force *= -1;
|
||||
if (Math.abs(checkAngle) < consts.TIP_ANGLE) {
|
||||
[force] = this.rotateVector(0, 1, correctionAngle);
|
||||
force *= 0.2;
|
||||
}
|
||||
|
||||
let canLand = Math.abs(checkAngle) < 0.03
|
||||
&& Math.abs(this.rvel) < 0.001;
|
||||
|
@ -131,7 +142,7 @@ export default class Ship extends Body {
|
|||
this.rvel = 0;
|
||||
this.r = celToCom - Math.PI / 2;
|
||||
}
|
||||
|
||||
|
||||
this.rvel += force * consts.TIP_SPEED;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,9 +11,11 @@ export function player() {
|
|||
ship.addModule(0, 0, modules.capsule.small);
|
||||
ship.addModule(0, 1, modules.fuel.small);
|
||||
ship.addModule(0, 2, modules.thruster.light);
|
||||
//ship.addModule(1, 2, modules.thruster.light);
|
||||
//ship.addModule(-1, 2, modules.thruster.light);
|
||||
world.ships.add(ship);
|
||||
world.setPlayerShip(ship);
|
||||
|
||||
|
||||
let tracer = new Tracer(ship);
|
||||
world.tracers.add(tracer);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue