summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tutorials/samegame/samegame3/samegame.js
diff options
context:
space:
mode:
Diffstat (limited to 'examples/declarative/tutorials/samegame/samegame3/samegame.js')
-rw-r--r--examples/declarative/tutorials/samegame/samegame3/samegame.js191
1 files changed, 92 insertions, 99 deletions
diff --git a/examples/declarative/tutorials/samegame/samegame3/samegame.js b/examples/declarative/tutorials/samegame/samegame3/samegame.js
index 33449fa..c12def7 100644
--- a/examples/declarative/tutorials/samegame/samegame3/samegame.js
+++ b/examples/declarative/tutorials/samegame/samegame3/samegame.js
@@ -1,23 +1,20 @@
/* 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 maxIndex = maxX*maxY;
+var maxColumn = 10;
+var maxRow = 15;
+var maxIndex = maxColumn * maxRow;
var board = new Array(maxIndex);
-var tileSrc = "Block.qml";
var component;
//Index function used instead of a 2D array
-function index(xIdx,yIdx) {
- return xIdx + (yIdx * maxX);
+function index(column, row) {
+ return column + (row * maxColumn);
}
-function initBoard()
-{
+function startNewGame() {
//Calculate board size
- maxX = Math.floor(gameCanvas.width/gameCanvas.tileSize);
- maxY = Math.floor(gameCanvas.height/gameCanvas.tileSize);
- maxIndex = maxY*maxX;
+ maxColumn = Math.floor(gameCanvas.width / gameCanvas.blockSize);
+ maxRow = Math.floor(gameCanvas.height / gameCanvas.blockSize);
+ maxIndex = maxRow * maxColumn;
//Close dialogs
dialog.forceClose();
@@ -25,37 +22,36 @@ function initBoard()
//Initialize Board
board = new Array(maxIndex);
gameCanvas.score = 0;
- for(var xIdx=0; xIdx<maxX; xIdx++){
- for(var yIdx=0; yIdx<maxY; yIdx++){
- board[index(xIdx,yIdx)] = null;
- createBlock(xIdx,yIdx);
+ for (var column = 0; column < maxColumn; column++) {
+ for (var row = 0; row < maxRow; row++) {
+ board[index(column, row)] = null;
+ createBlock(column, row);
}
}
}
-function createBlock(xIdx,yIdx){
- if(component==null)
- component = createComponent(tileSrc);
+function createBlock(column, row) {
+ if (component == null)
+ component = createComponent("Block.qml");
- // Note that we don't wait for the component to become ready. This will
- // only work if the block QML is a local file. Otherwise the component will
- // not be ready immediately. There is a statusChanged signal on the
- // component you could use if you want to wait to load remote files.
- if(component.isReady){
+ // Note that if Block.qml was not a local file, component.isReady would be
+ // false and we should wait for the component's statusChanged() signal to
+ // know when the file is downloaded and fully loaded before calling createObject().
+ if (component.isReady) {
var dynamicObject = component.createObject();
- if(dynamicObject == null){
+ if (dynamicObject == null) {
print("error creating block");
print(component.errorsString());
return false;
}
dynamicObject.type = Math.floor(Math.random() * 3);
dynamicObject.parent = gameCanvas;
- dynamicObject.x = xIdx*gameCanvas.tileSize;
- dynamicObject.y = yIdx*gameCanvas.tileSize;
- dynamicObject.width = gameCanvas.tileSize;
- dynamicObject.height = gameCanvas.tileSize;
- board[index(xIdx,yIdx)] = dynamicObject;
- }else{//isError or isLoading
+ dynamicObject.x = column * gameCanvas.blockSize;
+ dynamicObject.y = row * gameCanvas.blockSize;
+ dynamicObject.width = gameCanvas.blockSize;
+ dynamicObject.height = gameCanvas.blockSize;
+ board[index(column, row)] = dynamicObject;
+ } else {
print("error loading block component");
print(component.errorsString());
return false;
@@ -63,21 +59,21 @@ function createBlock(xIdx,yIdx){
return true;
}
-var fillFound;//Set after a floodFill call to the number of tiles found
-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
+var fillFound;
+//Set after a floodFill call to the number of blocks found
+var floodBoard;
+//Set to 1 if the floodFill reaches off that node
//![1]
-function handleClick(x,y)
-{
- var xIdx = Math.floor(x/gameCanvas.tileSize);
- var yIdx = Math.floor(y/gameCanvas.tileSize);
- if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0)
+function handleClick(xPos, yPos) {
+ var column = Math.floor(xPos / gameCanvas.blockSize);
+ var row = Math.floor(yPos / gameCanvas.blockSize);
+ if (column >= maxColumn || column < 0 || row >= maxRow || row < 0)
return;
- if(board[index(xIdx, yIdx)] == null)
+ if (board[index(column, row)] == null)
return;
- //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)
+ //If it's a valid block, remove it and all connected (does nothing if it's not connected)
+ floodFill(column, row, -1);
+ if (fillFound <= 0)
return;
gameCanvas.score += (fillFound - 1) * (fillFound - 1);
shuffleDown();
@@ -85,67 +81,65 @@ function handleClick(x,y)
}
//![1]
-function floodFill(xIdx,yIdx,type)
-{
- if(board[index(xIdx, yIdx)] == null)
+function floodFill(column, row, type) {
+ if (board[index(column, row)] == null)
return;
var first = false;
- if(type == -1){
+ if (type == -1) {
first = true;
- type = board[index(xIdx,yIdx)].type;
-
+ type = board[index(column, row)].type;
+
//Flood fill initialization
fillFound = 0;
floodBoard = new Array(maxIndex);
}
- if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0)
+ if (column >= maxColumn || column < 0 || row >= maxRow || row < 0)
return;
- if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].type))
+ if (floodBoard[index(column, row)] == 1 || (!first && type != board[index(column, row)].type))
return;
- floodBoard[index(xIdx, yIdx)] = 1;
- 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
- board[index(xIdx,yIdx)].opacity = 0;
- board[index(xIdx,yIdx)] = null;
+ floodBoard[index(column, row)] = 1;
+ floodFill(column + 1, row, type);
+ floodFill(column - 1, row, type);
+ floodFill(column, row + 1, type);
+ floodFill(column, row - 1, type);
+ if (first == true && fillFound == 0)
+ return; //Can't remove single blocks
+ board[index(column, row)].opacity = 0;
+ board[index(column, row)] = null;
fillFound += 1;
}
-function shuffleDown()
-{
+function shuffleDown() {
//Fall down
- for(var xIdx=0; xIdx<maxX; xIdx++){
+ for (var column = 0; column < maxColumn; column++) {
var fallDist = 0;
- for(var yIdx=maxY-1; yIdx>=0; yIdx--){
- if(board[index(xIdx,yIdx)] == null){
+ for (var row = maxRow - 1; row >= 0; row--) {
+ if (board[index(column, row)] == null) {
fallDist += 1;
- }else{
- if(fallDist > 0){
- var obj = board[index(xIdx,yIdx)];
- obj.y += fallDist * gameCanvas.tileSize;
- board[index(xIdx,yIdx+fallDist)] = obj;
- board[index(xIdx,yIdx)] = null;
+ } else {
+ if (fallDist > 0) {
+ var obj = board[index(column, row)];
+ obj.y += fallDist * gameCanvas.blockSize;
+ board[index(column, row + fallDist)] = obj;
+ board[index(column, row)] = null;
}
}
}
}
//Fall to the left
var fallDist = 0;
- for(var xIdx=0; xIdx<maxX; xIdx++){
- if(board[index(xIdx, maxY - 1)] == null){
+ for (var column = 0; column < maxColumn; column++) {
+ if (board[index(column, maxRow - 1)] == null) {
fallDist += 1;
- }else{
- if(fallDist > 0){
- for(var yIdx=0; yIdx<maxY; yIdx++){
- var obj = board[index(xIdx,yIdx)];
- if(obj == null)
+ } else {
+ if (fallDist > 0) {
+ for (var row = 0; row < maxRow; row++) {
+ var obj = board[index(column, row)];
+ if (obj == null)
continue;
- obj.x -= fallDist * gameCanvas.tileSize;
- board[index(xIdx-fallDist,yIdx)] = obj;
- board[index(xIdx,yIdx)] = null;
+ obj.x -= fallDist * gameCanvas.blockSize;
+ board[index(column - fallDist, row)] = obj;
+ board[index(column, row)] = null;
}
}
}
@@ -153,31 +147,30 @@ function shuffleDown()
}
//![2]
-function victoryCheck()
-{
- //awards bonuses for no tiles left
+function victoryCheck() {
+ //Award bonus points if no blocks left
var deservesBonus = true;
- for(var xIdx=maxX-1; xIdx>=0; xIdx--)
- if(board[index(xIdx, maxY - 1)] != null)
- deservesBonus = false;
- if(deservesBonus)
+ for (var column = maxColumn - 1; column >= 0; column--)
+ if (board[index(column, maxRow - 1)] != null)
+ deservesBonus = false;
+ if (deservesBonus)
gameCanvas.score += 500;
- //Checks for game over
- if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1)))
+
+ //Check whether game has finished
+ if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1)))
dialog.show("Game Over. Your score is " + gameCanvas.score);
}
//![2]
-//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)
+//only floods up and right, to see if it can find adjacent same-typed blocks
+function floodMoveCheck(column, row, type) {
+ if (column >= maxColumn || column < 0 || row >= maxRow || row < 0)
return false;
- if(board[index(xIdx, yIdx)] == null)
+ if (board[index(column, row)] == null)
return false;
- var myType = board[index(xIdx, yIdx)].type;
- if(type == myType)
+ var myType = board[index(column, row)].type;
+ if (type == myType)
return true;
- return floodMoveCheck(xIdx + 1, yIdx, myType) ||
- floodMoveCheck(xIdx, yIdx - 1, board[index(xIdx,yIdx)].type);
+ return floodMoveCheck(column + 1, row, myType) || floodMoveCheck(column, row - 1, board[index(column, row)].type);
}
+