Browse Source

added sounds

master
mort 8 years ago
parent
commit
9347c212a1
6 changed files with 47 additions and 14 deletions
  1. 1
    1
      conf.js.example
  2. 46
    13
      es/game.js
  3. BIN
      sounds/bullet_despawn.wav
  4. BIN
      sounds/bullet_spawn.wav
  5. BIN
      sounds/player_despawn.wav
  6. BIN
      sounds/player_thrust.wav

+ 1
- 1
conf.js.example View File

window.conf = { window.conf = {
address: "ws://some-url.com:port" address: "ws://some-url.com:port"
}
};

+ 46
- 13
es/game.js View File

return Math.abs(n1 - n2); return Math.abs(n1 - n2);
} }


function createImage(url) {
var img = document.createElement("img");
img.src = url;
return img;
}

function background(ctx, camera, offset) { function background(ctx, camera, offset) {
if (!background.cache) { if (!background.cache) {
let cache = []; let cache = [];
let BulletImgs = { let BulletImgs = {
despawn: createImage("imgs/bullet_despawn.png") despawn: createImage("imgs/bullet_despawn.png")
}; };
let BulletSounds = {
spawn: "sounds/bullet_spawn.wav",
despawn: "sounds/bullet_despawn.wav"
};


class Bullet extends Entity { class Bullet extends Entity {
constructor(x, y, vel, id, ownerId, game) { constructor(x, y, vel, id, ownerId, game) {
super(x, y, 5, 5, id, game); super(x, y, 5, 5, id, game);
this.imgs = BulletImgs;
this.vel.set(vel.x, vel.y); this.vel.set(vel.x, vel.y);
this.ownerId = ownerId; this.ownerId = ownerId;

game.playSound(BulletSounds.spawn, this.pos);
} }


draw(ctx, selfId) { draw(ctx, selfId) {


despawn() { despawn() {
this.game.animate(new Animation({ this.game.animate(new Animation({
img: this.imgs.despawn,
img: BulletImgs.despawn,
x: this.pos.x, x: this.pos.x,
y: this.pos.y, y: this.pos.y,
width: 64, width: 64,
wsteps: 5, wsteps: 5,
hsteps: 5 hsteps: 5
})); }));
this.game.playSound(BulletSounds.despawn, this.pos);
} }
} }


thrust_back: createImage("imgs/player_thrust_back.png"), thrust_back: createImage("imgs/player_thrust_back.png"),
despawn: createImage("imgs/player_despawn.png") despawn: createImage("imgs/player_despawn.png")
}; };
let PlayerSounds = {
despawn: "sounds/player_despawn.wav",
thrust: "sounds/player_thrust.wav"
};


class Player extends Entity { class Player extends Entity {
constructor(x, y, id, rot, game) { constructor(x, y, id, rot, game) {
super(x, y, 25, 60, id, game); super(x, y, 25, 60, id, game);
this.imgs = PlayerImgs;
this.rot = rot; this.rot = rot;
this.rotVel = 0; this.rotVel = 0;
this.keys = {}; this.keys = {};
this.health = 0; this.health = 0;


this.thrustAnim = new Animation({ this.thrustAnim = new Animation({
img: this.imgs.thrust_back,
img: PlayerImgs.thrust_back,
x: this.pos.x, x: this.pos.x,
y: this.pos.y, y: this.pos.y,
width: 128, width: 128,
}); });
this.thrustAnim.visible = false; this.thrustAnim.visible = false;
game.animate(this.thrustAnim); game.animate(this.thrustAnim);

this.thrustSound = document.createElement("audio");
this.thrustSound.src = PlayerSounds.thrust;
this.thrustSound.loop = true;
this.thrustSound.play();
this.thrustSound.volume = 0;
} }


draw(ctx, selfId) { draw(ctx, selfId) {
super.update(dt); super.update(dt);
this.rot += this.rotVel * dt; this.rot += this.rotVel * dt;


if (this.keys.up)
if (this.keys.up) {
this.thrustAnim.visible = true; this.thrustAnim.visible = true;
else
if (this.keys.sprint)
this.thrustSound.volume = 1;
else
this.thrustSound.volume = 0.5;
} else {
this.thrustAnim.visible = false; this.thrustAnim.visible = false;
this.thrustSound.volume = 0;
}
} }


despawn() { despawn() {
this.game.animate(new Animation({ this.game.animate(new Animation({
img: this.imgs.despawn,
img: PlayerImgs.despawn,
x: this.pos.x, x: this.pos.x,
y: this.pos.y, y: this.pos.y,
width: 64, width: 64,
})); }));
this.thrustAnim.visible = false; this.thrustAnim.visible = false;
this.thrustAnim.loop = false; this.thrustAnim.loop = false;
this.game.playSound(PlayerSounds.despawn, this.pos);
} }
} }


} }
} }


function createImage(url) {
var img = document.createElement("img");
img.src = url;
return img;
}

export default class Game { export default class Game {
constructor(sock, canvas) { constructor(sock, canvas) {
this.sock = sock; this.sock = sock;
delete this.animations[i]; delete this.animations[i];
}; };
} }

playSound(url, pos) {
let player = this.entities[this.id];
let dist = player.pos.clone().sub(pos);
console.log(dist, dist.length());

let sound = document.createElement("audio");
sound.src = url;
sound.volume = Math.max(1 - (dist.length() / 1000), 0);
sound.play();
}
} }

BIN
sounds/bullet_despawn.wav View File


BIN
sounds/bullet_spawn.wav View File


BIN
sounds/player_despawn.wav View File


BIN
sounds/player_thrust.wav View File


Loading…
Cancel
Save