Refactoring samegame
[profile/ivi/qtdeclarative.git] / examples / demos / samegame / content / GameArea.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42 import QtQuick.Particles 2.0
43 import "samegame.js" as Logic
44
45 Item {
46     id: gameCanvas
47     property bool gameOver: true
48     property int score: 0
49     property int highScore: 0
50     property int moves: 0
51     property string mode: ""
52     property ParticleSystem ps: particleSystem
53     //For easy theming
54     property alias backgroundVisible: bg.visible
55     property string background: "gfx/background.png"
56     property string blockFile: "Block.qml"
57     onBlockFileChanged: Logic.changeBlock(blockFile);
58     property alias particlePack: auxLoader.source
59     //For multiplayer
60     property int score2: 0
61     property int curTurn: 1
62     property bool autoTurnChange: false
63     signal swapPlayers
64     property bool swapping: false
65     //onSwapPlayers: if (autoTurnChange) Logic.turnChange();//Now implemented below
66     //For puzzle
67     property url level
68     property bool puzzleWon: false
69     signal puzzleLost //Since root is tracking the puzzle progress
70     function showPuzzleEnd (won) {
71         if (won) {
72             smokeParticle.color = Qt.rgba(0,1,0,0);
73             puzzleWin.play();
74         } else {
75             smokeParticle.color = Qt.rgba(1,0,0,0);
76             puzzleFail.play();
77             puzzleLost();
78         }
79     }
80     function showPuzzleGoal (str) {
81         puzzleTextBubble.opacity = 1;
82         puzzleTextLabel.text = str;
83     }
84     Image {
85         id: bg
86         z: -1
87         anchors.fill: parent
88         source: background;
89         fillMode: Image.PreserveAspectCrop
90     }
91
92     MouseArea {
93         anchors.fill: parent; onClicked: {
94             if (puzzleTextBubble.opacity == 1) {
95                 puzzleTextBubble.opacity = 0;
96                 Logic.finishLoadingMap();
97             } else if (!swapping) {
98                 Logic.handleClick(mouse.x,mouse.y);
99             }
100         }
101     }
102
103     Image {
104         id: highScoreTextBubble
105         opacity: mode == "arcade" && gameOver && gameCanvas.score == gameCanvas.highScore ? 1 : 0
106         Behavior on opacity { NumberAnimation {} }
107         anchors.centerIn: parent
108         z: 10
109         source: "gfx/bubble-highscore.png"
110         Image {
111             anchors.centerIn: parent
112             source: "gfx/text-highscore-new.png"
113             rotation: -10
114         }
115     }
116
117     Image {
118         id: puzzleTextBubble
119         anchors.centerIn: parent
120         opacity: 0
121         Behavior on opacity { NumberAnimation {} }
122         z: 10
123         source: "gfx/bubble-puzzle.png"
124         Connections {
125             target: gameCanvas
126             onModeChanged: if (mode != "puzzle" && puzzleTextBubble.opacity > 0) puzzleTextBubble.opacity = 0;
127         }
128         Text {
129             id: puzzleTextLabel
130             width: parent.width - 24
131             anchors.centerIn: parent
132             horizontalAlignment: Text.AlignHCenter
133             color: "white"
134             font.pixelSize: 24
135             font.bold: true
136             wrapMode: Text.WordWrap
137         }
138     }
139     onModeChanged: {
140         p1WonImg.opacity = 0;
141         p2WonImg.opacity = 0;
142     }
143     SmokeText { id: puzzleWin; source: "gfx/icon-ok.png"; system: particleSystem }
144     SmokeText { id: puzzleFail; source: "gfx/icon-fail.png"; system: particleSystem }
145
146     onSwapPlayers: {
147         smokeParticle.color = "yellow"
148         Logic.turnChange();
149         if (curTurn == 1) {
150             p1Text.play();
151         } else {
152             p2Text.play();
153         }
154         clickDelay.running = true;
155     }
156     SequentialAnimation {
157         id: clickDelay
158         ScriptAction { script: gameCanvas.swapping = true; }
159         PauseAnimation { duration: 750 }
160         ScriptAction { script: gameCanvas.swapping = false; }
161     }
162
163     SmokeText {
164         id: p1Text; source: "gfx/text-p1-go.png";
165         system: particleSystem; playerNum: 1
166         opacity: p1WonImg.opacity + p2WonImg.opacity > 0 ? 0 : 1
167     }
168
169     SmokeText {
170         id: p2Text; source: "gfx/text-p2-go.png";
171         system: particleSystem; playerNum: 2
172         opacity: p1WonImg.opacity + p2WonImg.opacity > 0 ? 0 : 1
173     }
174
175     onGameOverChanged: {
176         if (gameCanvas.mode == "multiplayer") {
177             if (gameCanvas.score >= gameCanvas.score2) {
178                 p1WonImg.opacity = 1;
179             } else {
180                 p2WonImg.opacity = 1;
181             }
182         }
183     }
184     Image {
185         id: p1WonImg
186         source: "gfx/text-p1-won.png"
187         anchors.centerIn: parent
188         opacity: 0
189         Behavior on opacity { NumberAnimation {} }
190         z: 10
191     }
192     Image {
193         id: p2WonImg
194         source: "gfx/text-p2-won.png"
195         anchors.centerIn: parent
196         opacity: 0
197         Behavior on opacity { NumberAnimation {} }
198         z: 10
199     }
200
201     ParticleSystem{
202         id: particleSystem;
203         anchors.fill: parent
204         z: 5
205         ImageParticle {
206             id: smokeParticle
207             groups: ["smoke"]
208             source: "gfx/particle-smoke.png"
209             alpha: 0.1
210             alphaVariation: 0.1
211             color: "yellow"
212         }
213         Loader {
214             id: auxLoader
215             anchors.fill: parent
216             source: "PrimaryPack.qml"
217             onItemChanged: {
218                 if (item && "particleSystem" in item)
219                     item.particleSystem = particleSystem
220                 if (item && "gameArea" in item)
221                     item.gameArea = gameCanvas
222             }
223         }
224     }
225 }
226