Permalink
Browse files

Fix bugs in refill algorithm.

  • Loading branch information...
1 parent c3b46a5 commit 710b9ad72abed285a1fce933b452e3afb4a3010f @jimmycuadra committed Nov 22, 2012
@@ -66,11 +66,15 @@ class ph.Application
tempMarked = [cell]
color = cell.get("color")
i = 1
- nextCell = @grid.rows[rowIndex + i].at(columnIndex)
+ nextRow = @grid.rows[rowIndex + i]
+ break unless nextRow
+ nextCell = nextRow.at(columnIndex)
while nextCell.get("color") is color
tempMarked.push(nextCell)
i++
- nextCell = @grid.rows[rowIndex + i].at(columnIndex)
+ 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
@@ -80,20 +84,20 @@ class ph.Application
@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")
- # Forced delay to keep the sounds from overlapping. Find a better solution
- # for this.
- setTimeout(
- ->
- ph.app.sfx.trigger("play", "fill")
- _.each marked.slice().reverse(), (cell) ->
- cell.trigger("refill")
- ph.app.trigger("push", chain + 1)
- 250
- )
+ # 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)
@@ -1,2 +1,6 @@
class ph.CellRow extends Backbone.Collection
model: ph.Cell
+
+ refill: ->
+ @each (cell) ->
+ cell.refill()
@@ -13,12 +13,16 @@ class ph.Cell extends Backbone.Model
@set("color", colors[index])
@on("clear", @clear)
- @on("refill", @refill)
clear: =>
@set("color", null)
+ isClear: ->
+ !@get("color")
+
refill: =>
+ return unless @isClear()
+
upperNeighbor = @get("upperNeighbor")
color = if upperNeighbor
@@ -33,7 +37,7 @@ class ph.Cell extends Backbone.Model
upperNeighbor = @get("upperNeighbor")
if color
- @clear()
+ @trigger("clear")
color
else if upperNeighbor
upperNeighbor.fallingBlockColor()
@@ -8,3 +8,7 @@ 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 710b9ad

Please sign in to comment.