diff options
author | Alan Alpert <alan.alpert@nokia.com> | 2009-07-27 06:39:56 (GMT) |
---|---|---|
committer | Alan Alpert <alan.alpert@nokia.com> | 2009-07-27 06:39:56 (GMT) |
commit | 1a5338d8cc96cd07d2340aa2aff5880fd2fdce68 (patch) | |
tree | b2a6f161b835ac44bc6ce8ad3923e3ef646c3583 | |
parent | 5cddf5630ff0e408a84c19b7bb5ebc36871fa025 (diff) | |
download | Qt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.zip Qt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.tar.gz Qt-1a5338d8cc96cd07d2340aa2aff5880fd2fdce68.tar.bz2 |
Repurpose SameGame to be slimmer
No theming and no selected, plus some cleanup.
15 files changed, 42 insertions, 274 deletions
diff --git a/demos/declarative/samegame/README b/demos/declarative/samegame/README index 2f2a728..244b205 100644 --- a/demos/declarative/samegame/README +++ b/demos/declarative/samegame/README @@ -8,5 +8,3 @@ greenStone.png yellowStone.png and are presumably under the same GPL2 license as the rest of kdegames - -The images in the pics/gnome folder are from the gnome games project, and are also GPL2 licensed. diff --git a/demos/declarative/samegame/SameGame.qml b/demos/declarative/samegame/SameGame.qml index e2c2089..1788341 100644 --- a/demos/declarative/samegame/SameGame.qml +++ b/demos/declarative/samegame/SameGame.qml @@ -3,38 +3,38 @@ import Qt 4.6 import "content" Rect { - width: 460 - height: 700 - color: activePalette.window + id: page; width: 460; height: 700; color: activePalette.window Script { source: "content/samegame.js" } - Rect{ + Rect { + id: gameCanvas property int score: 0 - y:20; width:400; height:600; id: gameCanvas; - //For Fixed Size + z:20; y:20; width:400; height:600; color: "white"; pen.width: 1 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/background.png" anchors.fill: parent } + MouseRegion { id: gameMR + anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y); + } + } + Dialog { id: dialog; anchors.centeredIn: parent; z: 21} + Button { + id: btnA; text: "New Game"; onClicked: {initBoard();} + anchors.top: gameCanvas.bottom; anchors.topMargin: 4; anchors.left: gameCanvas.left; } - HorizontalLayout { - anchors.top: gameCanvas.bottom - anchors.topMargin: 10 - anchors.horizontalCenter: parent.horizontalCenter - MediaButton { id: btnA; text: "New Game"; onClicked: {initBoard();} } - MediaButton { id: btnB; text: "Swap Tiles"; onClicked: {swapTileSrc(); dialog.opacity = 1; - dialog.text="Takes effect next game.";} } - Text{ text: "Score: " + gameCanvas.score; width:120; font.size:14 } + Text { + text: "Score: " + gameCanvas.score; width:100; font.size:14 + anchors.top: gameCanvas.bottom; anchors.topMargin: 4; anchors.right: gameCanvas.right; } - SameDialog { - id: dialog - anchors.centeredIn: parent - text: "Hello World" + Text { + text: "Just over 300 lines of QML/JS code!" + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottom: parent.bottom; anchors.bottomMargin: 16; + opacity: SequentialAnimation{ running: true; repeat: true; + NumberAnimation { from: 0; to: 1; duration: 1000; easing: "easeInQuad" } + NumberAnimation { from: 1; to: 0; duration: 1000; easing: "easeInQuad" } + } } } diff --git a/demos/declarative/samegame/TODO b/demos/declarative/samegame/TODO deleted file mode 100644 index 3be2a3a..0000000 --- a/demos/declarative/samegame/TODO +++ /dev/null @@ -1,2 +0,0 @@ -Still to do: - diff --git a/demos/declarative/samegame/content/BoomBlock.qml b/demos/declarative/samegame/content/BoomBlock.qml index ec4430e..a4f4fe9 100644 --- a/demos/declarative/samegame/content/BoomBlock.qml +++ b/demos/declarative/samegame/content/BoomBlock.qml @@ -3,7 +3,6 @@ import Qt 4.6 Item { id:block property bool dying: false property bool spawned: false - property bool selected: false property int type: 0 property int targetX: 0 property int targetY: 0 @@ -11,19 +10,6 @@ Item { id:block x: Follow { enabled: spawned; source: targetX; spring: 2; damping: 0.2 } y: Follow { source: targetY; spring: 2; damping: 0.2 } - //TODO: Replace with an image with a fuzzy effect like KSame - Rect { id: shine; radius:16; anchors.fill:parent; color: "yellow"; - opacity: 0 - opacity: SequentialAnimation{running: selected&&!dying; finishPlaying: true; repeat: true - NumberAnimation{ from: 0; to: 1; }NumberAnimation{ from:1; to:0; }} - } - MouseRegion { - id: gameMR; anchors.fill: parent - onClicked: handleClick(Math.floor(parent.x/width), Math.floor(parent.y/height)); - onEntered: handleHover(Math.floor(parent.x/width), Math.floor(parent.y/height)); - onExited: handleHover(-1,-1); - } - Image { id: img source: { if(type == 0){ @@ -55,7 +41,6 @@ Item { id:block } states: [ - State{ name: "AliveState"; when: spawned == true && dying == false SetProperties { target: img; opacity: 1 } }, diff --git a/demos/declarative/samegame/content/FastBlock.qml b/demos/declarative/samegame/content/FastBlock.qml deleted file mode 100644 index f30d8da..0000000 --- a/demos/declarative/samegame/content/FastBlock.qml +++ /dev/null @@ -1,33 +0,0 @@ -import Qt 4.6 - -Rect { id:block - //Note: These properties are the interface used to control the blocks - property bool dying: false - property bool spawned: false - property bool selected: false - property int type: 0 - property int targetY: 0 - property int targetX: 0 - - color: {if(type==0){"red";}else if(type==1){"blue";}else{"green";}} - pen.width: 1 - pen.color: "black" - opacity: 0 - y: targetY - x: targetX - - MouseRegion { - id: gameMR; anchors.fill: parent - onClicked: handleClick(Math.floor(parent.x/width), Math.floor(parent.y/height)); - } - - states: [ - - State{ name: "AliveState"; when: spawned == true && dying == false - SetProperties { target: block; opacity: 1 } - }, - State{ name: "DeathState"; when: dying == true - SetProperties { target: block; opacity: 0 } - } - ] -} diff --git a/demos/declarative/samegame/content/MediaButton.qml b/demos/declarative/samegame/content/MediaButton.qml deleted file mode 100644 index cbe4265..0000000 --- a/demos/declarative/samegame/content/MediaButton.qml +++ /dev/null @@ -1,41 +0,0 @@ -import Qt 4.6 - -Item { - id: Container - - signal clicked - - property string text - - Image { - id: Image - source: "pics/button.png" - } - Image { - id: Pressed - source: "pics/button-pressed.png" - opacity: 0 - } - MouseRegion { - id: MouseRegion - anchors.fill: Image - onClicked: { Container.clicked(); } - } - Text { - font.bold: true - color: "white" - anchors.centeredIn: Image - text: Container.text - } - width: Image.width - states: [ - State { - name: "Pressed" - when: MouseRegion.pressed == true - SetProperties { - target: Pressed - opacity: 1 - } - } - ] -} diff --git a/demos/declarative/samegame/content/SameDialog.qml b/demos/declarative/samegame/content/SameDialog.qml deleted file mode 100644 index 86248a6..0000000 --- a/demos/declarative/samegame/content/SameDialog.qml +++ /dev/null @@ -1,19 +0,0 @@ -import Qt 4.6 - -Rect { - property string text: "Hello World!" - property int show: 0 - id: page - opacity: 0 - opacity: Behavior { - SequentialAnimation { - NumberAnimation {property: "opacity"; duration: 1500 } - NumberAnimation {property: "opacity"; to: 0; duration: 1500 } - } - } - color: "white" - pen.width: 1 - width: 200 - height: 60 - Text { anchors.centeredIn: parent; text: parent.text } -} diff --git a/demos/declarative/samegame/content/SpinBlock.qml b/demos/declarative/samegame/content/SpinBlock.qml deleted file mode 100644 index 4c8f123..0000000 --- a/demos/declarative/samegame/content/SpinBlock.qml +++ /dev/null @@ -1,47 +0,0 @@ -import Qt 4.6 - -Item { id:block - //Note: These properties are the interface used to control the blocks - property bool dying: false - property bool spawned: false - property bool selected: false - property int type: 0 - property int targetY: 0 - property int targetX: 0 - - AnimatedImage { - source: if(type == 0) { - "pics/gnome/redStone.gif"; - } else if (type == 1) { - "pics/gnome/blueStone.gif"; - } else { - "pics/gnome/greenStone.gif"; - } - paused: !selected - paused: Behavior { to: true; from: false; - NumberAnimation { properties:"currentFrame"; to:0; duration: 200} - } - } - opacity: 0 - y: targetY - x: targetX - y: Behavior { NumberAnimation { properties:"y"; duration: 200 } } - x: Behavior { NumberAnimation { properties:"x"; duration: 200 } } - opacity: Behavior { NumberAnimation { properties:"opacity"; duration: 200 } } - MouseRegion { - id: gameMR; anchors.fill: parent - onClicked: handleClick(Math.floor(parent.x/width), Math.floor(parent.y/height)); - onEntered: handleHover(Math.floor(parent.x/width), Math.floor(parent.y/height)); - onExited: handleHover(Math.floor(parent.x/width), Math.floor(parent.y/height)); - } - - states: [ - - State{ name: "AliveState"; when: spawned == true && dying == false - SetProperties { target: block; opacity: 1 } - }, - State{ name: "DeathState"; when: dying == true - SetProperties { target: block; opacity: 0 } - } - ] -} diff --git a/demos/declarative/samegame/content/pics/button-pressed.png b/demos/declarative/samegame/content/pics/button-pressed.png Binary files differdeleted file mode 100644 index e434d32..0000000 --- a/demos/declarative/samegame/content/pics/button-pressed.png +++ /dev/null diff --git a/demos/declarative/samegame/content/pics/button.png b/demos/declarative/samegame/content/pics/button.png Binary files differdeleted file mode 100644 index 56a63ce..0000000 --- a/demos/declarative/samegame/content/pics/button.png +++ /dev/null diff --git a/demos/declarative/samegame/content/pics/gnome/blueStone.gif b/demos/declarative/samegame/content/pics/gnome/blueStone.gif Binary files differdeleted file mode 100644 index 333efbe..0000000 --- a/demos/declarative/samegame/content/pics/gnome/blueStone.gif +++ /dev/null diff --git a/demos/declarative/samegame/content/pics/gnome/greenStone.gif b/demos/declarative/samegame/content/pics/gnome/greenStone.gif Binary files differdeleted file mode 100644 index 1bc5bf4..0000000 --- a/demos/declarative/samegame/content/pics/gnome/greenStone.gif +++ /dev/null diff --git a/demos/declarative/samegame/content/pics/gnome/redStone.gif b/demos/declarative/samegame/content/pics/gnome/redStone.gif Binary files differdeleted file mode 100644 index b80f901..0000000 --- a/demos/declarative/samegame/content/pics/gnome/redStone.gif +++ /dev/null diff --git a/demos/declarative/samegame/content/pics/qtlogo.png b/demos/declarative/samegame/content/pics/qtlogo.png Binary files differdeleted file mode 100644 index 399bd0b..0000000 --- a/demos/declarative/samegame/content/pics/qtlogo.png +++ /dev/null diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js index ef32230..a7dd82b 100755 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -1,5 +1,4 @@ /* 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; @@ -7,19 +6,8 @@ var tileSize = 40; var maxIndex = maxX*maxY; var board = new Array(maxIndex); var tileSrc = "content/BoomBlock.qml"; -var swapped = false; - -var compSrc; var component; -function swapTileSrc(){ - if(tileSrc == "content/SpinBlock.qml"){ - tileSrc = "content/BoomBlock.qml"; - }else{ - tileSrc = "content/SpinBlock.qml"; - } -} - //Index function used instead of a 2D array function index(xIdx,yIdx){ return xIdx + (yIdx * maxX); @@ -33,11 +21,6 @@ function initBoard() board[i].destroy(); } - //Game size is as many rows/cols fit in the GameCanvas - maxX = Math.floor(gameCanvas.width/tileSize); - maxY = Math.floor(gameCanvas.height/tileSize); - maxIndex = maxX*maxY; - //Initialize Board board = new Array(maxIndex); gameCanvas.score = 0; @@ -47,43 +30,21 @@ function initBoard() startCreatingBlock(xIdx,yIdx); } } - //TODO: a flag that handleMouse uses to ignore clicks when we're loading } var fillFound;//Set after a floodFill call to the number of tiles found -var floodBoard;//Set to 1 if the floodFill ticks off that node (mostly used by floodFill) - -var lastHoveredIdx = -2 -function handleHover(xIdx,yIdx, btn) -{ - //Turn UI x,y into Game x,y - if(index(xIdx, yIdx) == lastHoveredIdx) - return; - //if(btn != 0) - // return; - //Sets 'selected' on tile underneath and all connected - floodFill(xIdx, yIdx, -1, "hover"); - //Could use the fillFound value to tell the player how many stones/points - - //Resets any previously selected - if(board[lastHoveredIdx] != null){ - lastX = lastHoveredIdx % maxX; - lastY = Math.floor(lastHoveredIdx / maxX); - floodFill(lastX, lastY, -1, "unhover"); - } - lastHoveredIdx = index(xIdx, yIdx); -} - +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(xIdx,yIdx) +function handleClick(x,y) { + xIdx = Math.floor(x/tileSize); + yIdx = Math.floor(y/tileSize); if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) return; if(board[index(xIdx, yIdx)] == null) return; - //If it's a valid tile, remove it and all connected - //floodFill does nothing if it's not connected to any other tiles. - floodFill(xIdx,yIdx, -1, "kill"); + //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) return; gameCanvas.score += (fillFound - 1) * (fillFound - 1); @@ -91,18 +52,7 @@ function handleClick(xIdx,yIdx) victoryCheck(); } -/* - the floodFill function does something on all tiles connected to the - given Game position. Connected requires the adjacency of one or more - tiles of the same type. Note that if there is no tile, or the tile - is not adjacent to a similar typed tile, then it will not do anything. - - Since many things need this flood functionality, a string command is - given telling it what to do with these tiles. - - cmd = "kill" is the removal case, cmd = "hover" sets selection -*/ -function floodFill(xIdx,yIdx,type, cmd) +function floodFill(xIdx,yIdx,type) { if(board[index(xIdx, yIdx)] == null) return; @@ -117,29 +67,17 @@ function floodFill(xIdx,yIdx,type, cmd) } if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) return; - if(floodBoard[index(xIdx, yIdx)] == 1) + if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].type)) return; - if(!first && type != board[index(xIdx,yIdx)].type) - return; floodBoard[index(xIdx, yIdx)] = 1; - floodFill(xIdx+1,yIdx,type,cmd); - floodFill(xIdx-1,yIdx,type,cmd); - floodFill(xIdx,yIdx+1,type,cmd); - floodFill(xIdx,yIdx-1,type,cmd); - if(first==true && fillFound == 0){ - //TODO: Provide a way to inform the delegate + 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 - } - if(cmd == "kill"){ - board[index(xIdx,yIdx)].dying = true; - board[index(xIdx,yIdx)] = null; - }else if(cmd == "hover"){ - board[index(xIdx,yIdx)].selected = true; - }else if(cmd == "unhover"){ - board[index(xIdx,yIdx)].selected = false; - }else{ - print ("Flood Error"); - } + board[index(xIdx,yIdx)].dying = true; + board[index(xIdx,yIdx)] = null; fillFound += 1; } @@ -191,17 +129,12 @@ function victoryCheck() if(deservesBonus) gameCanvas.score += 500; //Checks for game over - if(deservesBonus || noMoreMoves()){ + if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))){ dialog.text = "Game Over. Your score is " + gameCanvas.score; dialog.opacity = 1; } } -function noMoreMoves() -{ - return !floodMoveCheck(0, maxY-1, -1); -} - //only floods up and right, to see if it can find adjacent same-typed tiles function floodMoveCheck(xIdx, yIdx, type) { @@ -212,15 +145,13 @@ function floodMoveCheck(xIdx, yIdx, type) myType = board[index(xIdx, yIdx)].type; if(type == myType) return true; - aT = myType; - bT = myType; - return floodMoveCheck(xIdx + 1, yIdx, aT) || floodMoveCheck(xIdx, yIdx - 1, bT); + return floodMoveCheck(xIdx + 1, yIdx, myType) || + floodMoveCheck(xIdx, yIdx - 1, board[index(xIdx,yIdx)].type); } //If the component isn't ready, then the signal doesn't include the game x,y //So we store any x,y sent that we couldn't create at the time, and use those //if we are triggered by the signal. -//Need a simpler method of doing this? var waitStack = new Array(maxIndex); var waitTop = -1; @@ -263,15 +194,11 @@ function finishCreatingBlock(xIdx,yIdx){ } function startCreatingBlock(xIdx,yIdx){ - if(component!=null && compSrc == tileSrc){ + if(component!=null){ finishCreatingBlock(xIdx,yIdx); return; } - if(component!=null){//Changed source - //delete component; //Does the engine handle this? - compSrc = tileSrc; - } component = createComponent(tileSrc); if(finishCreatingBlock(xIdx,yIdx)) return; |