diff options
Diffstat (limited to 'examples/declarative/tutorials/samegame/samegame4/content/samegame.js')
-rwxr-xr-x | examples/declarative/tutorials/samegame/samegame4/content/samegame.js | 117 |
1 files changed, 55 insertions, 62 deletions
diff --git a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js index a905f7d..0b1c6d6 100755 --- a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js @@ -1,38 +1,28 @@ /* This script file handles the game logic */ -//Note that X/Y referred to here are in game coordinates -var maxColumn = 10;//Nums are for gameCanvas.tileSize 40 +var maxColumn = 10; var maxRow = 15; var maxIndex = maxColumn*maxRow; var board = new Array(maxIndex); -var tileSrc = "content/BoomBlock.qml"; -//var scoresURL = "http://qtfx-nokia.trolltech.com.au/samegame/scores.php"; +var component; var scoresURL = ""; -var timer; -var component = createComponent(tileSrc); +var gameDuration; //Index function used instead of a 2D array function index(column,row) { return column + (row * maxColumn); } -function timeStr(msecs) { - var secs = Math.floor(msecs/1000); - var m = Math.floor(secs/60); - var ret = "" + m + "m " + (secs%60) + "s"; - return ret; -} - -function initBoard() +function startNewGame() { for(var i = 0; i<maxIndex; i++){ - //Delete old blocks + //Delete blocks from previous game if(board[i] != null) board[i].destroy(); } //Calculate board size - maxColumn = Math.floor(gameCanvas.width/gameCanvas.tileSize); - maxRow = Math.floor(gameCanvas.height/gameCanvas.tileSize); + maxColumn = Math.floor(gameCanvas.width/gameCanvas.blockSize); + maxRow = Math.floor(gameCanvas.height/gameCanvas.blockSize); maxIndex = maxRow*maxColumn; //Close dialogs @@ -49,21 +39,52 @@ function initBoard() } } - timer = new Date(); + gameDuration = new Date(); +} + +function createBlock(column,row){ + if(component==null) + component = createComponent("content/BoomBlock.qml"); + + // 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){ + print("error creating block"); + print(component.errorsString()); + return false; + } + dynamicObject.type = Math.floor(Math.random() * 3); + dynamicObject.parent = gameCanvas; + dynamicObject.x = column*gameCanvas.blockSize; + dynamicObject.targetX = column*gameCanvas.blockSize; + dynamicObject.targetY = row*gameCanvas.blockSize; + dynamicObject.width = gameCanvas.blockSize; + dynamicObject.height = gameCanvas.blockSize; + dynamicObject.spawned = true; + board[index(column,row)] = dynamicObject; + }else{ + print("error loading block component"); + print(component.errorsString()); + return false; + } + return true; } -var fillFound;//Set after a floodFill call to the number of tiles found +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 -//NOTE: Be careful with vars named x,y, as the calling object's x,y are still in scope -function handleClick(x,y) + +function handleClick(xPos,yPos) { - var column = Math.floor(x/gameCanvas.tileSize); - var row = Math.floor(y/gameCanvas.tileSize); + 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(column, row)] == null) return; - //If it's a valid tile, remove it and all connected (does nothing if it's not connected) + //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; @@ -95,7 +116,7 @@ function floodFill(column,row,type) floodFill(column,row+1,type); floodFill(column,row-1,type); if(first==true && fillFound == 0) - return;//Can't remove single tiles + return;//Can't remove single blocks board[index(column,row)].dying = true; board[index(column,row)] = null; fillFound += 1; @@ -112,7 +133,7 @@ function shuffleDown() }else{ if(fallDist > 0){ var obj = board[index(column,row)]; - obj.targetY += fallDist * gameCanvas.tileSize; + obj.targetY += fallDist * gameCanvas.blockSize; board[index(column,row+fallDist)] = obj; board[index(column,row)] = null; } @@ -130,7 +151,7 @@ function shuffleDown() obj = board[index(column,row)]; if(obj == null) continue; - obj.targetX -= fallDist * gameCanvas.tileSize; + obj.targetX -= fallDist * gameCanvas.blockSize; board[index(column-fallDist,row)] = obj; board[index(column,row)] = null; } @@ -141,21 +162,22 @@ function shuffleDown() function victoryCheck() { - //awards bonuses for no tiles left + //Award bonus points if no blocks left var deservesBonus = true; 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 + + //Check whether game has finished if(deservesBonus || !(floodMoveCheck(0,maxRow-1, -1))){ - timer = new Date() - timer; + gameDuration = new Date() - gameDuration; nameInputDialog.show("You won! Please enter your name: "); } } -//only floods up and right, to see if it can find adjacent same-typed tiles +//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) @@ -169,35 +191,6 @@ function floodMoveCheck(column, row, type) floodMoveCheck(column, row - 1, board[index(column,row)].type); } -function createBlock(column,row){ - // 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){ - var dynamicObject = component.createObject(); - if(dynamicObject == null){ - print("error creating block"); - print(component.errorsString()); - return false; - } - dynamicObject.type = Math.floor(Math.random() * 3); - dynamicObject.parent = gameCanvas; - dynamicObject.x = column*gameCanvas.tileSize; - dynamicObject.targetX = column*gameCanvas.tileSize; - dynamicObject.targetY = row*gameCanvas.tileSize; - dynamicObject.width = gameCanvas.tileSize; - dynamicObject.height = gameCanvas.tileSize; - dynamicObject.spawned = true; - board[index(column,row)] = dynamicObject; - }else{//isError or isLoading - print("error loading block component"); - print(component.errorsString()); - return false; - } - return true; -} - //![2] function saveHighScore(name) { if(scoresURL!="") @@ -205,7 +198,7 @@ function saveHighScore(name) { //OfflineStorage var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores",100); var dataStr = "INSERT INTO Scores VALUES(?, ?, ?, ?)"; - var data = [name, gameCanvas.score, maxColumn+"x"+maxRow ,Math.floor(timer/1000)]; + var data = [name, gameCanvas.score, maxColumn+"x"+maxRow ,Math.floor(gameDuration/1000)]; db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize TEXT, time NUMBER)'); @@ -228,7 +221,7 @@ function saveHighScore(name) { function sendHighScore(name) { var postman = new XMLHttpRequest() var postData = "name="+name+"&score="+gameCanvas.score - +"&gridSize="+maxColumn+"x"+maxRow +"&time="+Math.floor(timer/1000); + +"&gridSize="+maxColumn+"x"+maxRow +"&time="+Math.floor(gameDuration/1000); postman.open("POST", scoresURL, true); postman.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postman.onreadystatechange = function() { |