From ea89cbc2ccf1d77505a739e6fa8143105405dd1a Mon Sep 17 00:00:00 2001 From: zenoamaro Date: Wed, 26 Jun 2019 22:48:18 +0200 Subject: [PATCH] Explosion sounds --- main.js | 1 + math.js | 5 ++++- weapons.js | 35 ++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index e147a48..e6a74d3 100644 --- a/main.js +++ b/main.js @@ -105,6 +105,7 @@ function update() { if (!explosionType.update(explosion)) { explosionType.clip(explosion, terrain); + explosionType.stop(explosion); for (let player of players) { if (player.energy > 0) { player.energy -= explosionType.damage(explosion, player) || 0; diff --git a/math.js b/math.js index ed28c24..3eacde0 100644 --- a/math.js +++ b/math.js @@ -1,4 +1,3 @@ - export function index2coords(width, i) { return [i % width, Math.floor(i/width)]; } @@ -27,6 +26,10 @@ export function wrap(min, x, max) { return ((x-min + max-min) % (max-min)) + min; } +export function cycle(x, d) { + return Math.floor(x / d); +} + export function within(x, y, tx, ty, r) { return Math.abs(x-tx) < r && Math.abs(y-ty) < r; } diff --git a/weapons.js b/weapons.js index 746ea04..3e4a893 100644 --- a/weapons.js +++ b/weapons.js @@ -1,6 +1,7 @@ import {drawCircle} from './gfx.js'; import {clipTerrain} from './terrain.js'; -import {within} from './math.js'; +import {within, cycle} from './math.js'; +import {createOsc, audio} from './sound.js'; export function drawExplosion(ctx, x, y, r) { const color = 255 - (16 * (r % 16)); @@ -8,10 +9,10 @@ export function drawExplosion(ctx, x, y, r) { } export const WEAPON_TYPES = [ - {id:'baby-missile', name:'Baby Missile', explosion:{type:'blast', r:5}}, - {id:'missile', name:'Missile', explosion:{type:'blast', r:10}}, - {id:'baby-nuke', name:'Baby Nuke', explosion:{type:'blast', r:50}}, - {id:'nuke', name:'Nuke', explosion:{type:'blast', r:100}}, + // {id:'baby-missile', name:'Baby Missile', explosion:{type:'blast', r:5}}, + // {id:'missile', name:'Missile', explosion:{type:'blast', r:10}}, + // {id:'baby-nuke', name:'Baby Nuke', explosion:{type:'blast', r:50}}, + // {id:'nuke', name:'Nuke', explosion:{type:'blast', r:100}}, {id:'dirt', name:'Dirt', explosion:{type:'dirt', r:25}}, {id:'large-dirt', name:'Ton of Dirt', explosion:{type:'dirt', r:75}}, ]; @@ -20,15 +21,23 @@ export const EXPLOSION_TYPES = { blast: { create(spec, x, y) { const {r} = spec; - return {x, y, r, cr:0}; + const osc = createOsc(); + osc.start(); + return {x, y, r, cr:0, osc}; }, update(explosion) { return ++explosion.cr < explosion.r; }, draw(explosion, foreground) { - const {x, y, cr} = explosion; + const {x, y, cr, osc} = explosion; + const f = cycle(explosion.cr, 6) % 2 === 0 ? 220 : 440; + osc.frequency.setValueAtTime(f, audio.currentTime); drawExplosion(foreground, x, y, cr); }, + stop(explosion) { + const {osc} = explosion; + osc.stop(); + }, clip(explosion, terrain) { const {x, y, cr} = explosion; clipTerrain(terrain, (ctx) => drawExplosion(ctx, x, y, cr)); @@ -42,13 +51,21 @@ export const EXPLOSION_TYPES = { dirt: { create(spec, x, y) { const {r} = spec; - return {x, y, r, cr:0}; + const osc = createOsc(); + osc.start(); + return {x, y, r, cr:0, osc}; + }, + stop(explosion) { + const {osc} = explosion; + osc.stop(); }, update(explosion) { return ++explosion.cr < explosion.r; }, draw(explosion, foreground) { - const {x, y, cr} = explosion; + const {x, y, cr, osc} = explosion; + const f = explosion.cr % 2 === 0 ? 220 + explosion.cr : 0; + osc.frequency.setValueAtTime(f, audio.currentTime); drawExplosion(foreground, x, y, cr); }, clip(explosion, terrain) {