mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-04-25 21:43:47 -04:00
Add thang sound panning for #454
This commit is contained in:
parent
aa3167f303
commit
99f43028a0
3 changed files with 22 additions and 3 deletions
app/lib
|
@ -38,6 +38,7 @@ class AudioPlayer extends CocoClass
|
|||
constructor: () ->
|
||||
super()
|
||||
@ext = if createjs.Sound.getCapability('mp3') then '.mp3' else '.ogg'
|
||||
@camera = null
|
||||
@listenToSound()
|
||||
@createNewManifest()
|
||||
@soundsToPlayWhenLoaded = {}
|
||||
|
@ -51,6 +52,13 @@ class AudioPlayer extends CocoClass
|
|||
# So for now, we'll just load through SoundJS instead.
|
||||
createjs.Sound.on 'fileload', @onSoundLoaded
|
||||
|
||||
applyPanning: (options, pos) ->
|
||||
sup = @camera.worldToSurface pos
|
||||
svp = @camera.surfaceViewport
|
||||
pan = Math.max -1, Math.min 1, ((sup.x - svp.x) - svp.width / 2) / svp.width * 2
|
||||
# TODO: derive new volume from old one and distance ratio
|
||||
volume: options.volume, delay: options.delay, pan: pan
|
||||
|
||||
# PUBLIC LOADING METHODS
|
||||
|
||||
soundForDialogue: (message, soundTriggers) ->
|
||||
|
@ -78,8 +86,11 @@ class AudioPlayer extends CocoClass
|
|||
@preloadInterfaceSounds [name] unless filename of cache
|
||||
@soundsToPlayWhenLoaded[name] = volume
|
||||
|
||||
playSound: (name, volume=1, delay=0) ->
|
||||
instance = createjs.Sound.play name, {volume: (me.get('volume') ? 1) * volume, delay: delay}
|
||||
playSound: (name, volume=1, delay=0, pos=null) ->
|
||||
audioOptions = {volume: (me.get('volume') ? 1) * volume, delay: delay}
|
||||
unless @camera is null or pos is null
|
||||
audioOptions = @applyPanning audioOptions, pos
|
||||
instance = createjs.Sound.play name, audioOptions
|
||||
instance
|
||||
|
||||
# # TODO: load Interface sounds somehow, somewhere, somewhen
|
||||
|
|
|
@ -188,6 +188,13 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
|
|||
return 0 unless @thang.bobHeight
|
||||
@thang.bobHeight * (1 + Math.sin(@age * Math.PI / @thang.bobTime))
|
||||
|
||||
getWorldPosition: ->
|
||||
p1 = @thang.pos
|
||||
if bobOffset = @getBobOffset()
|
||||
p1 = p1.copy?() or _.clone(p1)
|
||||
p1.z += bobOffset
|
||||
x: p1.x, y: p1.y, z: if @thang.isLand then 0 else p1.z - @thang.depth / 2
|
||||
|
||||
updatePosition: ->
|
||||
return unless @thang?.pos and @options.camera?
|
||||
[p0, p1] = [@lastPos, @thang.pos]
|
||||
|
@ -477,6 +484,6 @@ module.exports = CocoSprite = class CocoSprite extends CocoClass
|
|||
return null unless sound
|
||||
delay = if withDelay and sound.delay then 1000 * sound.delay / createjs.Ticker.getFPS() else 0
|
||||
name = AudioPlayer.nameForSoundReference sound
|
||||
instance = AudioPlayer.playSound name, volume, delay
|
||||
instance = AudioPlayer.playSound name, volume, delay, @getWorldPosition()
|
||||
# console.log @thang?.id, "played sound", name, "with delay", delay, "volume", volume, "and got sound instance", instance
|
||||
instance
|
||||
|
|
|
@ -360,6 +360,7 @@ module.exports = Surface = class Surface extends CocoClass
|
|||
canvasHeight = parseInt(@canvas.attr('height'), 10)
|
||||
@camera?.destroy()
|
||||
@camera = new Camera canvasWidth, canvasHeight
|
||||
AudioPlayer.camera = @camera
|
||||
@layers.push @surfaceLayer = new Layer name: "Surface", layerPriority: 0, transform: Layer.TRANSFORM_SURFACE, camera: @camera
|
||||
@layers.push @surfaceTextLayer = new Layer name: "Surface Text", layerPriority: 1, transform: Layer.TRANSFORM_SURFACE_TEXT, camera: @camera
|
||||
@layers.push @screenLayer = new Layer name: "Screen", layerPriority: 2, transform: Layer.TRANSFORM_SCREEN, camera: @camera
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue