|
| 1 | +// mods by Patrick OReilly |
| 2 | +// Twitter: @pato_reilly Web: http://patricko.byethost9.com |
| 3 | + |
| 4 | +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
| 5 | + |
| 6 | +function preload() { |
| 7 | + |
| 8 | + game.load.tilemap('matching', 'assets/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON); |
| 9 | + game.load.tileset('tiles', 'assets/tiles/phaser_tiles.png', 100, 100, -1, 1, 1); |
| 10 | + |
| 11 | +} |
| 12 | + |
| 13 | +var timeCheck = 0; |
| 14 | +var flipFlag = false; |
| 15 | + |
| 16 | +var startList = new Array(); |
| 17 | +var squareList = new Array(); |
| 18 | + |
| 19 | +var masterCounter = 0; |
| 20 | +var squareCounter = 0; |
| 21 | +var square1Num; |
| 22 | +var square2Num; |
| 23 | +var savedSquareX1; |
| 24 | +var savedSquareY1; |
| 25 | +var savedSquareX2; |
| 26 | +var savedSquareY2; |
| 27 | + |
| 28 | +var map; |
| 29 | +var tileset; |
| 30 | +var layer; |
| 31 | + |
| 32 | +var marker; |
| 33 | +var currentTile; |
| 34 | +var currentTilePosition; |
| 35 | + |
| 36 | +var tileBack = 25; |
| 37 | +var timesUp = '+'; |
| 38 | +var youWin = '+'; |
| 39 | + |
| 40 | +function create() { |
| 41 | + |
| 42 | + map = game.add.tilemap('matching'); |
| 43 | + |
| 44 | + tileset = game.add.tileset('tiles'); |
| 45 | + |
| 46 | + layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0); |
| 47 | + |
| 48 | + marker = game.add.graphics(); |
| 49 | + marker.lineStyle(2, 0x00FF00, 1); |
| 50 | + marker.drawRect(0, 0, 100, 100); |
| 51 | + |
| 52 | + randomizeTiles(); |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +function update() { |
| 57 | + |
| 58 | + countDownTimer(); |
| 59 | + |
| 60 | + if (layer.getTileX(game.input.activePointer.worldX) <= 5) // to prevent the marker from going out of bounds |
| 61 | + { |
| 62 | + marker.x = layer.getTileX(game.input.activePointer.worldX) * 100; |
| 63 | + marker.y = layer.getTileY(game.input.activePointer.worldY) * 100; |
| 64 | + } |
| 65 | + |
| 66 | + if (flipFlag == true) |
| 67 | + { |
| 68 | + if (game.time.now - timeCheck > 1000) |
| 69 | + { |
| 70 | + flipBack(); |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + processClick(); |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +function countDownTimer() { |
| 81 | + |
| 82 | + var timeLimit = 120; |
| 83 | + |
| 84 | + myTime = game.time.now; |
| 85 | + mySeconds = parseInt(myTime / 1000); |
| 86 | + myCountdownSeconds = timeLimit - mySeconds; |
| 87 | + |
| 88 | + if (myCountdownSeconds <= 0) |
| 89 | + { |
| 90 | + // time is up |
| 91 | + timesUp = 'Time is up!'; |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +function processClick() { |
| 97 | + |
| 98 | + currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)); |
| 99 | + currentTilePosition = ((layer.getTileY(game.input.activePointer.worldY) + 1) * 6) - (6 - (layer.getTileX(game.input.activePointer.worldX) + 1)); |
| 100 | + |
| 101 | + if (game.input.mousePointer.isDown) |
| 102 | + { |
| 103 | + // check to make sure the tile is not already flipped |
| 104 | + if (currentTile == tileBack) |
| 105 | + { |
| 106 | + // get the corresponding item out of squareList |
| 107 | + currentNum = squareList[currentTilePosition - 1]; |
| 108 | + flipOver(); |
| 109 | + squareCounter++; |
| 110 | + |
| 111 | + // is the second tile of pair flipped? |
| 112 | + if (squareCounter == 2) |
| 113 | + { |
| 114 | + // reset squareCounter |
| 115 | + squareCounter = 0; |
| 116 | + square2Num = currentNum; |
| 117 | + |
| 118 | + // check for match |
| 119 | + if (square1Num == square2Num) |
| 120 | + { |
| 121 | + masterCounter++; |
| 122 | + |
| 123 | + if (masterCounter == 18) |
| 124 | + { |
| 125 | + // go "win" |
| 126 | + youWin = 'Got them all!'; |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + savedSquareX2 = layer.getTileX(marker.x); |
| 132 | + savedSquareY2 = layer.getTileY(marker.y); |
| 133 | + flipFlag = true; |
| 134 | + timeCheck = game.time.now; |
| 135 | + } |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + savedSquareX1 = layer.getTileX(marker.x); |
| 140 | + savedSquareY1 = layer.getTileY(marker.y); |
| 141 | + square1Num = currentNum; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function flipOver() { |
| 148 | + |
| 149 | + map.putTile(currentNum, layer.getTileX(marker.x), layer.getTileY(marker.y)); |
| 150 | +} |
| 151 | + |
| 152 | +function flipBack() { |
| 153 | + |
| 154 | + flipFlag = false; |
| 155 | + |
| 156 | + map.putTile(tileBack, savedSquareX1, savedSquareY1); |
| 157 | + map.putTile(tileBack, savedSquareX2, savedSquareY2); |
| 158 | + |
| 159 | +} |
| 160 | + |
| 161 | +function randomizeTiles() { |
| 162 | + |
| 163 | + for (num = 1; num <= 18; num++) |
| 164 | + { |
| 165 | + startList.push(num); |
| 166 | + } |
| 167 | + |
| 168 | + for (num = 1; num <= 18; num++) |
| 169 | + { |
| 170 | + startList.push(num); |
| 171 | + } |
| 172 | + |
| 173 | + // for debugging |
| 174 | + myString1 = startList.toString(); |
| 175 | + |
| 176 | + // randomize squareList |
| 177 | + for (i = 1; i <= 36; i++) |
| 178 | + { |
| 179 | + randomPosition = game.rnd.integerInRange(0, startList.length); |
| 180 | + |
| 181 | + thisNumber = startList[randomPosition]; |
| 182 | + |
| 183 | + squareList.push(thisNumber); |
| 184 | + |
| 185 | + a = startList.indexOf(thisNumber); |
| 186 | + |
| 187 | + startList.splice(a, 1); |
| 188 | + } |
| 189 | + |
| 190 | + // for debugging |
| 191 | + myString2 = squareList.toString(); |
| 192 | + |
| 193 | + for (col = 0; col < 6; col++) |
| 194 | + { |
| 195 | + for (row = 0; row < 6; row++) |
| 196 | + { |
| 197 | + map.putTile(tileBack, col, row); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +function getHiddenTile() { |
| 203 | + |
| 204 | + thisTile = squareList[currentTilePosition - 1]; |
| 205 | + return thisTile; |
| 206 | +} |
| 207 | + |
| 208 | +function render() { |
| 209 | + |
| 210 | + game.debug.renderText(timesUp, 620, 208, 'rgb(0,255,0)'); |
| 211 | + game.debug.renderText(youWin, 620, 240, 'rgb(0,255,0)'); |
| 212 | + |
| 213 | + game.debug.renderText('Time: ' + myCountdownSeconds, 620, 15, 'rgb(0,255,0)'); |
| 214 | + game.debug.renderText('Matched Pairs: ' + masterCounter, 620, 304, 'rgb(0,0,255)'); |
| 215 | + game.debug.renderText('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 620, 48, 'rgb(255,0,0)'); |
| 216 | + |
| 217 | + game.debug.renderText('LayerX: ' + layer.getTileX(marker.x), 620, 80, 'rgb(255,0,0)'); |
| 218 | + game.debug.renderText('LayerY: ' + layer.getTileY(marker.y), 620, 112, 'rgb(255,0,0)'); |
| 219 | + |
| 220 | + game.debug.renderText('Tile Position: ' + currentTilePosition, 620, 144, 'rgb(255,0,0)'); |
| 221 | + game.debug.renderText('Hidden Tile: ' + getHiddenTile(), 620, 176, 'rgb(255,0,0)'); |
| 222 | + |
| 223 | +} |
0 commit comments