diff --git a/main.js b/main.js index 192a458..09df882 100644 --- a/main.js +++ b/main.js @@ -3,7 +3,7 @@ import {createCanvas, drawLine, drawRect, drawText, loop} from './gfx.js'; import {key} from './input.js'; import {clamp, deg2rad, parable, randomInt, vec, wrap} from './math.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 {WEAPON_TYPES, EXPLOSION_TYPES} from './weapons.js'; import {sample} from './utils.js'; @@ -53,15 +53,20 @@ function update() { else if (state === 'aim') { const player = players[currentPlayer]; const {x, y, a, p, energy} = player; + const maxPower = energy * PLAYER_ENERGY_POWER_MULTIPLIER; if (key('ArrowLeft')) { player.a = wrap(0, a -1, 180); + if (a % 2 === 0) playTickSound(); } else if (key('ArrowRight')) { player.a = wrap(0, a +1, 180); + if (a % 2 === 0) playTickSound(); } 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')) { - 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(' ')) { state = 'fire'; const [px, py] = vec(x, y-3, a+180, 5); diff --git a/sound.js b/sound.js index bb42850..5a6d9f3 100644 --- a/sound.js +++ b/sound.js @@ -11,6 +11,14 @@ export function createOsc() { 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() { const osc = createOsc(); osc.start(); diff --git a/weapons.js b/weapons.js index 4c1f330..c35c41d 100644 --- a/weapons.js +++ b/weapons.js @@ -66,7 +66,7 @@ export const EXPLOSION_TYPES = { const {x, y, cr, osc} = explosion; const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0; 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); }, clip(explosion, terrain) {