From 09e9472d5548b21d31c6b246babb3e8840cf45c5 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Wed, 15 Jul 2009 18:57:18 +1000 Subject: Restyle SameGame demo based on feedback -Uses a more familar rule set -Default theme swapped -Sizing altered. Now the tiles are 40x40 so as to be able to be pretty and have shiny animations, board size is set by what can fit in the game canvas. --- demos/declarative/samegame/SameGame.qml | 11 ++- demos/declarative/samegame/content/BoomBlock.qml | 17 +---- demos/declarative/samegame/content/FastBlock.qml | 14 ---- demos/declarative/samegame/content/samegame.js | 91 ++++++++++++++++++------ 4 files changed, 80 insertions(+), 53 deletions(-) diff --git a/demos/declarative/samegame/SameGame.qml b/demos/declarative/samegame/SameGame.qml index 0e5bb0f..c929c91 100644 --- a/demos/declarative/samegame/SameGame.qml +++ b/demos/declarative/samegame/SameGame.qml @@ -1,17 +1,22 @@ import "content" Rect { - width: 400 + width: 460 height: 700 color: "white" Script { source: "content/samegame.js" } Rect{ property int score: 0 - x:20; y:20; width:360; height:600; id: gameCanvas; + y:20; width:400; height:600; id: gameCanvas; + //For Fixed Size + anchors.horizontalCenter: parent.horizontalCenter + //For flexible width + //anchors.left: parent.left; anchors.leftMargin: 30 + //anchors.right: parent.right; anchors.rightMargin: 30 color: "white" pen.width: 1 Image { id:background; - source: "content/pics/qtlogo.png" + source: "content/pics/background.png" anchors.fill: parent } diff --git a/demos/declarative/samegame/content/BoomBlock.qml b/demos/declarative/samegame/content/BoomBlock.qml index 5eaaade..68e0e1a 100644 --- a/demos/declarative/samegame/content/BoomBlock.qml +++ b/demos/declarative/samegame/content/BoomBlock.qml @@ -5,9 +5,8 @@ Item { id:block property int targetX: 0 property int targetY: 0 - x: targetX + x: Follow { source: targetX; spring: 1.2; damping: 0.1 } y: Follow { source: targetY; spring: 1.2; damping: 0.1 } - //y: Behavior { NumberAnimation { properties:"y"; duration: 200 } } Image { id: img source: {if(type==0){"pics/redStone.png";}else if(type==1){"pics/blueStone.png";}else{"pics/greenStone.png";}} @@ -31,18 +30,4 @@ Item { id:block SetProperties { target: img; opacity: 0 } } ] -// transitions: [ -// Transition { -// fromState: "SpawnState" -// NumberAnimation { properties: "opacity"; duration: 200 } -// }, -// Transition { -// toState: "DeathState" -// SequentialAnimation { -// NumberAnimation { properties: "opacity"; duration: 200 } -// //TODO: Warning about following line, if it works -// //RunScriptAction { script: page.destroy() } -// } -// } -// ] } diff --git a/demos/declarative/samegame/content/FastBlock.qml b/demos/declarative/samegame/content/FastBlock.qml index 3d14959..04eb59b 100644 --- a/demos/declarative/samegame/content/FastBlock.qml +++ b/demos/declarative/samegame/content/FastBlock.qml @@ -24,18 +24,4 @@ Rect { id:block SetProperties { target: block; opacity: 0 } } ] -// transitions: [ -// Transition { -// fromState: "SpawnState" -// NumberAnimation { properties: "opacity"; duration: 200 } -// }, -// Transition { -// toState: "DeathState" -// SequentialAnimation { -// NumberAnimation { properties: "opacity"; duration: 200 } -// //TODO: Warning about following line, if it works -// //RunScriptAction { script: page.destroy() } -// } -// } -// ] } diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js index f3bb5b6..fe5ac87 100644 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -1,11 +1,11 @@ /* This script file handles the game logic */ -var maxIdx = 18/2;//Nums are for tileSize 20 (desired tile size but too slow) -var maxY = 30/2; +var maxX = 10;//Nums are for tileSize 40 +var maxY = 15; var tileSize = 40; -var maxIndex = maxIdx*maxY; +var maxIndex = maxX*maxY; var board = new Array(maxIndex); -var tileSrc = "content/FastBlock.qml"; +var tileSrc = "content/BoomBlock.qml"; var backSrc = "content/pics/background.png"; var swapped = false; @@ -26,20 +26,27 @@ function swapTileSrc(){ } function index(xIdx,yIdx){ - return xIdx + (yIdx * maxIdx); + return xIdx + (yIdx * maxX); } function initBoard() { + for(i = 0; i= maxIdx || xIdx < 0 || yIdx >= maxY || yIdx < 0) + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) return; if(board[index(xIdx, yIdx)] == null) return; removed = 0; + floodBoard = new Array(maxIndex); floodKill(xIdx,yIdx, -1); - gameCanvas.score += removed * removed; + if(removed <= 0) + return; + gameCanvas.score += (removed - 1) * (removed - 1); shuffleDown(); + victoryCheck(); } function floodKill(xIdx,yIdx,type) { - if(xIdx >= maxIdx || xIdx < 0 || yIdx >= maxY || yIdx < 0) + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(floodBoard[index(xIdx, yIdx)] == 1) return; if(board[index(xIdx, yIdx)] == null) return; + var first = false; if(type == -1){ type = board[index(xIdx,yIdx)].type; + first = true; }else{ if(type != board[index(xIdx,yIdx)].type) return; } - board[index(xIdx,yIdx)].dying = true; - board[index(xIdx,yIdx)] = null;//They'll have to destroy themselves(can we do that?) - removed += 1; + floodBoard[index(xIdx, yIdx)] = 1; floodKill(xIdx+1,yIdx,type); floodKill(xIdx-1,yIdx,type); floodKill(xIdx,yIdx+1,type); floodKill(xIdx,yIdx-1,type); + if(first==true && removed == 0){ + //TODO: Provide a way to inform the delegate + return;//Can't remove single tiles + } + board[index(xIdx,yIdx)].dying = true; + board[index(xIdx,yIdx)] = null;//They'll have to destroy themselves(can we do that?) + removed += 1; } function shuffleDown() { - for(xIdx=0; xIdx=0; yIdx--){ if(board[index(xIdx,yIdx)] == null){ @@ -101,6 +123,35 @@ function shuffleDown() } } } + //Fall to the left + fallDist = 0; + for(xIdx=0; xIdx 0){ + for(yIdx=0; yIdx=0; xIdx--) + if(board[index(xIdx, maxY - 1)] != null) + deservesBonus = false; + if(deservesBonus) + gameCanvas.score += 250; } //Need a simpler method of doing this? @@ -108,14 +159,14 @@ var waitStack = new Array(maxIndex); var waitTop = -1; function finishCreatingBlock(xIdx,yIdx){ - //TODO: Doc that the 'xIdx', 'yIdx' here are hidden properties from the calling QFxItem + //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) if(waitTop == -1) return;//Don't have a previously stored (xIdx,yIdx) - xIdx = waitStack[waitTop] % maxIdx; - yIdx = Math.floor(waitStack[waitTop] / maxIdx); + xIdx = waitStack[waitTop] % maxX; + yIdx = Math.floor(waitStack[waitTop] / maxX); waitTop -= 1; } dynamicObject = component.createObject(); -- cgit v0.12