Key sounds

This commit is contained in:
zenoamaro committed 2019-06-27 01:07:10 +02:00
1 parent 2a3867d01b
commit 6dab8504fe
3 files changed
+17 -4

No files matched your search

+8 -3
View File
@@ -3,7 +3,7 @@ import {createCanvas, drawLine, drawRect, drawText, loop} from './gfx.js';
import {key} from './input.js'; import {key} from './input.js';
import {clamp, deg2rad, parable, randomInt, vec, wrap} from './math.js'; import {clamp, deg2rad, parable, randomInt, vec, wrap} from './math.js';
import {createSky} from './sky.js'; import {createSky} from './sky.js';
import {audio, createOsc} from './sound.js'; import {audio, createOsc, playTickSound} from './sound.js';
import {collapseTerrain, createTerrain, isTerrain, landHeight, closestLand} from './terrain.js'; import {collapseTerrain, createTerrain, isTerrain, landHeight, closestLand} from './terrain.js';
import {WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js'; import {WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js';
import {sample} from './utils.js'; import {sample} from './utils.js';
@@ -53,15 +53,20 @@ function update() {
else if (state === 'aim') { else if (state === 'aim') {
const player = players[currentPlayer]; const player = players[currentPlayer];
const {x, y, a, p, energy} = player; const {x, y, a, p, energy} = player;
const maxPower = energy * PLAYER_ENERGY_POWER_MULTIPLIER;
if (key('ArrowLeft')) { if (key('ArrowLeft')) {
player.a = wrap(0, a -1, 180); player.a = wrap(0, a -1, 180);
if (a % 2 === 0) playTickSound();
} else if (key('ArrowRight')) { } else if (key('ArrowRight')) {
player.a = wrap(0, a +1, 180); player.a = wrap(0, a +1, 180);
if (a % 2 === 0) playTickSound();
} else if (key('ArrowUp')) { } else if (key('ArrowUp')) {
player.p = clamp(0, p +1, energy * PLAYER_ENERGY_POWER_MULTIPLIER); player.p = clamp(0, p +2, maxPower);
if (p < maxPower && p % 4 === 0) playTickSound();
} else if (key('ArrowDown')) { } else if (key('ArrowDown')) {
player.p = clamp(0, p -1, energy * PLAYER_ENERGY_POWER_MULTIPLIER); player.p = clamp(0, p -2, maxPower);
if (p > 0 && p % 4 === 0) playTickSound();
} else if (key(' ')) { } else if (key(' ')) {
state = 'fire'; state = 'fire';
const [px, py] = vec(x, y-3, a+180, 5); const [px, py] = vec(x, y-3, a+180, 5);
+8
View File
@@ -11,6 +11,14 @@ export function createOsc() {
return osc; return osc;
} }
export function playTickSound() {
const osc = createOsc();
osc.frequency.setValueAtTime(440, audio.currentTime);
osc.frequency.setValueAtTime(0, audio.currentTime + 0.005);
osc.start();
osc.stop(audio.currentTime + 0.005);
}
function clickInit() { function clickInit() {
const osc = createOsc(); const osc = createOsc();
osc.start(); osc.start();
+1 -1
View File
@@ -66,7 +66,7 @@ export const EXPLOSION_TYPES = {
const {x, y, cr, osc} = explosion; const {x, y, cr, osc} = explosion;
const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0; const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0;
osc.frequency.setValueAtTime(f, audio.currentTime); osc.frequency.setValueAtTime(f, audio.currentTime);
osc.frequency.setValueAtTime(0, audio.currentTime+1); osc.frequency.setValueAtTime(0, audio.currentTime+0.1);
drawExplosion(foreground, x, y, cr); drawExplosion(foreground, x, y, cr);
}, },
clip(explosion, terrain) { clip(explosion, terrain) {