Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.namespace 'app.game.match3Logic'
  2.  
  3. app.game.match3Logic = (pSize = '8x8') ->
  4.     @store =
  5.         switch: []
  6.         array:  []
  7.         gems:   game.add.group()
  8.         map:    null
  9.         level:
  10.             size: pSize.split 'x'
  11.             score: {}
  12.  
  13.     @
  14.  
  15. app.game.match3Logic:: =
  16.  
  17.     store: {}
  18.  
  19.     pushIntoArray: (pX, pY, pElem) ->
  20.         @store.array[pX] = [] if not @store.array[pX]
  21.  
  22.         @store.array[pX][pY] = pElem
  23.  
  24.     getChains: ->
  25.         chains = []
  26.         x = 0
  27.  
  28.         while x < @store.level.size[0]
  29.             chains[x] = []
  30.             y = 0
  31.             while y < @store.level.size[1]
  32.                 chains[x][y] = @checkChain(x, y)
  33.                 y++
  34.             x++
  35.  
  36.         chains
  37.  
  38.     canGemMove: (x, y) ->
  39.         (x > 0 and @canSwitch(x, y, x - 1, y)) or
  40.         (x < @store.level.size[0] - 1 and @canSwitch(x, y, x + 1, y)) or
  41.         (y > 0 and @canSwitch(x, y, x, y - 1)) or
  42.         (y < @store.level.size[1] - 1 and @canSwitch(x, y, x, y + 1))
  43.  
  44.     getRandomMoveableGem: ->
  45.         moveable = false
  46.         tmpArr = {}
  47.  
  48.         while moveable is false
  49.             x = _.sample(_.range(0, @store.level.size[0] - 1))
  50.             y = _.sample(_.range(0, @store.level.size[1] - 1))
  51.  
  52.             continue if tmpArr[x + 'x' + y]
  53.             if @canGemMove x, y
  54.                 moveable = true
  55.                 return [x, y]
  56.             else
  57.                 tmpArr[x + 'x' + y] = false
  58.  
  59.     anyMoves: ->
  60.         x = 0
  61.  
  62.         while x < @store.level.size[0]
  63.             y = 0
  64.  
  65.             while y < @store.level.size[1]
  66.                 return [x, y]  if @canGemMove x, y
  67.                 y++
  68.             x++
  69.         false
  70.  
  71.     howMuchBlocksBetween: (x, y1, y2) ->
  72.         i = y1 + 1
  73.  
  74.         blocks = 0
  75.         while i < y2
  76.             type = @getGemType x, i
  77.             blocks++ if type is 'block'
  78.             i++
  79.  
  80.         blocks
  81.  
  82.     check: (pEvents = []) ->
  83.         chains = @getChains()
  84.         hadChains = false
  85.         removed = []
  86.         moved = []
  87.         score =
  88.             colors: {}
  89.             total: 0
  90.         gaps = []
  91.         x = 0
  92.  
  93.         while x < @store.level.size[0]
  94.             gaps[x] = 0
  95.             y = @store.level.size[1] - 1
  96.  
  97.             while y >= 0
  98.                 if chains[x][y] > 2
  99.                     hadChains = true
  100.                     gaps[x]++
  101.                     type = @getGemType x, y
  102.  
  103.                     removed.push
  104.                         x: x
  105.                         y: y
  106.                         type: type
  107.                         gem: @getGem x, y
  108.  
  109.                     #score += app.config.baseScore * Math.pow 2, (chains[x][y] - 3)
  110.                     score.colors[type] = 0 unless score.colors[type]
  111.                     # for each gem is baseProgress value:
  112.                     score.colors[type] = score.colors[type] + app.config.score.baseProgress
  113.  
  114.                 else if gaps[x] > 0
  115.                     gem = @getGem x, y
  116.  
  117.                     if gem.name isnt 'block'
  118.                         canPlaceBlock = false
  119.                         addY = @howMuchBlocksBetween x, y, y + gaps[x]
  120.  
  121.                         while canPlaceBlock is false
  122.                             tmpGem = @getGem x, y + gaps[x] + addY
  123.  
  124.                             if tmpGem.name isnt 'block'
  125.                                 canPlaceBlock = true
  126.                             else
  127.                                 addY++
  128.  
  129.                         moved.push
  130.                             toX: x
  131.                             toY: y + gaps[x] + addY
  132.                             fromX: x
  133.                             fromY: y
  134.                             type: @getGemType x, y
  135.                             gem: gem
  136.  
  137.                         @store.array[x][y + gaps[x] + addY] = gem
  138.  
  139.                 y--
  140.  
  141.             y = 0
  142.             while y < gaps[x]
  143.  
  144.                 canPlaceBlock = false
  145.                 addY = 0
  146.                 while canPlaceBlock is false
  147.                     gem = @getGem x, y + addY
  148.  
  149.                     if gem.name isnt 'block'
  150.                         canPlaceBlock = true
  151.                     else
  152.                         addY++
  153.  
  154.                 type = @getRandomGemType()
  155.                 sprite = @createGem x, y + addY
  156.                 sprite.name = type
  157.                 sprite.visible = false
  158.  
  159.                 new app.game.gem sprite
  160.                 sprite.gem_setAction()
  161.  
  162.                 @store.array[x][y + addY] = sprite
  163.  
  164.                 moved.push
  165.                     toX: x
  166.                     toY: y + addY
  167.                     fromX: x
  168.                     fromY: y - gaps[x]
  169.                     type: type
  170.                     gem: sprite
  171.  
  172.                 y++
  173.  
  174.             x++
  175.  
  176.         colorsPoints = _.values score.colors
  177.         score.total  =  _.sum(colorsPoints) * app.config.score.basePoints +
  178.                         app.config.score.bonus * _.sum(_.range(_.max(colorsPoints)+1))
  179.  
  180.         return pEvents if not hadChains
  181.  
  182.         pEvents.push
  183.             type: 'move'
  184.             data: moved
  185.         ,
  186.             type: 'remove'
  187.             data: removed
  188.         ,
  189.             type: 'score'
  190.             data: score
  191.  
  192.         @check pEvents
  193.  
  194.     checkChain: (pX, pY) ->
  195.         type = @getGemType pX, pY
  196.  
  197.         return 0 if type is 'block'
  198.  
  199.         left = down = right = top = 0
  200.  
  201.         right++  while type is @getGemType pX + right + 1, pY
  202.  
  203.         left++  while type is @getGemType pX - left - 1, pY
  204.  
  205.         top++  while type is @getGemType pX, pY + top + 1
  206.  
  207.         down++  while type is @getGemType pX, pY - down - 1
  208.  
  209.         Math.max left + 1 + right, top + 1 + down
  210.  
  211.     inNeighborhood: (x1, y1, x2, y2) ->
  212.         dx = Math.abs x1 - x2
  213.         dy = Math.abs y1 - y2
  214.  
  215.         dx + dy is 1
  216.  
  217.     switch: (x1, y1, x2, y2, callback) ->
  218.         if not @canSwitch x1, y1, x2, y2
  219.             callback false
  220.             return
  221.  
  222.         tmp = @getGem x1, y1
  223.         @store.array[x1][y1] = @getGem x2, y2
  224.         @store.array[x2][y2] = tmp
  225.  
  226.         events = @check()
  227.         callback events
  228.  
  229.     # method is useless in createFromRandom
  230.     reBuild: (pEvents) ->
  231.         return if not pEvents
  232.  
  233.         _.each pEvents, (obj) ->
  234.             _.each obj.data, (elem) ->
  235.  
  236.                 if obj.type is 'remove'
  237.                     elem.gem.kill()
  238.  
  239.                 else if obj.type is 'move'
  240.                     elem.gem.gem_setCoords elem.toX, elem.toY
  241.                     elem.gem.visible = true
  242.  
  243.     canSwitch: (x1, y1, x2, y2) ->
  244.         return false  unless @inNeighborhood x1, y1, x2, y2
  245.  
  246.         sprite1 = @getGem x1, y1
  247.         sprite2 = @getGem x2, y2
  248.  
  249.         return false if sprite1.name is 'block'
  250.         return false if sprite2.name is 'block'
  251.  
  252.         @store.array[x1][y1] = sprite2
  253.         @store.array[x2][y2] = sprite1
  254.         chain = @checkChain(x2, y2) > 2 or @checkChain(x1, y1) > 2
  255.  
  256.         @store.array[x1][y1] = sprite1
  257.         @store.array[x2][y2] = sprite2
  258.  
  259.         chain
  260.  
  261.     getRandomGemType: ->
  262.         _.sample @store.level.availableGems
  263.  
  264.     getGem: (pX, pY) ->
  265.         if pX < 0 or pX > @store.level.size[0] - 1 or pY < 0 or pY > @store.level.size[1] - 1
  266.             return false
  267.  
  268.         @store.array[pX][pY]
  269.  
  270.     getGemType: (pX, pY) ->
  271.         gem = @getGem pX, pY
  272.  
  273.         return false if not gem
  274.  
  275.         gem.name
  276.  
  277.     createGem: (pX, pY) ->
  278.         sprite = game.add.sprite pX * @store.map.tileWidth, pY * @store.map.tileHeight, 'gems'
  279.  
  280.         sprite
  281.  
  282.     createGems: ->
  283.         x = 0
  284.         while x < @store.level.size[0]
  285.             y = 0
  286.  
  287.             while y < @store.level.size[1]
  288.                 type = @getRandomGemType()
  289.                 while   (type is @getGemType(x - 1, y) and type is @getGemType(x - 2, y)) or
  290.                         (type is @getGemType(x, y - 1) and type is @getGemType(x, y - 2))
  291.                     type = @getRandomGemType()
  292.  
  293.                 sprite = @createGem x, y
  294.                 sprite.name = type
  295.  
  296.                 @pushIntoArray x, y, sprite
  297.  
  298.                 @store.gems.add sprite
  299.  
  300.                 y++
  301.             x++
  302.  
  303.     aliveGems: ->
  304.         that = @
  305.  
  306.         @store.gems.forEach (child) ->
  307.             x = Math.round child.x / game.world.match3.store.map.tileWidth
  308.             y = Math.round child.y / game.world.match3.store.map.tileHeight
  309.  
  310.             if that.store.array and that.store.array[x] and that.store.array[x][y]
  311.                 child.kill()
  312.                 return
  313.  
  314.             new app.game.gem child
  315.  
  316.             return if child.name is 'initLevel'
  317.  
  318.             child.gem_alignToGrid()
  319.             child.gem_setAction()
  320.  
  321.     createFromRandom: ->
  322.         @store.map =
  323.             tileWidth: 77
  324.             tileHeight: 77
  325.  
  326.         info = gm.getLevelInfo()
  327.         @store.level = _.extend @store.level, info
  328.  
  329.         helper = (->
  330.             @createGems()
  331.             @store.array = []
  332.             @aliveGems()
  333.         ).bind(@)
  334.  
  335.         helper()
  336.         helper() while not @anyMoves()
  337.  
  338.     createFromMap: (pMap) ->
  339.         that = @
  340.         that.store.map = pMap
  341.  
  342.         _.each app.config.gems, (rect, name) ->
  343.             gem = gm.getObjectIn name, pMap.objects.items
  344.  
  345.             if gem and gem.gid
  346.                 pMap.createFromObjects 'items', gem.gid,
  347.                         'gems', app.config.gems[gem.name].sprite, true, false, that.store.gems
  348.  
  349.         @aliveGems()
  350.         @reBuild @check()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement