summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-07-15 08:57:18 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-07-15 08:57:18 (GMT)
commit09e9472d5548b21d31c6b246babb3e8840cf45c5 (patch)
treee8686adb16fd34fdad7192639193deabdee6d30c
parent25d3c3bcf1bebcb033699a5318eb86e22a950e2e (diff)
downloadQt-09e9472d5548b21d31c6b246babb3e8840cf45c5.zip
Qt-09e9472d5548b21d31c6b246babb3e8840cf45c5.tar.gz
Qt-09e9472d5548b21d31c6b246babb3e8840cf45c5.tar.bz2
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.
-rw-r--r--demos/declarative/samegame/SameGame.qml11
-rw-r--r--demos/declarative/samegame/content/BoomBlock.qml17
-rw-r--r--demos/declarative/samegame/content/FastBlock.qml14
-rw-r--r--demos/declarative/samegame/content/samegame.js91
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<maxIndex; i++){
+ //Delete old blocks
+ if(board[i] != null)
+ board[i].destroy();
+ }
+
background.source = backSrc;
swapped = false;
+ maxX = Math.floor(gameCanvas.width/tileSize);
+ maxY = Math.floor(gameCanvas.height/tileSize);
+ maxIndex = maxX*maxY;
+ board = new Array(maxIndex);
gameCanvas.score = 0;
- for(xIdx=0; xIdx<maxIdx; xIdx++){
+
+ for(xIdx=0; xIdx<maxX; xIdx++){
for(yIdx=0; yIdx<maxY; yIdx++){
- if(board[index(xIdx,yIdx)] != null){
- //Delete old blocks
- board[index(xIdx,yIdx)].destroy();
- }
board[index(xIdx,yIdx)] = null;
startCreatingBlock(xIdx,yIdx);
}
@@ -48,45 +55,60 @@ function initBoard()
}
var removed;
+var floodBoard;
function handleClick(x,y)
{
//NOTE: Be careful with vars named x,y - they can set to the calling object?
xIdx = Math.floor(x/tileSize);
yIdx = Math.floor(y/tileSize);
- if(xIdx >= 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<maxIdx; xIdx++){
+ //Fall down
+ for(xIdx=0; xIdx<maxX; xIdx++){
fallDist = 0;
for(yIdx=maxY-1; yIdx>=0; yIdx--){
if(board[index(xIdx,yIdx)] == null){
@@ -101,6 +123,35 @@ function shuffleDown()
}
}
}
+ //Fall to the left
+ fallDist = 0;
+ for(xIdx=0; xIdx<maxX; xIdx++){
+ if(board[index(xIdx, maxY - 1)] == null){
+ fallDist += 1;
+ }else{
+ if(fallDist > 0){
+ for(yIdx=0; yIdx<maxY; yIdx++){
+ obj = board[index(xIdx,yIdx)];
+ if(obj == null)
+ continue;
+ obj.targetX -= fallDist * tileSize;
+ board[index(xIdx-fallDist,yIdx)] = obj;
+ board[index(xIdx,yIdx)] = null;
+ }
+ }
+ }
+ }
+}
+
+function victoryCheck()
+{
+ //Only awards bonuses at the moment
+ deservesBonus = true;
+ for(xIdx=maxX-1; xIdx>=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();