Skip to content

Commit

Permalink
Allow making of squares which remove all dots of the same color
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbunsch committed Oct 16, 2014
1 parent bc498fd commit b6075cf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/game.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Game
@level = options.level || new Level() @level = options.level || new Level()
@score = 0 @score = 0
@moves = @level.moves @moves = @level.moves
@dots = []


dispose: => dispose: =>
@level = null @level = null
Expand Down Expand Up @@ -87,6 +88,12 @@ class Game
column: column column: column
}) })
dot.draw() dot.draw()
@dots.push dot

removeDot: (dot) =>
index = @dots.indexOf(dot)
@dots.splice(index, 1)
dot.destroy()


module.exports = Game module.exports = Game


50 changes: 45 additions & 5 deletions app/move.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@ class Move


complete: => complete: =>
Crafty.removeEvent this, Crafty.stage.elem, "mouseup", this.complete Crafty.removeEvent this, Crafty.stage.elem, "mouseup", this.complete
if this.squarePresent()
this.addAllSameColoredDots()
this.destroyDots() this.destroyDots()
this.destroyAllLines() this.destroyAllLines()
this.updateGameState() this.updateGameState()
this.dispose() this.dispose()


squarePresent: =>
groupedDots = this.getGroupedDots()
for id, dots of groupedDots
return true if dots.length > 1
return false

addAllSameColoredDots: =>
uniqueDots = this.getUniqueDots()
@game.dots.forEach (dot) =>
if dot.color == @color and !uniqueDots[dot.id]
@dots.push(dot)

updateGameState: => updateGameState: =>
if @dots.length > 1 if @dots.length > 1
@game.addPoints(@dots.length * 10) dotCount = this.getUniqueDotCount()
@game.addPoints(dotCount * 10)
@game.subtractMove() @game.subtractMove()


dispose: => dispose: =>
Expand All @@ -30,22 +45,47 @@ class Move


destroyDots: => destroyDots: =>
if @dots.length > 1 if @dots.length > 1
replacements = []
uniqueDots = this.getUniqueDots() uniqueDots = this.getUniqueDots()
iterator = 0 iterator = {}
for id, dot of uniqueDots for id, dot of uniqueDots
iterator += 1 column = dot.column
dot.destroy() iterator[column] ||= 0
this.addReplacementDot(dot.column, iterator) iterator[column] += 1
this.removeDot(dot)
replacements.push({
column: column,
iterator: iterator[column]
})
replacements.forEach (data) =>
this.addReplacementDot(data.column, data.iterator)


addReplacementDot: (column, index) => addReplacementDot: (column, index) =>
@game.addDotAt(-index, column) @game.addDotAt(-index, column)


removeDot: (dot) =>
@game.removeDot(dot)

getUniqueDots: => getUniqueDots: =>
uniqueDots = {} uniqueDots = {}
for index, dot of @dots for index, dot of @dots
uniqueDots[dot.id] = dot uniqueDots[dot.id] = dot
uniqueDots uniqueDots


getGroupedDots: =>
uniqueDots = {}
for index, dot of @dots
uniqueDots[dot.id] ||= []
uniqueDots[dot.id].push(dot)
uniqueDots

getUniqueDotCount: =>
uniqueDots = this.getUniqueDots()
count = 0
for index, dot of uniqueDots
count++
count

destroyAllLines: => destroyAllLines: =>
for line in @lines for line in @lines
line.destroy() line.destroy()
Expand Down

0 comments on commit b6075cf

Please sign in to comment.