Permalink
Browse files

Revamp sound system, .ogg all sounds w/ .mp3 alt

  • Loading branch information...
1 parent c0fae91 commit af1f0894ca9694ec67dbd8dbb6fdd43e0351a808 Daniel Mark Schwartz committed Nov 30, 2012
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
View
@@ -1,6 +1,10 @@
run: all
open index.html
+publish: all
+ rsync --verbose --progress --stats --compress --rsh=/usr/bin/ssh *.png *.ogg *.mp3 *.js *.html root@techno-monk.com:/var/www/techno-monk.com
+ open http://www.techno-monk.com
+
all: index.html atom.js game.js
atom.js: atom.coffee
View
@@ -142,55 +142,3 @@ class Game
atom.input.clearPressed()
atom.Game = Game
-
-## Audio
-
-# TODO: firefox support
-# TODO: streaming music
-
-atom.audioContext = new webkitAudioContext
-
-atom._mixer = atom.audioContext?.createGainNode()
-atom._mixer?.connect atom.audioContext.destination
-
-atom.loadSound = (url, callback) ->
- return callback? 'No audio support' unless atom.audioContext
-
- request = new XMLHttpRequest()
- request.open 'GET', url, true
- request.responseType = 'arraybuffer'
-
- request.onload = ->
- atom.audioContext.decodeAudioData request.response, (buffer) ->
- callback? null, buffer
- , (error) ->
- callback? error
-
- try
- request.send()
- catch e
- callback? e.message
-
-atom.sfx = {}
-atom.preloadSounds = (sfx, cb) ->
- return cb? 'No audio support' unless atom.audioContext
- # sfx is { name: 'url' }
- toLoad = 0
- for name, url of sfx
- toLoad++
- do (name, url) ->
- atom.loadSound "#{url}", (error, buffer) ->
- console.error error if error
- atom.sfx[name] = buffer if buffer
- cb?() unless --toLoad
-
-atom.playSound = (name, time = 0) ->
- return unless atom.sfx[name] and atom.audioContext
- source = atom.audioContext.createBufferSource()
- source.buffer = atom.sfx[name]
- source.connect atom._mixer
- source.noteOn time
- source
-
-atom.setVolume = (v) ->
- atom._mixer?.gain.value = v
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -57,31 +57,15 @@ imgs =
enlightenment: 'enlightenment.png'
enlightenment_blur: 'enlightenment_blur.png'
-preloadImages = (imgs) ->
- atom.images = {}
- numImages = 0
- for name, url of imgs
- numImages++
- img = new Image
- img.src = url
- img.onload = () ->
- numImages--
- if numImages <= 0
- game.loadedGraphics = true
- atom.images[name] = img
-
-preloadImages imgs
-
+
sounds =
intro: 'DST-ClubNight.mp3'
level1: 'DST-SpaceBuddha.mp3'
- credits: 'DST-EndingCredits.mp3'
- hum: 'hum.wav'
- explosion0: 'explosion0.wav'
- explosion1: 'explosion1.wav'
-
-atom.preloadSounds sounds, (x) -> game.loadedSounds = true
+ #credits: 'DST-EndingCredits.mp3'
+ hum: 'hum.mp3'
+ explosion0: 'explosion0.mp3'
+ explosion1: 'explosion1.mp3'
handleEvent = (e, dt, s) ->
#Time prefixes
@@ -125,8 +109,6 @@ handleEvent = (e, dt, s) ->
class Scene
constructor: (events) ->
@events = clone(events)
- @events.sort (x,y) ->
- x.t - y.t
@ship = undefined
@explosions = []
@bullets = []
@@ -396,13 +378,13 @@ moveRandom = (speed = 50, duration = [.2, 2], padx = [50,50], pady = [100, 250])
[x,y]
bulletStraight = (dx=0, dy=100) ->
- {image: atom.images.bullet, move: moveStraight dx, dy}
+ {image: game.images.bullet, move: moveStraight dx, dy}
bulletDirect = (s, e, speed = 100) ->
dx = s.ship?.x - e.x
dy = s.ship?.y - e.y
mag = speed / Math.sqrt(dx*dx + dy*dy)
- {image: atom.images.bullet, move: moveStraight mag * dx, mag * dy}
+ {image: game.images.bullet, move: moveStraight mag * dx, mag * dy}
fireEvery = (fireEvery=1) ->
t = 0
@@ -415,10 +397,10 @@ fireEvery = (fireEvery=1) ->
die =
normal: (e) ->
- atom.playSound 'explosion0'
+ game.soundOn 'explosion0'
game.scene.spawnExplosion e.x, e.y
massive: (e) ->
- atom.playSound 'explosion1'
+ game.soundOn 'explosion1'
for i in [0 .. 15]
game.scene.spawnExplosion atom.width * randNorm() + e.x - atom.width/2, atom.height * randNorm() + e.y - atom.height/2
@@ -427,7 +409,7 @@ enemy =
move: moveStraight dx, dy
fire: fireEvery fireRate
bullet: [() -> bulletStraight()]
- animate: new AnimationRotate atom.images.eye, 2
+ animate: new AnimationRotate game.images.eye, 2
health: 500
die: die.normal
circle: (n=6, speed=100)->
@@ -440,7 +422,7 @@ enemy =
bulletStraight speed * Math.cos(theta), speed * Math.sin(theta)
)
)(i, 1)
- animate: new AnimationRotate atom.images.wheel
+ animate: new AnimationRotate game.images.wheel
health: 1000
die: die.normal
fan: (dx, dy, n=6, speed=100)->
@@ -455,14 +437,14 @@ enemy =
theta = 2 * Math.PI * (i + .5*odd) / n
bulletStraight speed * Math.cos(theta), speed * Math.sin(theta)
)
- animate: new AnimationRotate atom.images.head, .25
+ animate: new AnimationRotate game.images.head, .25
health: 1000
die: die.normal
direct: (dx, dy, g) ->
move: moveStraightGravity dx, dy, g
fire: fireEvery 1
bullet: [(dt, s, e) -> bulletDirect s, e]
- animate: new Animation atom.images.eyeGreen
+ animate: new Animation game.images.eyeGreen
health: 1250
die: die.normal
@@ -487,7 +469,7 @@ enemy =
bulletStraight dx, dy
]
- animate: new Animation atom.images.buddha1
+ animate: new Animation game.images.buddha1
health: 20000
die: die.massive
@@ -542,9 +524,9 @@ class Ship
@do = 0
@o = 1
- @body = new AnimationBlink atom.images.monk
+ @body = new AnimationBlink game.images.monk
- @lotus = new AnimationRotateScale atom.images.lotus
+ @lotus = new AnimationRotateScale game.images.lotus
@lotusY = -33
@lotusX = 5
@@ -554,7 +536,7 @@ class Ship
@beamSpace = 6
collide: ->
- atom.playSound 'explosion0'
+ game.soundOn 'explosion0'
game.scene.spawnExplosion @x, @y
if not game.immune
game.scene.fadeOut 1, () -> game.sceneChange new Scene title
@@ -674,7 +656,7 @@ class BackgroundCredits
ctx.fillStyle = 'white'
ctx.fillRect 0, 0, atom.width, atom.height
- i = atom.images.title_monk
+ i = game.images.title_monk
x = atom.width/2 + 20
y = atom.height - i.height / 2 - 95
ctx.globalAlpha = @en_alpha * .6 + .4
@@ -773,20 +755,20 @@ class BackgroundTitle
do ctx.fill
if @tm_light
- i = atom.images.tm_light
+ i = game.images.tm_light
else
- i = atom.images.tm_dark
+ i = game.images.tm_dark
ctx.drawImage i, atom.width/2 - i.width/2, 10
- i = atom.images.enlightenment_blur
+ i = game.images.enlightenment_blur
ctx.globalAlpha = @en_alpha
- ctx.drawImage i, atom.width/2 - i.width/2, atom.images.tm_dark.height + 20
+ ctx.drawImage i, atom.width/2 - i.width/2, game.images.tm_dark.height + 20
- i = atom.images.enlightenment
+ i = game.images.enlightenment
ctx.globalAlpha = 1
- ctx.drawImage i, atom.width/2 - i.width/2, atom.images.tm_dark.height + 20
+ ctx.drawImage i, atom.width/2 - i.width/2, game.images.tm_dark.height + 20
- i = atom.images.title_monk
+ i = game.images.title_monk
ctx.drawImage i, atom.width/2 - i.width/2, atom.height - i.height
if @press_show
@@ -840,6 +822,10 @@ class BackgroundStars
class Game extends atom.Game
constructor: (@scene) ->
super
+
+ @preloadImages imgs
+ @preloadSounds sounds
+
atom.input.bind atom.key.LEFT_ARROW, 'left'
atom.input.bind atom.key.RIGHT_ARROW, 'right'
atom.input.bind atom.key.UP_ARROW, 'up'
@@ -861,10 +847,6 @@ class Game extends atom.Game
@time_warp = 1
@immune = false
- @loadedSounds = false
- @loadedGraphics = false
- @playing = {}
-
update: (dt) ->
if atom.input.pressed 's'
@time_warp /= 2
@@ -904,26 +886,51 @@ class Game extends atom.Game
@scene_new = scene
musicPlay: (source = @music_source) ->
- if @music?
- @music.noteOff 0
- @music = atom.playSound source
- @music.loop = true
- @music_source = source
+ if @music_source == source
+ @music?.play()
+ else
+ @music?.pause()
+ @music?.currentTime = 0
+ @music = game.sounds[source]
+ @music.play()
+ @music.loop = true
+ @music_source = source
- musicStop: ->
- if @music?
- @music.noteOff 0
- @music = null
+ musicPause: ->
+ @music?.pause()
soundOn: (sound, gain = 1) ->
- if not @playing[sound]? or @playing[sound].playbackState == 3
- @playing[sound] = atom.playSound sound
- @playing[sound].loop = true
- @playing[sound].gain.value = gain
+ game.sounds[sound].play()
+ game.sounds[sound].volume = gain
soundOff: (sound) ->
- if @playing[sound]?
- @playing[sound].noteOff 0
+ game.sounds[sound]?.pause()
+
+ preloadImages: (imgs) ->
+ @images = {}
+ @imagesNum = 0
+ @imagesLoaded = 0
+ for name, url of imgs
+ @imagesNum++
+ img = new Image
+ img.src = url
+ img.onload = () -> game.imagesLoaded++
+ @images[name] = img
+
+ preloadSounds: (sounds) ->
+ @sounds = {}
+ @soundsNum = 0
+ @soundsLoaded = 0
+ for name, url of sounds
+ @soundsNum++
+ sound = new Audio
+ if sound.canPlayType 'audio/ogg'
+ sound.src = url + '.ogg'
+ else
+ sound.src = url
+ sound.preload = 'auto'
+ sound.addEventListener 'canplaythrough', (-> game.soundsLoaded++), false
+ @sounds[name] = sound
text =
enemy: ['Your worst enemy cannot','harm you as much as your','own unguarded thoughts.']
@@ -933,7 +940,7 @@ text =
credits = [
{do: (s) -> s.fadeIn 2},
{do: (s) -> s.background = new BackgroundCredits 'green'},
- {do: (s) -> game.musicPlay 'credits'},
+ {do: (s) -> game.musicPlay 'intro'},
{waitUntil: (s) -> atom.input.pressed 'space'},
{do: (s) -> s.fadeOut .5},
{wait: .5},
@@ -1016,11 +1023,12 @@ title = [
loading = [
{do: (s) -> s.background = new BackgroundFlat 'white'},
{
- repeatUntil: (s) -> game.loadedSounds and game.loadedGraphics,
+ repeatUntil: (s) -> game.imagesNum == game.imagesLoaded and game.soundsNum == game.soundsLoaded
draw: (ctx) ->
+ percent = Math.round((game.imagesLoaded + game.soundsLoaded)/(game.imagesNum + game.soundsNum)*100)
atom.context.font = '24px "Apple Chancery", Mistral'
- ctx.fillStyle = "blue"
- ctx.fillText 'Loading', 150, atom.height/2 - 24
+ ctx.fillStyle = "blue"
+ ctx.fillText "Loading: #{percent}%", 120, atom.height/2 - 24
},
{do: (s) -> game.sceneChange (new Scene title)},
]
@@ -1029,8 +1037,8 @@ game = new Game(new Scene loading)
window.onblur = ->
game.stop()
- game.musicStop()
- game.soundOff s for s,v of game.playing
+ game.musicPause()
+ game.soundOff s for s,v of game.sounds
window.onfocus = ->
game.run()
game.musicPlay()
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.

0 comments on commit af1f089

Please sign in to comment.