| despawn: createImage("imgs/bullet_despawn.png") | despawn: createImage("imgs/bullet_despawn.png") | ||||
| }; | }; | ||||
| let BulletSounds = { | let BulletSounds = { | ||||
| spawn: "sounds/bullet_spawn.wav", | |||||
| despawn: "sounds/bullet_despawn.wav" | |||||
| spawn: {url: "sounds/bullet_spawn.wav", vol: 0.5}, | |||||
| despawn: {url: "sounds/bullet_despawn.wav", vol: 1} | |||||
| }; | }; | ||||
| class Bullet extends Entity { | class Bullet extends Entity { | ||||
| despawn: createImage("imgs/player_despawn.png") | despawn: createImage("imgs/player_despawn.png") | ||||
| }; | }; | ||||
| let PlayerSounds = { | let PlayerSounds = { | ||||
| despawn: "sounds/player_despawn.wav", | |||||
| thrust: "sounds/player_thrust.wav" | |||||
| despawn: {url: "sounds/player_despawn.wav", vol: 1.5}, | |||||
| thrust: {url: "sounds/player_thrust.wav", vol: 1} | |||||
| }; | }; | ||||
| class Player extends Entity { | class Player extends Entity { | ||||
| if (this.id === game.id) { | if (this.id === game.id) { | ||||
| this.thrustSound = document.createElement("audio"); | this.thrustSound = document.createElement("audio"); | ||||
| this.thrustSound.src = PlayerSounds.thrust; | |||||
| this.thrustSound.src = PlayerSounds.thrust.url; | |||||
| this.thrustSound.loop = true; | this.thrustSound.loop = true; | ||||
| this.thrustSound.play(); | this.thrustSound.play(); | ||||
| this.thrustSound.volume = 0; | this.thrustSound.volume = 0; | ||||
| return new Bullet(obj.pos.x, obj.pos.y, obj.vel, obj.id, obj.ownerId, game); | return new Bullet(obj.pos.x, obj.pos.y, obj.vel, obj.id, obj.ownerId, game); | ||||
| } else { | } else { | ||||
| throw new Error("Unknown entity type: "+obj.type); | throw new Error("Unknown entity type: "+obj.type); | ||||
| return false; | |||||
| } | } | ||||
| } | } | ||||
| }; | }; | ||||
| } | } | ||||
| playSound(url, pos) { | |||||
| playSound(s, pos) { | |||||
| let player = this.entities[this.id]; | let player = this.entities[this.id]; | ||||
| if (!player) | if (!player) | ||||
| return; | return; | ||||
| let dist = player.pos.clone().sub(pos); | let dist = player.pos.clone().sub(pos); | ||||
| let sound = document.createElement("audio"); | let sound = document.createElement("audio"); | ||||
| sound.src = url; | |||||
| sound.volume = Math.max(1 - (dist.length() / 1000), 0); | |||||
| sound.src = s.url; | |||||
| sound.volume = Math.max(1 - (dist.length() / 1000), 0) * s.vol; | |||||
| sound.play(); | sound.play(); | ||||
| } | } | ||||
| } | } |