Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.contains = function (obj) {
  2.     var i = this.length;
  3.     while (i--) {
  4.         if (this[i] === obj) {
  5.             return true;
  6.         }
  7.     }
  8.     return false;
  9. };
  10. var MSG_SELECT_GEM = "msg_select_gem";
  11. var MSG_SWAP_DONE = "msg_swap_done";
  12. var MSG_REMOVE_GEM = "msg_remove_gem";
  13. var AIEMatch3Game = cc.Layer.extend(
  14. {
  15.     _bg: null,
  16.     _grid: null,
  17.     _gemsGrid: [], // [] = same as new Array()
  18.     _gameData: null,
  19.     _selectedGems: null,
  20.     _swapping: false,
  21.     _numExploding: 0,
  22.     _numSwapping: 0,
  23.     _firstSwap: false,
  24.     _xOffset: 0,
  25.     _yOffset: 0,
  26.     _xSpace: 0,
  27.     _ySpace: 0,
  28.     _gemsPool: null,
  29.     ctor: function () {
  30.         //call the constructor of the parent class
  31.         this._super();
  32.         this.loadLevels();
  33.         //grab the background file
  34.         this._bg = cc.Sprite.create('res/background.png');
  35.         this._bg.setZOrder(1);
  36.         //get the size of the window
  37.         var size = cc.Director.getInstance().getWinSize();
  38.         //position background in the center
  39.         this._bg.setPosition(size.width / 2, size.height / 2)
  40.  
  41.         //scale the background
  42.         this._bg.setScale(size.height / this._bg.getContentSize().height);
  43.  
  44.         // defining out game messages:
  45.         // message when gem has been selected
  46.         var MSG_SELECT_GEM = "msg_select_gem";
  47.         this._grid = cc.Sprite.create('res/background_grid.png');
  48.         this._grid.setPosition(size.width / 2, size.height / 2);
  49.         this._grid.setScale(size.height / this._bg.getContentSize().height);
  50.         this._grid.setOpacity(100); //0 - 255
  51.  
  52.         this.addChild(this._grid);
  53.         this.addChild(this._bg);
  54.  
  55.  
  56.         cc.SpriteFrameCache.getInstance().addSpriteFrames("res/gems.plist", "res/gems.png");
  57.  
  58.  
  59.         this._swapping = false;
  60.  
  61.         this._xOffset = 215 * (size.width / 640);
  62.         this._yOffset = (480 - 440) * (size.height / 480);
  63.         this._xSpace = 384 / this._gameData.level.maxCols * size.width / 640;
  64.         this._ySpace = 384 / this._gameData.level.maxCols * size.width / 640;
  65.  
  66.         this._selectedGems = [];
  67.         // adding ourself as an observer for this message
  68.         cc.NotificationCenter.getInstance().addObserver(this, this.selectGem, MSG_SELECT_GEM);
  69.         cc.NotificationCenter.getInstance().addObserver(this, this.selectGem, MSG_SELECT_GEM);
  70.         cc.NotificationCenter.getInstance().addObserver(this, this.onSwapDone, MSG_SWAP_DONE);
  71.         cc.NotificationCenter.getInstance().addObserver(this, this.removeExplodedGem, MSG_REMOVE_GEM);
  72.         //setting up explosion spritesheet
  73.         cc.SpriteFrameCache.getInstance().addSpriteFrames("res/explode.plist", "res/explode.png");
  74.         this._xOffset = 215 * (size.width / 640);
  75.         this._yOffset = (480 - 440) * (size.height / 480);
  76.         this._xSpace = 384 / this._gameData.level.maxCols * size.width / 640;
  77.         this._ySpace = 384 / this._gameData.level.maxCols * size.width / 640;
  78.         //adding a static member to gem
  79.         Gem._explosionFrames = [];
  80.         var frame;
  81.         for (var i = 1; i < 10; i++) {
  82.             frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(i + ".png"),
  83.             Gem._explosionFrames.push(frame);
  84.         }
  85.         this.initGrid();
  86.         this.schedule(this.update, 0.30);
  87.         this._gemsPool = [];
  88.     },
  89.     update: function (dt) {
  90.  
  91.         if ((this._numSwapping == 0) && (this._numExploding == 0)) {
  92.             this.checkForMatches();
  93.         }
  94.     },
  95.     loadLevels: function () {
  96.         var data = cc.FileUtils.getInstance().getTextFileData(s_gameData);
  97.         this._gameData = eval("(" + data + ")");
  98.  
  99.     },
  100.     selectGem: function (gem) {
  101.         //toggle selection
  102.         console.log("trying to swap");
  103.         if (this._swapping == false) {
  104.             gem.setSelected(!gem.getSelected());
  105.             if (gem.getSelected()) {
  106.                 this._selectedGems.push(gem);
  107.             } else {
  108.                 //clear out the selected gems
  109.                 this._selectedGems.length = 0;
  110.             }
  111.             if (this._selectedGems.length >= 2) {
  112.                 //swap dem gems
  113.                 this._firstSwap = true;
  114.                 this.swapGems()
  115.             }
  116.         }
  117.     },
  118.     swapGems: function () {
  119.         var swapRow1 = this._selectedGems[0].getRow();
  120.         var swapCol1 = this._selectedGems[0].getCol();
  121.         var swapRow2 = this._selectedGems[1].getRow();
  122.         var swapCol2 = this._selectedGems[1].getCol();
  123.  
  124.         if ((swapRow2 + 1 == swapRow1 && swapCol1 == swapCol2) || (swapRow2 - 1 == swapRow1 && swapCol1 == swapCol2) || (swapRow1 == swapRow2 && swapCol2 + 1 == swapCol1) || (swapRow1 == swapRow2 && swapCol2 - 1 == swapCol1)) {
  125.             this._swapping = true;
  126.             var tempGem = this._gemsGrid[swapRow1][swapCol1];
  127.             this._gemsGrid[swapRow1][swapCol1] = this._gemsGrid[swapRow2][swapCol2];
  128.             this._gemsGrid[swapRow1][swapCol1].setRow(swapRow1);
  129.             this._gemsGrid[swapRow1][swapCol1].setCol(swapCol1);
  130.  
  131.             this._gemsGrid[swapRow2][swapCol2] = tempGem;
  132.             this._gemsGrid[swapRow2][swapCol2].setRow(swapRow2);
  133.             this._gemsGrid[swapRow2][swapCol2].setCol(swapCol2);
  134.             var size = cc.Director.getInstance().getWinSize();
  135.  
  136.             // now move them to their eventual places
  137.             this._gemsGrid[swapRow1][swapCol1].swapGem(this._gameData.level.swappingTime, new cc.p(this._xOffset + swapCol1 * this._xSpace, this._yOffset + swapRow1 * this._ySpace));
  138.             this._gemsGrid[swapRow2][swapCol2].swapGem(this._gameData.level.swappingTime, new cc.p(this._xOffset + swapCol2 * this._xSpace, this._yOffset + swapRow2 * this._ySpace));
  139.            
  140.  
  141.             if (this._numSwapping < 0) {
  142.                 this._numSwapping = 0;
  143.             }
  144.             this._numSwapping += 2;
  145.             console.log(this._numSwapping);
  146.             this.runAction(cc.Sequence.create(cc.DelayTime.create(1.0), cc.CallFunc.create(this.onSwapDone.bind(this), this)));
  147.         } else {
  148.             this._selectedGems[0].setSelected(false);
  149.             this._selectedGems[1].setSelected(false);
  150.             this._selectedGems.length = 0;
  151.             this._numSwapping = 0;
  152.             this._swapping = false;
  153.         }
  154.        
  155.         //this.onSwapDone();
  156.     },
  157.     swapGem: function (){
  158.  
  159.     },
  160.     onSwapDone: function () {
  161.         //console.log(this._numSwapping);
  162.         //if (this._numSwapping == 2) {
  163.         //    this._firstSwap = true;
  164.         //}
  165.         this._numSwapping--;
  166.         if (this._numSwapping == 0) {
  167.             // check if there is a match or not
  168.  
  169.             if (this.checkForMatches()) {
  170.                 this._selectedGems[0].setSelected(false);
  171.                 this._selectedGems[1].setSelected(false);
  172.                 this._selectedGems.length = 0;
  173.                 this._swapping = false;
  174.             } else {
  175.                 // swap back
  176.                 console.log("first trying to swap back");
  177.                 if (this._firstSwap) {
  178.                     console.log("trying to swap back");
  179.                     this._firstSwap = false;
  180.                     var tempGem = this._selectedGems[0];
  181.                     this._selectedGems[0] = this._selectedGems[1];
  182.                     this._selectedGems[1] = tempGem;
  183.                     this.swapGems();
  184.                 } else {
  185.                     console.log("swapped back");
  186.                     // swapped back, now unselect
  187.                     this._selectedGems[0].setSelected(false);
  188.                     this._selectedGems[1].setSelected(false);
  189.                     this._selectedGems.length = 0;
  190.                     this._numSwapping = 0;
  191.                     this._swapping = false;
  192.                 }
  193.             }
  194.         }
  195.     },
  196.     removeExplodedGem: function (gem) {
  197.         this._gemsPool.push(gem);
  198.         this.removeChild(gem);
  199.         this._gemsGrid[gem.getRow()][gem.getCol()] = null;
  200.         this._numExploding--;
  201.     },
  202.     initGrid: function () {
  203.         //get the size of the window
  204.         var size = cc.Director.getInstance().getWinSize();
  205.  
  206.         for (var row = 0; row < this._gameData.level.maxRows; row++) {
  207.             this._gemsGrid[row] = [];
  208.             console.log(row);
  209.             for (var col = 0; col < this._gameData.level.maxCols; col++) {
  210.                 var buffer = 16;
  211.                 var newGem = new Gem();
  212.                 var random = Math.floor(Math.random() * 4)
  213.                 newGem.setGemType(random);
  214.                 newGem.setAnchorPoint(0, 0);
  215.                 newGem.setWidth(384 / this._gameData.level.maxCols * size.width / 640);
  216.                 newGem.setHeight(384 / this._gameData.level.maxRows * size.height / 480);
  217.                 newGem.setScaleX(newGem.getWidth() / newGem.getContentSize().width);
  218.                 newGem.setScaleY(newGem.getHeight() / newGem.getContentSize().height);
  219.                 newGem.setPosition(new cc.Point(215 * (size.width / 640) + col * newGem.getWidth(), (480 - 440)
  220.                    * (size.height / 480) + (row + this._gameData.level.maxRows) * newGem.getHeight()));
  221.  
  222.                 newGem._overlaySprite.setScaleX(newGem.getWidth() / Gem._explosionFrames[0].getOriginalSize().width);
  223.                 newGem._overlaySprite.setScaleY(newGem.getHeight() / Gem._explosionFrames[0].getOriginalSize().height);
  224.  
  225.                 newGem.runAction(cc.MoveTo.create(0.75, new cc.Point(215 * (size.width / 640) +
  226.                    col * newGem.getWidth(), (480 - 440) * (size.height / 480) + row * newGem.getHeight())));
  227.                 newGem.setRow(row);
  228.                 newGem.setCol(col);
  229.                 // var gemPosition = new cc.Point
  230.                 // (
  231.                 //     275 + row * (newGem.getContentSize().width + buffer),
  232.                 //     120 + col * (newGem.getContentSize().height + buffer)
  233.                 // );
  234.  
  235.                 //newGem.setPosition(gemPosition);
  236.  
  237.                 this.addChild(newGem);
  238.  
  239.                 this._gemsGrid[row][col] = newGem;
  240.             }
  241.         }
  242.         this.checkForMatches();
  243.  
  244.     },
  245.  
  246.     checkForMatches: function () {
  247.         var matched = false;
  248.         var matches = [];
  249.         for (var row = 0; row < this._gameData.level.maxRows; row++) {
  250.             for (var col = 0; col < this._gameData.level.maxCols - 2; col++) {
  251.                 if ((this._gemsGrid[row][col] != null) &&
  252.                     (this._gemsGrid[row][col + 1] != null) &&
  253.                     (this._gemsGrid[row][col + 2] != null) &&
  254.                     (this._gemsGrid[row][col].getGemType() == this._gemsGrid[row][col + 1].getGemType()) &&
  255.                     (this._gemsGrid[row][col].getGemType() == this._gemsGrid[row][col + 2].getGemType())) {
  256.                     matched = true;
  257.                     if (!matches.contains(this._gemsGrid[row][col]))
  258.                         matches.push(this._gemsGrid[row][col]);
  259.                     if (!matches.contains(this._gemsGrid[row][col + 1]))
  260.                         matches.push(this._gemsGrid[row][col + 1]);
  261.                     if (!matches.contains(this._gemsGrid[row][col + 2]))
  262.                         matches.push(this._gemsGrid[row][col + 2]);
  263.                 }
  264.             }
  265.         }
  266.         for (var col = 0; col < this._gameData.level.maxCols; col++) {
  267.             for (var row = 0; row < this._gameData.level.maxRows - 2; row++) {
  268.                 if (
  269.                     (this._gemsGrid[row][col] !== null) &&
  270.                     (this._gemsGrid[row + 1][col] !== null) &&
  271.                     (this._gemsGrid[row + 2][col] !== null) &&
  272.                     (this._gemsGrid[row][col].getGemType() === this._gemsGrid[row + 1][col].getGemType()) &&
  273.                     (this._gemsGrid[row][col].getGemType() === this._gemsGrid[row + 2][col].getGemType())
  274.  
  275.                     ) {
  276.                     matched = true;
  277.                     if (!matches.contains(this._gemsGrid[row][col]))
  278.                         matches.push(this._gemsGrid[row][col]);
  279.                     if (!matches.contains(this._gemsGrid[row + 1][col]))
  280.                         matches.push(this._gemsGrid[row + 1][col]);
  281.                     if (!matches.contains(this._gemsGrid[row + 2][col]))
  282.                         matches.push(this._gemsGrid[row + 2][col]);
  283.                 }
  284.             }
  285.         }
  286.         for (var i = 0; i < matches.length; i++) {
  287.             this._numExploding++;
  288.             matches[i].setGemState(GemState.Exploding);
  289.         }
  290.         return matched;
  291.     }
  292. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement