Skip to content
Browse files

Move grid code into Grid class.

  • Loading branch information...
1 parent f5002cc commit f7b1bb2356f8e5577885cd2a8ead85265a95e88c @jimmycuadra committed Nov 22, 2012
View
64 source/javascripts/application.js.coffee
@@ -13,17 +13,14 @@
class ph.Application
constructor: (rowCount, columnCount) ->
@store = new ph.Store(amplify.store("pushing-hands"))
- @setUpGrid(rowCount, columnCount)
+ @grid = new ph.Grid
+ rowCount: rowCount,
+ columnCount: columnCount,
+ app: this
@setUpSounds()
@setUpHUD()
- @on("push", @markMatches)
$("#loading").remove()
- setUpGrid: (rowCount, columnCount) ->
- rows = @generateRows(rowCount, columnCount)
- @grid = new ph.CellRowsView(rows: rows)
- @grid.render()
-
setUpSounds: ->
@music = new ph.MusicPlayer {}, collection: new ph.Sounds [
new ph.Sound(name: "relaxing")
@@ -46,59 +43,6 @@ class ph.Application
hud.append(@musicPlayer.render().el)
hud.append(@settings.render().el)
- generateRows: (rowCount, columnCount) ->
- upperNeighbors = []
-
- rows = for i in [0...rowCount]
- collection = for j in [0...columnCount]
- if i > 0
- upperNeighbor = upperNeighbors[j]
- upperNeighbors[j] = new ph.Cell(upperNeighbor: upperNeighbor)
- new ph.CellRow(collection)
-
- markMatches: (chain) ->
- chain or= 1
- score = 0
- marked = []
-
- for row, rowIndex in @grid.rows[0..@grid.rows.length - 3]
- for cell, columnIndex in row.models
- tempMarked = [cell]
- color = cell.get("color")
- i = 1
- nextRow = @grid.rows[rowIndex + i]
- break unless nextRow
- nextCell = nextRow.at(columnIndex)
- while nextCell.get("color") is color
- tempMarked.push(nextCell)
- i++
- nextRow = @grid.rows[rowIndex + i]
- break unless nextRow
- nextCell = nextRow.at(columnIndex)
- if tempMarked.length >= 3
- marked.push.apply(marked, tempMarked)
- score += (3 + (tempMarked.length - 3) * 2) * chain
-
- return if score is 0
-
- @store.set("score", @store.get("score") + score)
- @store.set("chain", chain) if chain > @store.get("chain")
-
- # Clear matches
-
- ph.app.sfx.trigger("play", "match")
- _.each marked, (cell) ->
- cell.trigger("clear")
-
- # Refill cleared blocks
-
- ph.app.sfx.trigger("play", "fill")
- @grid.refill()
-
- # Check for more matches
-
- ph.app.trigger("push", chain + 1)
-
_.extend(ph.Application.prototype, Backbone.Events)
$ ->
View
4 source/javascripts/models/cell.js.coffee
@@ -12,9 +12,7 @@ class ph.Cell extends Backbone.Model
index = Math.floor(Math.random() * colors.length)
@set("color", colors[index])
- @on("clear", @clear)
-
- clear: =>
+ clear: ->
@set("color", null)
isClear: ->
View
60 source/javascripts/models/grid.js.coffee
@@ -0,0 +1,60 @@
+class ph.Grid
+ constructor: (options) ->
+ {@rowCount, @columnCount, @app} = options
+
+ @cellRowsView = new ph.CellRowsView(rows: @generateRows())
+ @cellRowsView.render()
+
+ generateRows: ->
+ upperNeighbors = []
+
+ rows = for i in [0...@rowCount]
+ collection = for j in [0...@columnCount]
+ if i > 0
+ upperNeighbor = upperNeighbors[j]
+ upperNeighbors[j] = new ph.Cell(upperNeighbor: upperNeighbor)
+ new ph.CellRow(collection)
+
+ markMatches: (chain) ->
+ chain or= 1
+ score = 0
+ marked = []
+
+ for row, rowIndex in @cellRowsView.rows[0..(@cellRowsView.rows.length - 3)]
+ for cell, columnIndex in row.models
+ tempMarked = [cell]
+ color = cell.get("color")
+ i = 1
+ nextRow = @cellRowsView.rows[rowIndex + i]
+ break unless nextRow
+ nextCell = nextRow.at(columnIndex)
+ while nextCell.get("color") is color
+ tempMarked.push(nextCell)
+ i++
+ nextRow = @cellRowsView.rows[rowIndex + i]
+ break unless nextRow
+ nextCell = nextRow.at(columnIndex)
+ if tempMarked.length >= 3
+ marked.push.apply(marked, tempMarked)
+ score += (3 + (tempMarked.length - 3) * 2) * chain
+
+ return if score is 0
+
+ @app.store.set("score", @app.store.get("score") + score)
+ @app.store.set("chain", chain) if chain > @app.store.get("chain")
+
+ # Clear matches
+
+ @app.sfx.trigger("play", "match")
+ _.each marked, (cell) ->
+ cell.clear()
+
+ # Refill cleared blocks
+
+ @app.sfx.trigger("play", "fill")
+ _.each @cellRowsView.rows.slice().reverse(), (row) ->
+ row.refill()
+
+ # Check for more matches
+
+ @markMatches(chain + 1)
View
2 source/javascripts/views/cell_row_view.js.coffee
@@ -26,4 +26,4 @@ class ph.CellRowView extends Backbone.View
nextColor = cell.get("color")
cell.set("color", newColor)
ph.app.sfx.trigger("play", "push")
- ph.app.trigger("push")
+ ph.app.grid.markMatches()
View
4 source/javascripts/views/cell_rows_view.js.coffee
@@ -8,7 +8,3 @@ class ph.CellRowsView extends Backbone.View
view = new ph.CellRowView(collection: row)
@$el.append(view.render().el)
this
-
- refill: ->
- _.each @rows.slice().reverse(), (row) ->
- row.refill()

0 comments on commit f7b1bb2

Please sign in to comment.
Something went wrong with that request. Please try again.