Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.prototype.contains = function (obj) {
- var i = this.length;
- while (i--) {
- if (this[i] === obj) {
- return true;
- }
- }
- return false;
- };
- var MSG_SELECT_GEM = "msg_select_gem";
- var MSG_SWAP_DONE = "msg_swap_done";
- var MSG_REMOVE_GEM = "msg_remove_gem";
- var AIEMatch3Game = cc.Layer.extend(
- {
- _bg: null,
- _grid: null,
- _gemsGrid: [], // [] = same as new Array()
- _gameData: null,
- _selectedGems: null,
- _swapping: false,
- _numExploding: 0,
- _numSwapping: 0,
- _firstSwap: false,
- _xOffset: 0,
- _yOffset: 0,
- _xSpace: 0,
- _ySpace: 0,
- _gemsPool: null,
- ctor: function () {
- //call the constructor of the parent class
- this._super();
- this.loadLevels();
- //grab the background file
- this._bg = cc.Sprite.create('res/background.png');
- this._bg.setZOrder(1);
- //get the size of the window
- var size = cc.Director.getInstance().getWinSize();
- //position background in the center
- this._bg.setPosition(size.width / 2, size.height / 2)
- //scale the background
- this._bg.setScale(size.height / this._bg.getContentSize().height);
- // defining out game messages:
- // message when gem has been selected
- var MSG_SELECT_GEM = "msg_select_gem";
- this._grid = cc.Sprite.create('res/background_grid.png');
- this._grid.setPosition(size.width / 2, size.height / 2);
- this._grid.setScale(size.height / this._bg.getContentSize().height);
- this._grid.setOpacity(100); //0 - 255
- this.addChild(this._grid);
- this.addChild(this._bg);
- cc.SpriteFrameCache.getInstance().addSpriteFrames("res/gems.plist", "res/gems.png");
- this._swapping = false;
- this._xOffset = 215 * (size.width / 640);
- this._yOffset = (480 - 440) * (size.height / 480);
- this._xSpace = 384 / this._gameData.level.maxCols * size.width / 640;
- this._ySpace = 384 / this._gameData.level.maxCols * size.width / 640;
- this._selectedGems = [];
- // adding ourself as an observer for this message
- cc.NotificationCenter.getInstance().addObserver(this, this.selectGem, MSG_SELECT_GEM);
- cc.NotificationCenter.getInstance().addObserver(this, this.selectGem, MSG_SELECT_GEM);
- cc.NotificationCenter.getInstance().addObserver(this, this.onSwapDone, MSG_SWAP_DONE);
- cc.NotificationCenter.getInstance().addObserver(this, this.removeExplodedGem, MSG_REMOVE_GEM);
- //setting up explosion spritesheet
- cc.SpriteFrameCache.getInstance().addSpriteFrames("res/explode.plist", "res/explode.png");
- this._xOffset = 215 * (size.width / 640);
- this._yOffset = (480 - 440) * (size.height / 480);
- this._xSpace = 384 / this._gameData.level.maxCols * size.width / 640;
- this._ySpace = 384 / this._gameData.level.maxCols * size.width / 640;
- //adding a static member to gem
- Gem._explosionFrames = [];
- var frame;
- for (var i = 1; i < 10; i++) {
- frame = cc.SpriteFrameCache.getInstance().getSpriteFrame(i + ".png"),
- Gem._explosionFrames.push(frame);
- }
- this.initGrid();
- this.schedule(this.update, 0.30);
- this._gemsPool = [];
- },
- update: function (dt) {
- if ((this._numSwapping == 0) && (this._numExploding == 0)) {
- this.checkForMatches();
- }
- },
- loadLevels: function () {
- var data = cc.FileUtils.getInstance().getTextFileData(s_gameData);
- this._gameData = eval("(" + data + ")");
- },
- selectGem: function (gem) {
- //toggle selection
- console.log("trying to swap");
- if (this._swapping == false) {
- gem.setSelected(!gem.getSelected());
- if (gem.getSelected()) {
- this._selectedGems.push(gem);
- } else {
- //clear out the selected gems
- this._selectedGems.length = 0;
- }
- if (this._selectedGems.length >= 2) {
- //swap dem gems
- this._firstSwap = true;
- this.swapGems()
- }
- }
- },
- swapGems: function () {
- var swapRow1 = this._selectedGems[0].getRow();
- var swapCol1 = this._selectedGems[0].getCol();
- var swapRow2 = this._selectedGems[1].getRow();
- var swapCol2 = this._selectedGems[1].getCol();
- if ((swapRow2 + 1 == swapRow1 && swapCol1 == swapCol2) || (swapRow2 - 1 == swapRow1 && swapCol1 == swapCol2) || (swapRow1 == swapRow2 && swapCol2 + 1 == swapCol1) || (swapRow1 == swapRow2 && swapCol2 - 1 == swapCol1)) {
- this._swapping = true;
- var tempGem = this._gemsGrid[swapRow1][swapCol1];
- this._gemsGrid[swapRow1][swapCol1] = this._gemsGrid[swapRow2][swapCol2];
- this._gemsGrid[swapRow1][swapCol1].setRow(swapRow1);
- this._gemsGrid[swapRow1][swapCol1].setCol(swapCol1);
- this._gemsGrid[swapRow2][swapCol2] = tempGem;
- this._gemsGrid[swapRow2][swapCol2].setRow(swapRow2);
- this._gemsGrid[swapRow2][swapCol2].setCol(swapCol2);
- var size = cc.Director.getInstance().getWinSize();
- // now move them to their eventual places
- this._gemsGrid[swapRow1][swapCol1].swapGem(this._gameData.level.swappingTime, new cc.p(this._xOffset + swapCol1 * this._xSpace, this._yOffset + swapRow1 * this._ySpace));
- this._gemsGrid[swapRow2][swapCol2].swapGem(this._gameData.level.swappingTime, new cc.p(this._xOffset + swapCol2 * this._xSpace, this._yOffset + swapRow2 * this._ySpace));
- if (this._numSwapping < 0) {
- this._numSwapping = 0;
- }
- this._numSwapping += 2;
- console.log(this._numSwapping);
- this.runAction(cc.Sequence.create(cc.DelayTime.create(1.0), cc.CallFunc.create(this.onSwapDone.bind(this), this)));
- } else {
- this._selectedGems[0].setSelected(false);
- this._selectedGems[1].setSelected(false);
- this._selectedGems.length = 0;
- this._numSwapping = 0;
- this._swapping = false;
- }
- //this.onSwapDone();
- },
- swapGem: function (){
- },
- onSwapDone: function () {
- //console.log(this._numSwapping);
- //if (this._numSwapping == 2) {
- // this._firstSwap = true;
- //}
- this._numSwapping--;
- if (this._numSwapping == 0) {
- // check if there is a match or not
- if (this.checkForMatches()) {
- this._selectedGems[0].setSelected(false);
- this._selectedGems[1].setSelected(false);
- this._selectedGems.length = 0;
- this._swapping = false;
- } else {
- // swap back
- console.log("first trying to swap back");
- if (this._firstSwap) {
- console.log("trying to swap back");
- this._firstSwap = false;
- var tempGem = this._selectedGems[0];
- this._selectedGems[0] = this._selectedGems[1];
- this._selectedGems[1] = tempGem;
- this.swapGems();
- } else {
- console.log("swapped back");
- // swapped back, now unselect
- this._selectedGems[0].setSelected(false);
- this._selectedGems[1].setSelected(false);
- this._selectedGems.length = 0;
- this._numSwapping = 0;
- this._swapping = false;
- }
- }
- }
- },
- removeExplodedGem: function (gem) {
- this._gemsPool.push(gem);
- this.removeChild(gem);
- this._gemsGrid[gem.getRow()][gem.getCol()] = null;
- this._numExploding--;
- },
- initGrid: function () {
- //get the size of the window
- var size = cc.Director.getInstance().getWinSize();
- for (var row = 0; row < this._gameData.level.maxRows; row++) {
- this._gemsGrid[row] = [];
- console.log(row);
- for (var col = 0; col < this._gameData.level.maxCols; col++) {
- var buffer = 16;
- var newGem = new Gem();
- var random = Math.floor(Math.random() * 4)
- newGem.setGemType(random);
- newGem.setAnchorPoint(0, 0);
- newGem.setWidth(384 / this._gameData.level.maxCols * size.width / 640);
- newGem.setHeight(384 / this._gameData.level.maxRows * size.height / 480);
- newGem.setScaleX(newGem.getWidth() / newGem.getContentSize().width);
- newGem.setScaleY(newGem.getHeight() / newGem.getContentSize().height);
- newGem.setPosition(new cc.Point(215 * (size.width / 640) + col * newGem.getWidth(), (480 - 440)
- * (size.height / 480) + (row + this._gameData.level.maxRows) * newGem.getHeight()));
- newGem._overlaySprite.setScaleX(newGem.getWidth() / Gem._explosionFrames[0].getOriginalSize().width);
- newGem._overlaySprite.setScaleY(newGem.getHeight() / Gem._explosionFrames[0].getOriginalSize().height);
- newGem.runAction(cc.MoveTo.create(0.75, new cc.Point(215 * (size.width / 640) +
- col * newGem.getWidth(), (480 - 440) * (size.height / 480) + row * newGem.getHeight())));
- newGem.setRow(row);
- newGem.setCol(col);
- // var gemPosition = new cc.Point
- // (
- // 275 + row * (newGem.getContentSize().width + buffer),
- // 120 + col * (newGem.getContentSize().height + buffer)
- // );
- //newGem.setPosition(gemPosition);
- this.addChild(newGem);
- this._gemsGrid[row][col] = newGem;
- }
- }
- this.checkForMatches();
- },
- checkForMatches: function () {
- var matched = false;
- var matches = [];
- for (var row = 0; row < this._gameData.level.maxRows; row++) {
- for (var col = 0; col < this._gameData.level.maxCols - 2; col++) {
- if ((this._gemsGrid[row][col] != null) &&
- (this._gemsGrid[row][col + 1] != null) &&
- (this._gemsGrid[row][col + 2] != null) &&
- (this._gemsGrid[row][col].getGemType() == this._gemsGrid[row][col + 1].getGemType()) &&
- (this._gemsGrid[row][col].getGemType() == this._gemsGrid[row][col + 2].getGemType())) {
- matched = true;
- if (!matches.contains(this._gemsGrid[row][col]))
- matches.push(this._gemsGrid[row][col]);
- if (!matches.contains(this._gemsGrid[row][col + 1]))
- matches.push(this._gemsGrid[row][col + 1]);
- if (!matches.contains(this._gemsGrid[row][col + 2]))
- matches.push(this._gemsGrid[row][col + 2]);
- }
- }
- }
- for (var col = 0; col < this._gameData.level.maxCols; col++) {
- for (var row = 0; row < this._gameData.level.maxRows - 2; row++) {
- if (
- (this._gemsGrid[row][col] !== null) &&
- (this._gemsGrid[row + 1][col] !== null) &&
- (this._gemsGrid[row + 2][col] !== null) &&
- (this._gemsGrid[row][col].getGemType() === this._gemsGrid[row + 1][col].getGemType()) &&
- (this._gemsGrid[row][col].getGemType() === this._gemsGrid[row + 2][col].getGemType())
- ) {
- matched = true;
- if (!matches.contains(this._gemsGrid[row][col]))
- matches.push(this._gemsGrid[row][col]);
- if (!matches.contains(this._gemsGrid[row + 1][col]))
- matches.push(this._gemsGrid[row + 1][col]);
- if (!matches.contains(this._gemsGrid[row + 2][col]))
- matches.push(this._gemsGrid[row + 2][col]);
- }
- }
- }
- for (var i = 0; i < matches.length; i++) {
- this._numExploding++;
- matches[i].setGemState(GemState.Exploding);
- }
- return matched;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement