Explosion sounds

This commit is contained in:
zenoamaro committed 2019-06-26 22:48:31 +02:00
1 parent 512b256e2e
commit ea89cbc2cc
3 files changed
+31 -10

No files matched your search

+26 -9
View File
@@ -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) {