Permalink
Browse files

Inject dependencies in various classes.

  • Loading branch information...
1 parent 2f8f939 commit af5acb488696036bdcc96c79b5185bb6aba04fcc @jimmycuadra committed Nov 23, 2012
@@ -13,34 +13,46 @@
class ph.Application
constructor: (rowCount, columnCount) ->
@store = new ph.Store(amplify.store("pushing-hands"))
+
+ @setUpSounds()
+
@grid = new ph.Grid
- rowCount: rowCount,
- columnCount: columnCount,
+ rowCount: rowCount
+ columnCount: columnCount
app: this
- @setUpSounds()
+
@setUpHUD()
+
$("#loading").remove()
setUpSounds: ->
- @music = new ph.MusicPlayer {}, collection: new ph.Sounds [
+ music = new ph.Sounds [
new ph.Sound(name: "relaxing")
new ph.Sound(name: "tense")
- ]
+ ],
+ store: @store
+
+ @musicPlayer = new ph.MusicPlayer({}, collection: music)
+
@sfx = new ph.Sounds [
new ph.Sound(name: "push")
new ph.Sound(name: "match")
new ph.Sound(name: "fill")
- ]
- @music.play() if @store.get("autoPlayMusic")
+ ],
+ store: @store
+
+ @musicPlayer.play() if @store.get("autoPlayMusic")
setUpHUD: ->
@stats = new ph.StatsView(store: @store)
- @musicPlayer = new ph.MusicPlayerView(model: @music)
+
+ @musicPlayerView = new ph.MusicPlayerView(model: @musicPlayer)
+
@settings = new ph.SettingsView(model: @store)
hud = $("#hud")
hud.append(@stats.render().el)
- hud.append(@musicPlayer.render().el)
+ hud.append(@musicPlayerView.render().el)
hud.append(@settings.render().el)
_.extend(ph.Application.prototype, Backbone.Events)
@@ -1,11 +1,11 @@
class ph.Sounds extends Backbone.Collection
model: ph.Sound
- initialize: ->
- @on("play", @play, this)
+ initialize: (models, options) ->
+ {@store} = options
play: (name) ->
- return unless ph.app.store.get("playSoundEffects")
+ return unless @store.get("playSoundEffects")
sounds = @where(name: name)
_.each sounds, (sound) ->
@@ -1,8 +1,12 @@
class ph.Grid
constructor: (options) ->
- {@rowCount, @columnCount, @app} = options
+ {@rowCount, @columnCount, app} = options
+ {@sfx, @store} = app
- @cellRowsView = new ph.CellRowsView(rows: @generateRows())
+ @cellRowsView = new ph.CellRowsView
+ rows: @generateRows()
+ sfx: @sfx
+ grid: this
@cellRowsView.render()
generateRows: ->
@@ -48,21 +52,21 @@ class ph.Grid
@markMatches(chain + 1)
updateStats: (score, chain, callback) ->
- @app.store.set("score", @app.store.get("score") + score)
- @app.store.set("chain", chain) if chain > @app.store.get("chain")
+ @store.set("score", @store.get("score") + score)
+ @store.set("chain", chain) if chain > @store.get("chain")
setTimeout(callback, 250)
clear: (marked, callback) =>
- @app.sfx.trigger("play", "match")
+ @sfx.play("match")
_.each marked, (cell) ->
cell.clear()
setTimeout(callback, 250)
refill: (callback) ->
- @app.sfx.trigger("play", "fill")
+ @sfx.play("fill")
_.each @cellRowsView.rows.slice().reverse(), (row) ->
row.refill()
@@ -2,6 +2,7 @@ class ph.CellRowView extends Backbone.View
tagName: "tr"
initialize: ->
+ {@sfx, @grid} = @options
@collection.on("push", @push, this)
render: ->
@@ -17,13 +18,16 @@ class ph.CellRowView extends Backbone.View
this
push: (flip) ->
- models = @collection.models
- if flip
- models = models.slice().reverse()
+ models = if flip
+ @collection.models.slice().reverse()
+ else
+ @collection.models
+
nextColor = models[models.length - 1].get("color")
_.each models, (cell) =>
newColor = nextColor
nextColor = cell.get("color")
cell.set("color", newColor)
- ph.app.sfx.trigger("play", "push")
- ph.app.grid.markMatches()
+
+ @sfx.play("push")
+ @grid.markMatches()
@@ -1,10 +1,13 @@
class ph.CellRowsView extends Backbone.View
initialize: ->
- @rows = @options.rows
+ {@rows, @sfx, @grid} = @options
@setElement($("#grid"))
- render: ->
+ render: =>
_.each @rows, (row) =>
- view = new ph.CellRowView(collection: row)
+ view = new ph.CellRowView
+ collection: row
+ sfx: @sfx
+ grid: @grid
@$el.append(view.render().el)
this

0 comments on commit af5acb4

Please sign in to comment.