Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.namespace 'app.game.match3Logic'
- app.game.match3Logic = (pSize = '8x8') ->
- @store =
- switch: []
- array: []
- gems: game.add.group()
- map: null
- level:
- size: pSize.split 'x'
- score: {}
- @
- app.game.match3Logic:: =
- store: {}
- pushIntoArray: (pX, pY, pElem) ->
- @store.array[pX] = [] if not @store.array[pX]
- @store.array[pX][pY] = pElem
- getChains: ->
- chains = []
- x = 0
- while x < @store.level.size[0]
- chains[x] = []
- y = 0
- while y < @store.level.size[1]
- chains[x][y] = @checkChain(x, y)
- y++
- x++
- chains
- canGemMove: (x, y) ->
- (x > 0 and @canSwitch(x, y, x - 1, y)) or
- (x < @store.level.size[0] - 1 and @canSwitch(x, y, x + 1, y)) or
- (y > 0 and @canSwitch(x, y, x, y - 1)) or
- (y < @store.level.size[1] - 1 and @canSwitch(x, y, x, y + 1))
- getRandomMoveableGem: ->
- moveable = false
- tmpArr = {}
- while moveable is false
- x = _.sample(_.range(0, @store.level.size[0] - 1))
- y = _.sample(_.range(0, @store.level.size[1] - 1))
- continue if tmpArr[x + 'x' + y]
- if @canGemMove x, y
- moveable = true
- return [x, y]
- else
- tmpArr[x + 'x' + y] = false
- anyMoves: ->
- x = 0
- while x < @store.level.size[0]
- y = 0
- while y < @store.level.size[1]
- return [x, y] if @canGemMove x, y
- y++
- x++
- false
- howMuchBlocksBetween: (x, y1, y2) ->
- i = y1 + 1
- blocks = 0
- while i < y2
- type = @getGemType x, i
- blocks++ if type is 'block'
- i++
- blocks
- check: (pEvents = []) ->
- chains = @getChains()
- hadChains = false
- removed = []
- moved = []
- score =
- colors: {}
- total: 0
- gaps = []
- x = 0
- while x < @store.level.size[0]
- gaps[x] = 0
- y = @store.level.size[1] - 1
- while y >= 0
- if chains[x][y] > 2
- hadChains = true
- gaps[x]++
- type = @getGemType x, y
- removed.push
- x: x
- y: y
- type: type
- gem: @getGem x, y
- #score += app.config.baseScore * Math.pow 2, (chains[x][y] - 3)
- score.colors[type] = 0 unless score.colors[type]
- # for each gem is baseProgress value:
- score.colors[type] = score.colors[type] + app.config.score.baseProgress
- else if gaps[x] > 0
- gem = @getGem x, y
- if gem.name isnt 'block'
- canPlaceBlock = false
- addY = @howMuchBlocksBetween x, y, y + gaps[x]
- while canPlaceBlock is false
- tmpGem = @getGem x, y + gaps[x] + addY
- if tmpGem.name isnt 'block'
- canPlaceBlock = true
- else
- addY++
- moved.push
- toX: x
- toY: y + gaps[x] + addY
- fromX: x
- fromY: y
- type: @getGemType x, y
- gem: gem
- @store.array[x][y + gaps[x] + addY] = gem
- y--
- y = 0
- while y < gaps[x]
- canPlaceBlock = false
- addY = 0
- while canPlaceBlock is false
- gem = @getGem x, y + addY
- if gem.name isnt 'block'
- canPlaceBlock = true
- else
- addY++
- type = @getRandomGemType()
- sprite = @createGem x, y + addY
- sprite.name = type
- sprite.visible = false
- new app.game.gem sprite
- sprite.gem_setAction()
- @store.array[x][y + addY] = sprite
- moved.push
- toX: x
- toY: y + addY
- fromX: x
- fromY: y - gaps[x]
- type: type
- gem: sprite
- y++
- x++
- colorsPoints = _.values score.colors
- score.total = _.sum(colorsPoints) * app.config.score.basePoints +
- app.config.score.bonus * _.sum(_.range(_.max(colorsPoints)+1))
- return pEvents if not hadChains
- pEvents.push
- type: 'move'
- data: moved
- ,
- type: 'remove'
- data: removed
- ,
- type: 'score'
- data: score
- @check pEvents
- checkChain: (pX, pY) ->
- type = @getGemType pX, pY
- return 0 if type is 'block'
- left = down = right = top = 0
- right++ while type is @getGemType pX + right + 1, pY
- left++ while type is @getGemType pX - left - 1, pY
- top++ while type is @getGemType pX, pY + top + 1
- down++ while type is @getGemType pX, pY - down - 1
- Math.max left + 1 + right, top + 1 + down
- inNeighborhood: (x1, y1, x2, y2) ->
- dx = Math.abs x1 - x2
- dy = Math.abs y1 - y2
- dx + dy is 1
- switch: (x1, y1, x2, y2, callback) ->
- if not @canSwitch x1, y1, x2, y2
- callback false
- return
- tmp = @getGem x1, y1
- @store.array[x1][y1] = @getGem x2, y2
- @store.array[x2][y2] = tmp
- events = @check()
- callback events
- # method is useless in createFromRandom
- reBuild: (pEvents) ->
- return if not pEvents
- _.each pEvents, (obj) ->
- _.each obj.data, (elem) ->
- if obj.type is 'remove'
- elem.gem.kill()
- else if obj.type is 'move'
- elem.gem.gem_setCoords elem.toX, elem.toY
- elem.gem.visible = true
- canSwitch: (x1, y1, x2, y2) ->
- return false unless @inNeighborhood x1, y1, x2, y2
- sprite1 = @getGem x1, y1
- sprite2 = @getGem x2, y2
- return false if sprite1.name is 'block'
- return false if sprite2.name is 'block'
- @store.array[x1][y1] = sprite2
- @store.array[x2][y2] = sprite1
- chain = @checkChain(x2, y2) > 2 or @checkChain(x1, y1) > 2
- @store.array[x1][y1] = sprite1
- @store.array[x2][y2] = sprite2
- chain
- getRandomGemType: ->
- _.sample @store.level.availableGems
- getGem: (pX, pY) ->
- if pX < 0 or pX > @store.level.size[0] - 1 or pY < 0 or pY > @store.level.size[1] - 1
- return false
- @store.array[pX][pY]
- getGemType: (pX, pY) ->
- gem = @getGem pX, pY
- return false if not gem
- gem.name
- createGem: (pX, pY) ->
- sprite = game.add.sprite pX * @store.map.tileWidth, pY * @store.map.tileHeight, 'gems'
- sprite
- createGems: ->
- x = 0
- while x < @store.level.size[0]
- y = 0
- while y < @store.level.size[1]
- type = @getRandomGemType()
- while (type is @getGemType(x - 1, y) and type is @getGemType(x - 2, y)) or
- (type is @getGemType(x, y - 1) and type is @getGemType(x, y - 2))
- type = @getRandomGemType()
- sprite = @createGem x, y
- sprite.name = type
- @pushIntoArray x, y, sprite
- @store.gems.add sprite
- y++
- x++
- aliveGems: ->
- that = @
- @store.gems.forEach (child) ->
- x = Math.round child.x / game.world.match3.store.map.tileWidth
- y = Math.round child.y / game.world.match3.store.map.tileHeight
- if that.store.array and that.store.array[x] and that.store.array[x][y]
- child.kill()
- return
- new app.game.gem child
- return if child.name is 'initLevel'
- child.gem_alignToGrid()
- child.gem_setAction()
- createFromRandom: ->
- @store.map =
- tileWidth: 77
- tileHeight: 77
- info = gm.getLevelInfo()
- @store.level = _.extend @store.level, info
- helper = (->
- @createGems()
- @store.array = []
- @aliveGems()
- ).bind(@)
- helper()
- helper() while not @anyMoves()
- createFromMap: (pMap) ->
- that = @
- that.store.map = pMap
- _.each app.config.gems, (rect, name) ->
- gem = gm.getObjectIn name, pMap.objects.items
- if gem and gem.gid
- pMap.createFromObjects 'items', gem.gid,
- 'gems', app.config.gems[gem.name].sprite, true, false, that.store.gems
- @aliveGems()
- @reBuild @check()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement