diff options
author | Qt Continuous Integration System <qt-info@nokia.com> | 2010-10-30 00:21:46 (GMT) |
---|---|---|
committer | Qt Continuous Integration System <qt-info@nokia.com> | 2010-10-30 00:21:46 (GMT) |
commit | 856711caba2736ca9330f086635b418322462c3a (patch) | |
tree | 9f2b40f9899813a331f2114cc7770c46f127dab1 /demos/declarative | |
parent | 967afc717ffd411e22ce94de77cf54284815b27e (diff) | |
parent | 1cb7e3503a907719aebb98096c302a8327e9804d (diff) | |
download | Qt-856711caba2736ca9330f086635b418322462c3a.zip Qt-856711caba2736ca9330f086635b418322462c3a.tar.gz Qt-856711caba2736ca9330f086635b418322462c3a.tar.bz2 |
Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
* 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1:
Add autotest for QScriptExtensionPlugin
Improve QtScript test coverage
Don't crash when null-pointer is passed to qScriptValueFromValue()
Simplify object lifetime management when moving objects to a QThread
samegame: white space
Move list of trusted benchmarks out of QA dungeon and into Qt
Don't put Objective-C sources in SOURCES
mkspecs: Emit warning when including g++.conf directly
Diffstat (limited to 'demos/declarative')
-rwxr-xr-x | demos/declarative/samegame/SamegameCore/samegame.js | 198 |
1 files changed, 108 insertions, 90 deletions
diff --git a/demos/declarative/samegame/SamegameCore/samegame.js b/demos/declarative/samegame/SamegameCore/samegame.js index 9266767..e618a10 100755 --- a/demos/declarative/samegame/SamegameCore/samegame.js +++ b/demos/declarative/samegame/SamegameCore/samegame.js @@ -9,12 +9,14 @@ var scoresURL = ""; var gameDuration; var component = Qt.createComponent(blockSrc); -//Index function used instead of a 2D array -function index(column,row) { - return column + (row * maxColumn); +// Index function used instead of a 2D array +function index(column, row) +{ + return column + row * maxColumn; } -function timeStr(msecs) { +function timeStr(msecs) +{ var secs = Math.floor(msecs/1000); var m = Math.floor(secs/60); var ret = "" + m + "m " + (secs%60) + "s"; @@ -23,47 +25,48 @@ function timeStr(msecs) { function startNewGame() { - //Delete blocks from previous game - for(var i = 0; i<maxIndex; i++){ - if(board[i] != null) + // Delete blocks from previous game + for (var i = 0; i < maxIndex; i++) { + if (board[i] != null) board[i].destroy(); } - //Calculate board size + // Calculate board size maxColumn = Math.floor(gameCanvas.width/gameCanvas.blockSize); maxRow = Math.floor(gameCanvas.height/gameCanvas.blockSize); - maxIndex = maxRow*maxColumn; + maxIndex = maxRow * maxColumn; - //Close dialogs + // Close dialogs nameInputDialog.forceClose(); dialog.forceClose(); - //Initialize Board + // Initialize Board board = new Array(maxIndex); gameCanvas.score = 0; - for(var column=0; column<maxColumn; column++){ - for(var row=0; row<maxRow; row++){ - board[index(column,row)] = null; - createBlock(column,row); + for (var column = 0; column < maxColumn; column++) { + for (var row = 0; row < maxRow; row++) { + board[index(column, row)] = null; + createBlock(column, row); } } gameDuration = new Date(); } -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 +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) { var column = Math.floor(x/gameCanvas.blockSize); var row = Math.floor(y/gameCanvas.blockSize); - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return; - //If it's a valid block, 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) + if (fillFound <= 0) return; gameCanvas.score += (fillFound - 1) * (fillFound - 1); shuffleDown(); @@ -72,65 +75,65 @@ function handleClick(x,y) function floodFill(column,row,type) { - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return; var first = false; - if(type == -1){ + if (type == -1) { first = true; type = board[index(column,row)].type; - - //Flood fill initialization + + // Flood fill initialization fillFound = 0; floodBoard = new Array(maxIndex); } - if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(floodBoard[index(column, row)] == 1 || (!first && type != board[index(column,row)].type)) + if (floodBoard[index(column, row)] == 1 || (!first && type != board[index(column, row)].type)) return; 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)].dying = true; - board[index(column,row)] = null; + 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)].dying = true; + board[index(column, row)] = null; fillFound += 1; } function shuffleDown() { - //Fall down - for(var column=0; column<maxColumn; column++){ + // Fall down + for (var column = 0; column < maxColumn; column++) { var fallDist = 0; - for(var row=maxRow-1; row>=0; row--){ - if(board[index(column,row)] == 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(column,row)]; - obj.y = (row+fallDist) * gameCanvas.blockSize; - board[index(column,row+fallDist)] = obj; - board[index(column,row)] = null; + } else { + if (fallDist > 0) { + var obj = board[index(column, row)]; + obj.y = (row + fallDist) * gameCanvas.blockSize; + board[index(column, row + fallDist)] = obj; + board[index(column, row)] = null; } } } } - //Fall to the left + // Fall to the left fallDist = 0; - for(column=0; column<maxColumn; column++){ - if(board[index(column, maxRow - 1)] == null){ + for (column = 0; column < maxColumn; column++) { + if (board[index(column, maxRow - 1)] == null) { fallDist += 1; - }else{ - if(fallDist > 0){ - for(row=0; row<maxRow; row++){ - obj = board[index(column,row)]; - if(obj == null) + } else { + if (fallDist > 0) { + for (row = 0; row < maxRow; row++) { + obj = board[index(column, row)]; + if (obj == null) continue; - obj.x = (column-fallDist) * gameCanvas.blockSize; - board[index(column-fallDist,row)] = obj; - board[index(column,row)] = null; + obj.x = (column - fallDist) * gameCanvas.blockSize; + board[index(column - fallDist,row)] = obj; + board[index(column, row)] = null; } } } @@ -139,58 +142,59 @@ function shuffleDown() function victoryCheck() { - //awards bonuses for no blocks left + // Awards bonuses for no blocks left var deservesBonus = true; - for(var column=maxColumn-1; column>=0; column--) - if(board[index(column, maxRow - 1)] != null) + for (var column = maxColumn - 1; column >= 0; column--) + if (board[index(column, maxRow - 1)] != null) deservesBonus = false; - if(deservesBonus) + if (deservesBonus) gameCanvas.score += 500; - //Checks for game over - if(deservesBonus || !(floodMoveCheck(0,maxRow-1, -1))){ + // Checks for game over + if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1))) { gameDuration = new Date() - gameDuration; nameInputDialog.show("You won! Please enter your name: "); nameInputDialog.initialWidth = nameInputDialog.text.width + 20; - if(nameInputDialog.name == "") + if (nameInputDialog.name == "") nameInputDialog.width = nameInputDialog.initialWidth; - nameInputDialog.text.opacity = 0;//Just a spacer + nameInputDialog.text.opacity = 0; // Just a spacer } } -//only floods up and right, to see if it can find adjacent same-typed blocks +// 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) + if (column >= maxColumn || column < 0 || row >= maxRow || row < 0) return false; - if(board[index(column, row)] == null) + if (board[index(column, row)] == null) return false; var myType = board[index(column, row)].type; - if(type == myType) + if (type == myType) return true; return floodMoveCheck(column + 1, row, myType) || - floodMoveCheck(column, row - 1, board[index(column,row)].type); + floodMoveCheck(column, row - 1, board[index(column, row)].type); } -function createBlock(column,row){ +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.status == Component.Ready){ + if (component.status == Component.Ready) { var dynamicObject = component.createObject(gameCanvas); - if(dynamicObject == null){ + if (dynamicObject == null) { console.log("error creating block"); console.log(component.errorString()); return false; } dynamicObject.type = Math.floor(Math.random() * 3); - dynamicObject.x = column*gameCanvas.blockSize; - dynamicObject.y = row*gameCanvas.blockSize; + dynamicObject.x = column * gameCanvas.blockSize; + dynamicObject.y = row * gameCanvas.blockSize; dynamicObject.width = gameCanvas.blockSize; dynamicObject.height = gameCanvas.blockSize; dynamicObject.spawned = true; - board[index(column,row)] = dynamicObject; - }else{ + board[index(column, row)] = dynamicObject; + } else { console.log("error loading block component"); console.log(component.errorString()); return false; @@ -198,23 +202,35 @@ function createBlock(column,row){ return true; } -function saveHighScore(name) { - if(scoresURL!="") +function saveHighScore(name) +{ + if (scoresURL != "") sendHighScore(name); - //OfflineStorage - var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores",100); + // Offline storage + 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(gameDuration/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)'); tx.executeSql(dataStr, data); - //Only show results for the current grid size - var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "'+maxColumn+"x"+maxRow+'" ORDER BY score desc LIMIT 10'); + // Only show results for the current grid size + var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "' + + maxColumn + "x" + maxRow + '" ORDER BY score desc LIMIT 10'); var r = "\nHIGH SCORES for this grid size\n\n" - for(var i = 0; i < rs.rows.length; i++){ - r += (i+1)+". " + rs.rows.item(i).name +' got ' + for (var i = 0; i < rs.rows.length; i++) { + r += (i+1) + ". " + rs.rows.item(i).name + ' got ' + rs.rows.item(i).score + ' points in ' + rs.rows.item(i).time + ' seconds.\n'; } @@ -223,13 +239,15 @@ function saveHighScore(name) { ); } -function sendHighScore(name) { +function sendHighScore(name) +{ var postman = new XMLHttpRequest() - var postData = "name="+name+"&score="+gameCanvas.score - +"&gridSize="+maxColumn+"x"+maxRow +"&time="+Math.floor(gameDuration/1000); + var postData = "name=" + name + "&score=" + gameCanvas.score + + "&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() { + postman.onreadystatechange = function() { if (postman.readyState == postman.DONE) { dialog.show("Your score has been uploaded."); } |