Permalink
Browse files

Add refill algorithm.

  • Loading branch information...
1 parent 4e798d6 commit 6aab20f78e51b119cbedc9f2608caa12e6bb5bcc @jimmycuadra committed Nov 22, 2012
@@ -74,11 +74,24 @@ class ph.Application
marked.push.apply(marked, tempMarked)
score += 3 + (tempMarked.length - 3) * 2
+ return if score is 0
+
@store.set("score", @store.get("score") + score)
+ ph.app.sfx.trigger("play", "match") if ph.app.store.get("playSoundEffects")
_.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") if ph.app.store.get("playSoundEffects")
+ _.each marked.slice().reverse(), (cell) ->
+ cell.trigger("refill")
+ 250
+ )
+
_.extend(ph.Application.prototype, Backbone.Events)
$ ->
@@ -1,6 +1,9 @@
class ph.Cell extends Backbone.Model
@COLORS = ["r", "g", "b", "o"]
+ @randomColor: ->
+ @COLORS[_.random(0, @COLORS.length - 1)]
+
initialize: ->
colors = @constructor.COLORS.slice()
upperNeighbor = @get("upperNeighbor")
@@ -10,6 +13,29 @@ class ph.Cell extends Backbone.Model
@set("color", colors[index])
@on("clear", @clear)
+ @on("refill", @refill)
clear: =>
@set("color", null)
+
+ refill: =>
+ upperNeighbor = @get("upperNeighbor")
+
+ color = if upperNeighbor
+ upperNeighbor.fallingBlockColor()
+ else
+ @constructor.randomColor()
+
+ @set("color", color)
+
+ fallingBlockColor: ->
+ color = @get("color")
+ upperNeighbor = @get("upperNeighbor")
+
+ if color
+ @clear()
+ color
+ else if upperNeighbor
+ upperNeighbor.fallingBlockColor()
+ else
+ @constructor.randomColor()
@@ -19,7 +19,7 @@ class ph.CellRowView extends Backbone.View
push: (flip) ->
models = @collection.models
if flip
- models = Array::reverse.call(models.slice())
+ models = models.slice().reverse()
nextColor = models[models.length - 1].get("color")
_.each models, (cell) =>
newColor = nextColor

0 comments on commit 6aab20f

Please sign in to comment.