0185ab50dcd5fdb69e23eddcdeb389a08e78d458
[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 int blockSize: 32
52     property string mode: ""
53     property ParticleSystem ps: particleSystem
54     //For easy theming
55     property alias backgroundVisible: bg.visible
56     property string background: "gfx/background.png"
57     property string blockFile: "Block.qml"
58     onBlockFileChanged: Logic.changeBlock(blockFile);
59     property alias particlePack: auxLoader.source
60     //For multiplayer
61     property int score2: 0
62     property int curTurn: 1
63     property bool autoTurnChange: false
64     signal swapPlayers
65     property bool swapping: false
66     //onSwapPlayers: if (autoTurnChange) Logic.turnChange();//Now implemented below
67     //For puzzle
68     property url level
69     property bool puzzleWon: false
70     signal puzzleLost //Since root is tracking the puzzle progress
71     function showPuzzleEnd (won) {
72         if (won) {
73             smokeParticle.color = Qt.rgba(0,1,0,0);
74             puzzleWin.play();
75         } else {
76             smokeParticle.color = Qt.rgba(1,0,0,0);
77             puzzleFail.play();
78             puzzleLost();
79         }
80     }
81     function showPuzzleGoal (str) {
82         puzzleTextBubble.opacity = 1;
83         puzzleTextLabel.text = str;
84     }
85     Image {
86         id: bg
87         z: -1
88         source: background;
89         fillMode: Image.PreserveAspectCrop
90     }
91
92     width: 320
93     height: 480
94     MouseArea {
95         anchors.fill: parent; onClicked: {
96             if (puzzleTextBubble.opacity == 1) {
97                 puzzleTextBubble.opacity = 0;
98                 Logic.finishLoadingMap();
99             } else if (!swapping) {
100                 Logic.handleClick(mouse.x,mouse.y);
101             }
102         }
103     }
104
105     Image {
106         id: highScoreTextBubble
107         opacity: mode == "arcade" && gameOver && gameCanvas.score == gameCanvas.highScore ? 1 : 0
108         Behavior on opacity { NumberAnimation {} }
109         anchors.centerIn: parent
110         z: 10
111         source: "gfx/bubble-highscore.png"
112         Image {
113             anchors.centerIn: parent
114             source: "gfx/text-highscore-new.png"
115             rotation: -10
116         }
117     }
118
119     Image {
120         id: puzzleTextBubble
121         anchors.centerIn: parent
122         opacity: 0
123         Behavior on opacity { NumberAnimation {} }
124         z: 10
125         source: "gfx/bubble-puzzle.png"
126         Connections {
127             target: gameCanvas
128             onModeChanged: if (mode != "puzzle" && puzzleTextBubble.opacity > 0) puzzleTextBubble.opacity = 0;
129         }
130         Text {
131             id: puzzleTextLabel
132             width: parent.width - 24
133             anchors.centerIn: parent
134             horizontalAlignment: Text.AlignHCenter
135             color: "white"
136             font.pixelSize: 24
137             font.bold: true
138             wrapMode: Text.WordWrap
139         }
140     }
141     onModeChanged: {
142         p1WonImg.opacity = 0;
143         p2WonImg.opacity = 0;
144     }
145     SmokeText { id: puzzleWin; source: "gfx/icon-ok.png"; system: particleSystem }
146     SmokeText { id: puzzleFail; source: "gfx/icon-fail.png"; system: particleSystem }
147
148     onSwapPlayers: {
149         smokeParticle.color = "yellow"
150         Logic.turnChange();
151         if (curTurn == 1) {
152             p1Text.play();
153         } else {
154             p2Text.play();
155         }
156         clickDelay.running = true;
157     }
158     SequentialAnimation {
159         id: clickDelay
160         ScriptAction { script: gameCanvas.swapping = true; }
161         PauseAnimation { duration: 750 }
162         ScriptAction { script: gameCanvas.swapping = false; }
163     }
164
165     SmokeText {
166         id: p1Text; source: "gfx/text-p1-go.png";
167         system: particleSystem; playerNum: 1
168         opacity: p1WonImg.opacity + p2WonImg.opacity > 0 ? 0 : 1
169     }
170
171     SmokeText {
172         id: p2Text; source: "gfx/text-p2-go.png";
173         system: particleSystem; playerNum: 2
174         opacity: p1WonImg.opacity + p2WonImg.opacity > 0 ? 0 : 1
175     }
176
177     onGameOverChanged: {
178         if (gameCanvas.mode == "multiplayer") {
179             if (gameCanvas.score >= gameCanvas.score2) {
180                 p1WonImg.opacity = 1;
181             } else {
182                 p2WonImg.opacity = 1;
183             }
184         }
185     }
186     Image {
187         id: p1WonImg
188         source: "gfx/text-p1-won.png"
189         anchors.centerIn: parent
190         opacity: 0
191         Behavior on opacity { NumberAnimation {} }
192         z: 10
193     }
194     Image {
195         id: p2WonImg
196         source: "gfx/text-p2-won.png"
197         anchors.centerIn: parent
198         opacity: 0
199         Behavior on opacity { NumberAnimation {} }
200         z: 10
201     }
202
203     ParticleSystem{
204         id: particleSystem;
205         anchors.fill: parent
206         z: 5
207         ImageParticle {
208             id: smokeParticle
209             groups: ["smoke"]
210             source: "gfx/particle-smoke.png"
211             alpha: 0.1
212             alphaVariation: 0.1
213             color: "yellow"
214         }
215         Loader {
216             id: auxLoader
217             anchors.fill: parent
218             source: "PrimaryPack.qml"
219             onItemChanged: {
220                 if (item && "particleSystem" in item)
221                     item.particleSystem = particleSystem
222                 if (item && "gameArea" in item)
223                     item.gameArea = gameCanvas
224             }
225         }
226     }
227 }
228