summaryrefslogtreecommitdiffstats
path: root/demos/declarative/samegame/content/samegame.js
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-07-27 06:39:56 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-07-27 06:39:56 (GMT)
commit1a5338d8cc96cd07d2340aa2aff5880fd2fdce68 (patch)
treeb2a6f161b835ac44bc6ce8ad3923e3ef646c3583 /demos/declarative/samegame/content/samegame.js
parent5cddf5630ff0e408a84c19b7bb5ebc36871fa025 (diff)
downloadQt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.zip
Qt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.tar.gz
Qt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.tar.bz2
Repurpose SameGame to be slimmer
No theming and no selected, plus some cleanup.
Diffstat (limited to 'demos/declarative/samegame/content/samegame.js')
-rwxr-xr-xdemos/declarative/samegame/content/samegame.js111
1 files changed, 19 insertions, 92 deletions
diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js
index ef32230..a7dd82b 100755
--- a/demos/declarative/samegame/content/samegame.js
+++ b/demos/declarative/samegame/content/samegame.js
@@ -1,5 +1,4 @@
/* 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;
@@ -7,19 +6,8 @@ var tileSize = 40;
var maxIndex = maxX*maxY;
var board = new Array(maxIndex);
var tileSrc = "content/BoomBlock.qml";
-var swapped = false;
-
-var compSrc;
var component;
-function swapTileSrc(){
- if(tileSrc == "content/SpinBlock.qml"){
- tileSrc = "content/BoomBlock.qml";
- }else{
- tileSrc = "content/SpinBlock.qml";
- }
-}
-
//Index function used instead of a 2D array
function index(xIdx,yIdx){
return xIdx + (yIdx * maxX);
@@ -33,11 +21,6 @@ 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;
@@ -47,43 +30,21 @@ function initBoard()
startCreatingBlock(xIdx,yIdx);
}
}
- //TODO: a flag that handleMouse uses to ignore clicks when we're loading
}
var fillFound;//Set after a floodFill call to the number of tiles found
-var floodBoard;//Set to 1 if the floodFill ticks off that node (mostly used by floodFill)
-
-var lastHoveredIdx = -2
-function handleHover(xIdx,yIdx, btn)
-{
- //Turn UI x,y into Game x,y
- if(index(xIdx, yIdx) == lastHoveredIdx)
- return;
- //if(btn != 0)
- // return;
- //Sets 'selected' on tile underneath and all connected
- floodFill(xIdx, yIdx, -1, "hover");
- //Could use the fillFound value to tell the player how many stones/points
-
- //Resets any previously selected
- if(board[lastHoveredIdx] != null){
- lastX = lastHoveredIdx % maxX;
- lastY = Math.floor(lastHoveredIdx / maxX);
- floodFill(lastX, lastY, -1, "unhover");
- }
- lastHoveredIdx = index(xIdx, yIdx);
-}
-
+var floodBoard;//Set to 1 if the floodFill reaches off that node
//NOTE: Be careful with vars named x,y, as the calling object's x,y are still in scope
-function handleClick(xIdx,yIdx)
+function handleClick(x,y)
{
+ xIdx = Math.floor(x/tileSize);
+ yIdx = Math.floor(y/tileSize);
if(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 it's a valid tile, remove it and all connected (does nothing if it's not connected)
+ floodFill(xIdx,yIdx, -1);
if(fillFound <= 0)
return;
gameCanvas.score += (fillFound - 1) * (fillFound - 1);
@@ -91,18 +52,7 @@ function handleClick(xIdx,yIdx)
victoryCheck();
}
-/*
- 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)
+function floodFill(xIdx,yIdx,type)
{
if(board[index(xIdx, yIdx)] == null)
return;
@@ -117,29 +67,17 @@ function floodFill(xIdx,yIdx,type, cmd)
}
if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0)
return;
- if(floodBoard[index(xIdx, yIdx)] == 1)
+ if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].type))
return;
- if(!first && type != board[index(xIdx,yIdx)].type)
- return;
floodBoard[index(xIdx, yIdx)] = 1;
- floodFill(xIdx+1,yIdx,type,cmd);
- floodFill(xIdx-1,yIdx,type,cmd);
- floodFill(xIdx,yIdx+1,type,cmd);
- floodFill(xIdx,yIdx-1,type,cmd);
- if(first==true && fillFound == 0){
- //TODO: Provide a way to inform the delegate
+ floodFill(xIdx+1,yIdx,type);
+ floodFill(xIdx-1,yIdx,type);
+ floodFill(xIdx,yIdx+1,type);
+ floodFill(xIdx,yIdx-1,type);
+ if(first==true && fillFound == 0)
return;//Can't remove single tiles
- }
- if(cmd == "kill"){
- board[index(xIdx,yIdx)].dying = true;
- 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");
- }
+ board[index(xIdx,yIdx)].dying = true;
+ board[index(xIdx,yIdx)] = null;
fillFound += 1;
}
@@ -191,17 +129,12 @@ function victoryCheck()
if(deservesBonus)
gameCanvas.score += 500;
//Checks for game over
- if(deservesBonus || noMoreMoves()){
+ if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))){
dialog.text = "Game Over. Your score is " + gameCanvas.score;
dialog.opacity = 1;
}
}
-function noMoreMoves()
-{
- 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)
{
@@ -212,15 +145,13 @@ function floodMoveCheck(xIdx, yIdx, type)
myType = board[index(xIdx, yIdx)].type;
if(type == myType)
return true;
- aT = myType;
- bT = myType;
- return floodMoveCheck(xIdx + 1, yIdx, aT) || floodMoveCheck(xIdx, yIdx - 1, bT);
+ return floodMoveCheck(xIdx + 1, yIdx, myType) ||
+ floodMoveCheck(xIdx, yIdx - 1, board[index(xIdx,yIdx)].type);
}
//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;
@@ -263,15 +194,11 @@ function finishCreatingBlock(xIdx,yIdx){
}
function startCreatingBlock(xIdx,yIdx){
- if(component!=null && compSrc == tileSrc){
+ if(component!=null){
finishCreatingBlock(xIdx,yIdx);
return;
}
- if(component!=null){//Changed source
- //delete component; //Does the engine handle this?
- compSrc = tileSrc;
- }
component = createComponent(tileSrc);
if(finishCreatingBlock(xIdx,yIdx))
return;