From 1ca62262831f616ed0d04c5d70068192e4ad5931 Mon Sep 17 00:00:00 2001 From: monsterkodi Date: Sun, 21 Aug 2016 15:50:24 +0200 Subject: [PATCH] sound --- coffee/player.coffee | 1 - coffee/sound.coffee | 47 ++++++++++++++++++++++++-------------------- coffee/world.coffee | 5 +++++ 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/coffee/player.coffee b/coffee/player.coffee index c708e0c..7078b07 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -68,7 +68,6 @@ class Player extends Bot @projection.setPosition @projection.getPosition().mul(1.0-posDelta).plus camPos.mul posDelta if @look_angle # player is looking up or down - log "look_angle #{@look_angle}" @projection.setXVector playerDir.cross(playerUp).normal() rot = Quaternion.rotationAroundVector @look_angle, @projection.getXVector() @projection.setYVector rot.rotate playerUp diff --git a/coffee/sound.coffee b/coffee/sound.coffee index ce31e10..4357dfc 100644 --- a/coffee/sound.coffee +++ b/coffee/sound.coffee @@ -9,8 +9,9 @@ Howler = require 'howler' Howl = Howler.Howl class Sound - - @sounds = + + @sounds = Object.create null + @files = BOT_MOVE: file: "bot_move.wav", volume: 0.2 BOT_JUMP: file: "bot_jump.wav", volume: 0.7 BOT_LAND: file: "bot_land.wav", volume: 0.7 @@ -45,25 +46,29 @@ class Sound GENERATOR_OFF: file: "generator_off.wav", volume: 1.0 MOTOR: file: "bomb_splitter.wav", volume: 1.0 + @init: -> + return if _.size @sounds + for k,v of @files + @sounds[k] = new Howl + src: ["#{__dirname}/../sound/#{v.file}"] + volume: v.volume + @sounds[k].pannerAttr + maxDistance: 10 + refDistance: 1 + rolloffFactor: 4 + distanceModel: 'exponential' + + @setMatrix: (m) -> + p = m.getPosition() + f = m.getZVector() + u = m.getYVector() + Howler.Howler.pos p.x, p.y, p.z + Howler.Howler.orientation f.x, f.y, f.z, u.x, u.y, u.z + @play: (sound, pos, time) -> - log "Sound.playSound #{sound} #{time}", pos - # Howler.volume 1 - howl = new Howl src: ["#{__dirname}/../sound/#{@sounds[sound].file}"] - if world.projection? - p = world.projection.getPosition() - Howler.Howler.pos p.x, p.y, p.z - f = world.projection.getZVector() - u = world.projection.getYVector() - Howler.Howler.orientation f.x, f.y, f.z, u.x, u.y, u.z - howl.pannerAttr - maxDistance: 10 - refDistance: 1 - rolloffFactor: 1 - distanceModel: 'exponential' - id = howl.play() - # log "sound id #{id}" - howl.volume @sounds[sound].volume, id - if pos? - howl.pos pos.x, pos.y, pos.z, id + pos ?= world.player?.current_position + # log "Sound.play #{sound} #{time}", pos + id = @sounds[sound].play() + @sounds[sound].pos pos.x, pos.y, pos.z, id if pos? module.exports = Sound diff --git a/coffee/world.coffee b/coffee/world.coffee index e725103..7981fea 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -138,6 +138,8 @@ class World extends Actor @initGlobal: () -> return if @levels? + + Sound.init() global.rot0 = Quaternion.rot_0 global.rotx90 = Quaternion.rot_90_X @@ -672,6 +674,9 @@ class World extends Actor when World.CAMERA_INSIDE then @projection = @player.getInsideProjection() when World.CAMERA_BEHIND then @projection = @player.getBehindProjection() when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection() + + Sound.setMatrix @projection + @player.setOpacity clamp 0, 1, @projection.getPosition().minus(@player.current_position).length()-0.4 @projection.apply @camera @sun.position.copy @camera.position