2013-12-16 15:16:44 +00:00
|
|
|
// mods by Patrick OReilly
|
2014-02-26 00:58:19 +00:00
|
|
|
// twitter: @pato_reilly
|
2013-12-16 15:16:44 +00:00
|
|
|
|
|
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
|
|
|
|
|
|
|
function preload() {
|
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
game.load.tilemap('matching', 'assets/tilemaps/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
|
game.load.tileset('tiles', 'assets/tilemaps/tiles/phaser_tiles.png', 100, 100, -1, 1, 1);
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var timeCheck = 0;
|
|
|
|
var flipFlag = false;
|
|
|
|
|
|
|
|
var startList = new Array();
|
|
|
|
var squareList = new Array();
|
|
|
|
|
|
|
|
var masterCounter = 0;
|
|
|
|
var squareCounter = 0;
|
|
|
|
var square1Num;
|
|
|
|
var square2Num;
|
|
|
|
var savedSquareX1;
|
|
|
|
var savedSquareY1;
|
|
|
|
var savedSquareX2;
|
|
|
|
var savedSquareY2;
|
|
|
|
|
|
|
|
var map;
|
|
|
|
var tileset;
|
|
|
|
var layer;
|
|
|
|
|
|
|
|
var marker;
|
|
|
|
var currentTile;
|
|
|
|
var currentTilePosition;
|
|
|
|
|
|
|
|
var tileBack = 25;
|
|
|
|
var timesUp = '+';
|
|
|
|
var youWin = '+';
|
|
|
|
|
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
function create() {
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
map = game.add.tilemap('matching');
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
tileset = game.add.tileset('tiles');
|
|
|
|
|
|
|
|
layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
//layer.resizeWorld();
|
2013-12-16 15:16:44 +00:00
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
marker = game.add.graphics();
|
|
|
|
marker.lineStyle(2, 0x00FF00, 1);
|
|
|
|
marker.drawRect(0, 0, 100, 100);
|
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
randomizeTiles();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
countDownTimer();
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
if (layer.getTileX(game.input.activePointer.worldX) <= 5) // to prevent the marker from going out of bounds
|
|
|
|
{
|
|
|
|
marker.x = layer.getTileX(game.input.activePointer.worldX) * 100;
|
|
|
|
marker.y = layer.getTileY(game.input.activePointer.worldY) * 100;
|
|
|
|
}
|
|
|
|
|
2014-02-26 00:58:19 +00:00
|
|
|
if (flipFlag == true)
|
2013-12-16 15:16:44 +00:00
|
|
|
{
|
2014-02-26 00:58:19 +00:00
|
|
|
if (game.time.now - timeCheck >1000)
|
|
|
|
{
|
2013-12-16 15:16:44 +00:00
|
|
|
flipBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
processClick();
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
function countDownTimer() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
var timeLimit = 120;
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
myTime = game.time.now;
|
2014-02-26 00:58:19 +00:00
|
|
|
mySeconds = parseInt(myTime/1000);
|
2013-12-16 15:16:44 +00:00
|
|
|
myCountdownSeconds = timeLimit - mySeconds;
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
if (myCountdownSeconds <= 0)
|
|
|
|
{
|
2013-12-16 15:16:44 +00:00
|
|
|
// time is up
|
2014-02-26 00:58:19 +00:00
|
|
|
timesUp = 'Time is up!';
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processClick() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
currentTile = map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y));
|
2014-02-26 00:58:19 +00:00
|
|
|
currentTilePosition = ((layer.getTileY(game.input.activePointer.worldY)+1)*6)-(6-(layer.getTileX(game.input.activePointer.worldX)+1));
|
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
if (game.input.mousePointer.isDown)
|
2014-02-26 00:58:19 +00:00
|
|
|
{
|
2013-12-16 15:16:44 +00:00
|
|
|
// check to make sure the tile is not already flipped
|
2014-02-26 00:58:19 +00:00
|
|
|
if (currentTile == tileBack)
|
2013-12-16 15:16:44 +00:00
|
|
|
{
|
|
|
|
// get the corresponding item out of squareList
|
2014-02-26 00:58:19 +00:00
|
|
|
currentNum = squareList[currentTilePosition-1];
|
2013-12-16 15:16:44 +00:00
|
|
|
flipOver();
|
2014-02-26 00:58:19 +00:00
|
|
|
squareCounter++;
|
2013-12-16 15:16:44 +00:00
|
|
|
// is the second tile of pair flipped?
|
2014-02-26 00:58:19 +00:00
|
|
|
if (squareCounter == 2)
|
2013-12-16 15:16:44 +00:00
|
|
|
{
|
|
|
|
// reset squareCounter
|
|
|
|
squareCounter = 0;
|
|
|
|
square2Num = currentNum;
|
|
|
|
// check for match
|
|
|
|
if (square1Num == square2Num)
|
|
|
|
{
|
2014-02-26 00:58:19 +00:00
|
|
|
masterCounter++;
|
|
|
|
|
|
|
|
if (masterCounter == 18)
|
2013-12-16 15:16:44 +00:00
|
|
|
{
|
|
|
|
// go "win"
|
|
|
|
youWin = 'Got them all!';
|
2014-02-26 00:58:19 +00:00
|
|
|
}
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
savedSquareX2 = layer.getTileX(marker.x);
|
|
|
|
savedSquareY2 = layer.getTileY(marker.y);
|
2014-02-26 00:58:19 +00:00
|
|
|
flipFlag = true;
|
|
|
|
timeCheck = game.time.now;
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 15:16:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
savedSquareX1 = layer.getTileX(marker.x);
|
|
|
|
savedSquareY1 = layer.getTileY(marker.y);
|
2014-02-26 00:58:19 +00:00
|
|
|
square1Num = currentNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
function flipOver() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
map.putTile(currentNum, layer.getTileX(marker.x), layer.getTileY(marker.y));
|
|
|
|
}
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
function flipBack() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
flipFlag = false;
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
map.putTile(tileBack, savedSquareX1, savedSquareY1);
|
|
|
|
map.putTile(tileBack, savedSquareX2, savedSquareY2);
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
function randomizeTiles() {
|
|
|
|
|
|
|
|
for (num = 1; num <= 18; num++)
|
|
|
|
{
|
|
|
|
startList.push(num);
|
|
|
|
}
|
|
|
|
for (num = 1; num <= 18; num++)
|
|
|
|
{
|
|
|
|
startList.push(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
myString1 = startList.toString();
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
// randomize squareList
|
2014-02-26 00:58:19 +00:00
|
|
|
for (i = 1; i <=36; i++)
|
2013-12-16 15:16:44 +00:00
|
|
|
{
|
2014-02-26 00:58:19 +00:00
|
|
|
randomPosition = game.rnd.integerInRange(0,startList.length);
|
|
|
|
|
|
|
|
thisNumber = startList[ randomPosition ];
|
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
squareList.push(thisNumber);
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
a = startList.indexOf(thisNumber);
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
startList.splice( a, 1);
|
2013-12-16 15:16:44 +00:00
|
|
|
}
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
// for debugging
|
|
|
|
myString2 = squareList.toString();
|
2014-02-26 00:58:19 +00:00
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
for (col = 0; col < 6; col++)
|
|
|
|
{
|
|
|
|
for (row = 0; row < 6; row++)
|
|
|
|
{
|
|
|
|
map.putTile(tileBack, col, row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHiddenTile() {
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
thisTile = squareList[currentTilePosition-1];
|
2013-12-16 15:16:44 +00:00
|
|
|
return thisTile;
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
|
|
|
|
game.debug.renderText(timesUp, 620, 208, 'rgb(0,255,0)');
|
|
|
|
game.debug.renderText(youWin, 620, 240, 'rgb(0,255,0)');
|
|
|
|
|
|
|
|
game.debug.renderText('Time: ' + myCountdownSeconds, 620, 15, 'rgb(0,255,0)');
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
//game.debug.renderText('squareCounter: ' + squareCounter, 620, 272, 'rgb(0,0,255)');
|
2013-12-16 15:16:44 +00:00
|
|
|
game.debug.renderText('Matched Pairs: ' + masterCounter, 620, 304, 'rgb(0,0,255)');
|
2014-02-26 00:58:19 +00:00
|
|
|
|
|
|
|
//game.debug.renderText('startList: ' + myString1, 620, 208, 'rgb(255,0,0)');
|
|
|
|
//game.debug.renderText('squareList: ' + myString2, 620, 240, 'rgb(255,0,0)');
|
|
|
|
|
2013-12-16 15:16:44 +00:00
|
|
|
game.debug.renderText('Tile: ' + map.getTile(layer.getTileX(marker.x), layer.getTileY(marker.y)), 620, 48, 'rgb(255,0,0)');
|
|
|
|
|
|
|
|
game.debug.renderText('LayerX: ' + layer.getTileX(marker.x), 620, 80, 'rgb(255,0,0)');
|
|
|
|
game.debug.renderText('LayerY: ' + layer.getTileY(marker.y), 620, 112, 'rgb(255,0,0)');
|
|
|
|
|
|
|
|
game.debug.renderText('Tile Position: ' + currentTilePosition, 620, 144, 'rgb(255,0,0)');
|
|
|
|
game.debug.renderText('Hidden Tile: ' + getHiddenTile(), 620, 176, 'rgb(255,0,0)');
|
2014-02-26 00:58:19 +00:00
|
|
|
}
|