Switch to Howler for audio

This commit is contained in:
asraelite 2018-03-07 12:21:43 +00:00
parent 3eb74e44aa
commit 194c0bf846
13 changed files with 78 additions and 19 deletions

BIN
dist/audio/rocket2.mp3 vendored Normal file

Binary file not shown.

BIN
dist/audio/rocket2.ogg vendored Normal file

Binary file not shown.

1
dist/index.html vendored
View file

@ -3,6 +3,7 @@
<head>
<script src="improcket.min.js"></script>
<script src="howler.min.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="icon" href="/img/favicon.ico">
</head>

View file

@ -38,7 +38,8 @@ export const audio = {
itemPickup: 'up1.mp3',
fuelPickup: 'blip2.mp3',
endEdit: 'release1.mp3',
newPlanet: 'up2.mp3'
newPlanet: 'up2.mp3',
engine: 'rocket2.ogg'
};
export async function init() {
@ -56,9 +57,11 @@ export async function init() {
return img;
});
parse(audio, str => {
let audio = new Audio('audio/' + str);
let audio = new Howl({
src: ['audio/' + str]
});
promises.push(new Promise((res, rej) => {
audio.addEventListener('canplaythrough', res);
audio.once('load', res);
}));
return audio;
});

View file

@ -1,5 +1,23 @@
import {audio} from '../assets.mjs';
const playing = new Map();
export function play(name) {
audio[name].cloneNode(true).play();
audio[name].play();
}
export function start(name) {
if (!playing.has(name))
playing.set(name, audio[name]);
let howl = playing.get(name);
howl.loop(true);
howl.play();
}
export function stop(name) {
if (!playing.has(name)) return;
let howl = playing.get(name);
if (howl.playing())
howl.stop();
}

View file

@ -2,6 +2,7 @@ import * as input from '../input.mjs';
import * as events from './events.mjs';
import * as graphics from '../graphics/index.mjs';
import * as inventory from './inventory.mjs';
import * as audio from './audio.mjs';
import {playerShip} from '../world/index.mjs';
import {state} from './index.mjs';
@ -38,6 +39,12 @@ export function tick() {
function tickPlaying() {
if (held[mapping.thrust]) {
playerShip.applyThrust({ forward: 1 });
} else {
audio.stop('engine');
}
if (pressed[mapping.thrust]) {
audio.start('engine');
}
if (held[mapping.left]) {

View file

@ -45,7 +45,7 @@ export function landShip(planet) {
}
function newPlanet(planet) {
let value = (planet.radius + 30) | 0;
let value = (planet.radius * 2 + 50) | 0;
landedPlanets.add(planet);
audio.play('newPlanet');
score += value;
@ -70,6 +70,7 @@ export function toggleEdit() {
export function toggleTrace() {
let trace = graphics.toggleTrace();
notify('Path prediction: ' + (trace ? 'on' : 'off'));
audio.start('engine');
}
export function toggleMarkers() {

View file

@ -24,7 +24,7 @@ export async function init() {
gui.init();
input.init();
events.startGame();
//events.startGame();
//tick(); return;

View file

@ -11,6 +11,9 @@ let onupdate = () => {};
export function init() {
items.clear();
addItem('connector', 'xheavy');
addItem('connector', 'xheavy');
addItem('connector', 'xheavy');
addItem('connector', 'xheavy');
}
export function getTiles() {

View file

@ -24,8 +24,13 @@ export function init() {
perspective = new Perspective();
draw.text('Loading...', canvas.width / 2, canvas.height / 2,
{ align: 'center', valign: 'middle' });
context.fillStyle = '#000';
context.fillRect(0, 0, canvas.width, canvas.height);
context.font = '36px Consolas';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = '#fff';
context.fillText('Loading...', canvas.width / 2, canvas.height / 2);
}
export function render() {

View file

@ -114,9 +114,10 @@ export default class Body {
this.gravity = true;
let speed = Math.sqrt(G * cel.mass / (altitude + cel.radius));
let [cx, cy] = cel.com;
let [comX, comY] = this.localCom;
let [dx, dy] = this.rotateVector(0, -(altitude + cel.radius), angle);
[this.xvel, this.yvel] = this.rotateVector(speed, 0, angle);
this.x = cx + dx;
this.y = cy + dy;
this.x = cx + dx - comX;
this.y = cy + dy - comY;
}
}

View file

@ -38,6 +38,10 @@ export default class Entity extends Body {
return [this.x + this.width / 2, this.y + this.height / 2];
}
get localCom() {
return [this.width / 2, this.height / 2];
}
remove() {
entities.delete(this);
}

View file

@ -53,24 +53,39 @@ function spawnSector(x, y) {
spawnedSectors.add(`${x}.${y}`);
}
function randomPlanet(x, y) {
let rad = Math.random() * 60 + 30;
function randomPlanet(x, y, {
radius = Math.random() * 60 + 30,
type = 'green',
density = 3
} = {}) {
let [cel, dis] = nearest(x, y, world.celestials);
let mcs = consts.MIN_CELESTIAL_SPACING;
if (dis < Math.max(rad, cel.radius) * mcs) return;
if (cel !== null && dis < Math.max(radius, cel.radius) * mcs) return;
let planet = celestial(x, y, rad, {
density: 3,
type: 'green'
let planet = celestial(x, y, radius, {
density: density,
type: type
});
for (let i = 1.5; i < 8; i += 1) {
for (let i = 0.1; i < 4; i += 1) {
if (Math.random() > consts.ENTITY_SPAWN_RATE) {
let e = randomEntity();
e.orbit(planet, i * rad, Math.random() * Math.PI * 2);
e.orbit(planet, i * radius, Math.random() * Math.PI * 2);
}
}
for (let i = 0; i < 5; i++) {
if (Math.random() > consts.ENTITY_SPAWN_RATE || true) {
let e = randomEntity();
e.orbit(planet, 1.5, Math.random() * Math.PI * 2);
e.gravity = false;
e.halt();
}
}
return planet;
}
function randomEntity(x, y) {
@ -107,7 +122,8 @@ export function player() {
}
export function startPlanet() {
return celestial(0, 0, 40, {
return randomPlanet(0, 0, {
radius: 40,
density: 3,
type: 'green'
});