From 54afe4e470c365fec0bbe9f349c143dd2c84a343 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Fri, 24 Jul 2009 08:58:49 +1000 Subject: Some SameGame cleanup Mostly commenting the JS code. --- demos/declarative/samegame/SameGame.qml | 4 +- demos/declarative/samegame/content/samegame.js | 66 ++++++++++++++++++-------- 2 files changed, 48 insertions(+), 22 deletions(-) mode change 100644 => 100755 demos/declarative/samegame/content/samegame.js diff --git a/demos/declarative/samegame/SameGame.qml b/demos/declarative/samegame/SameGame.qml index 231de03..e2c2089 100644 --- a/demos/declarative/samegame/SameGame.qml +++ b/demos/declarative/samegame/SameGame.qml @@ -28,9 +28,9 @@ Rect { anchors.topMargin: 10 anchors.horizontalCenter: parent.horizontalCenter MediaButton { id: btnA; text: "New Game"; onClicked: {initBoard();} } - MediaButton { id: btnB; text: "Swap Theme"; onClicked: {swapTileSrc(); dialog.opacity = 1; + MediaButton { id: btnB; text: "Swap Tiles"; onClicked: {swapTileSrc(); dialog.opacity = 1; dialog.text="Takes effect next game.";} } - Text{ text: "Score: " + gameCanvas.score; width:100 } + Text{ text: "Score: " + gameCanvas.score; width:120; font.size:14 } } SameDialog { id: dialog diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js old mode 100644 new mode 100755 index 6bcfd17..ef32230 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -1,5 +1,6 @@ /* This script file handles the game logic */ +//Note that X/Y referred to here are in game coordinates var maxX = 10;//Nums are for tileSize 40 var maxY = 15; var tileSize = 40; @@ -19,6 +20,7 @@ function swapTileSrc(){ } } +//Index function used instead of a 2D array function index(xIdx,yIdx){ return xIdx + (yIdx * maxX); } @@ -31,12 +33,14 @@ function initBoard() board[i].destroy(); } + //Game size is as many rows/cols fit in the GameCanvas maxX = Math.floor(gameCanvas.width/tileSize); maxY = Math.floor(gameCanvas.height/tileSize); maxIndex = maxX*maxY; + + //Initialize Board board = new Array(maxIndex); gameCanvas.score = 0; - for(xIdx=0; xIdx= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) return; if(board[index(xIdx, yIdx)] == null) return; + //If it's a valid tile, remove it and all connected + //floodFill does nothing if it's not connected to any other tiles. floodFill(xIdx,yIdx, -1, "kill"); if(fillFound <= 0) return; @@ -80,7 +91,17 @@ function handleClick(xIdx,yIdx) victoryCheck(); } -//cmd = "kill" is the removal case, cmd = "hover" is the mouse overed case +/* + the floodFill function does something on all tiles connected to the + given Game position. Connected requires the adjacency of one or more + tiles of the same type. Note that if there is no tile, or the tile + is not adjacent to a similar typed tile, then it will not do anything. + + Since many things need this flood functionality, a string command is + given telling it what to do with these tiles. + + cmd = "kill" is the removal case, cmd = "hover" sets selection +*/ function floodFill(xIdx,yIdx,type, cmd) { if(board[index(xIdx, yIdx)] == null) @@ -111,9 +132,11 @@ function floodFill(xIdx,yIdx,type, cmd) } if(cmd == "kill"){ board[index(xIdx,yIdx)].dying = true; - board[index(xIdx,yIdx)] = null;//They'll have to destroy themselves(can we do that?) + board[index(xIdx,yIdx)] = null; }else if(cmd == "hover"){ board[index(xIdx,yIdx)].selected = true; + }else if(cmd == "unhover"){ + board[index(xIdx,yIdx)].selected = false; }else{ print ("Flood Error"); } @@ -160,10 +183,11 @@ function shuffleDown() function victoryCheck() { - //awards bonuses + //awards bonuses for no tiles left deservesBonus = true; - if(board[index(0, maxY - 1)] != null) - deservesBonus = false; + for(xIdx=maxX-1; xIdx>=0; xIdx--) + if(board[index(xIdx, maxY - 1)] != null) + deservesBonus = false; if(deservesBonus) gameCanvas.score += 500; //Checks for game over @@ -175,10 +199,10 @@ function victoryCheck() function noMoreMoves() { - moreMoves = floodMoveCheck(0, maxY-1, -1); - return !moreMoves; + return !floodMoveCheck(0, maxY-1, -1); } +//only floods up and right, to see if it can find adjacent same-typed tiles function floodMoveCheck(xIdx, yIdx, type) { if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) @@ -188,17 +212,19 @@ function floodMoveCheck(xIdx, yIdx, type) myType = board[index(xIdx, yIdx)].type; if(type == myType) return true; - var at = myType; - var bt = myType; - return floodMoveCheck(xIdx + 1, yIdx, at) || floodMoveCheck(xIdx, yIdx - 1, bt); + aT = myType; + bT = myType; + return floodMoveCheck(xIdx + 1, yIdx, aT) || floodMoveCheck(xIdx, yIdx - 1, bT); } +//If the component isn't ready, then the signal doesn't include the game x,y +//So we store any x,y sent that we couldn't create at the time, and use those +//if we are triggered by the signal. //Need a simpler method of doing this? var waitStack = new Array(maxIndex); var waitTop = -1; function finishCreatingBlock(xIdx,yIdx){ - //TODO: Doc that the 'x', 'y' that were here are hidden properties from the calling QFxItem if(component.isReady){ if(xIdx == undefined){ //Called without arguments, create a previously stored (xIdx,yIdx) -- cgit v0.12