diff --git a/src/activities/penalty/Penalty.qml b/src/activities/penalty/Penalty.qml --- a/src/activities/penalty/Penalty.qml +++ b/src/activities/penalty/Penalty.qml @@ -45,22 +45,28 @@ activity.stop.connect(stop) } - function changeBallState(saveBallState) { + function isEnabled (MyRectangle) { + return (items.saveBallState !== "INITIAL") ? + (items.saveBallState === MyRectangle.state) + :(ball.x === items.ballX && ball.y === items.ballY) + } + + function changeBallState(MyRectangle) { instruction.text = "" + //var saveBallState = MyRectangle.state if(ball.state === "FAIL") { Activity.resetLevel() return } /* This is a shoot */ - var progress = progressTop - if (saveBallState == "LEFT") { - progress = progressLeft - } - else if(saveBallState == "RIGHT") { - progress = progressRight + var progress = items.progress + if (items.saveBallState === "INITIAL" && progress === undefined) { + items.saveBallState = MyRectangle.state + progress = MyRectangle.progress } + items.progress = progress if(progress.ratio > 0) { /* Second click, stop animation */ @@ -72,7 +78,7 @@ /* Success or not */ if(progress.ratio < 100) { /* Success */ - ball.state = saveBallState + ball.state = items.saveBallState } else { /* failure */ ball.state = "FAIL" @@ -88,8 +94,10 @@ } // To enable tapping/clicking on left side of goal - Rectangle { + Zones { id: rectLeft + state: "LEFT" + progress: progressLeft anchors.left: parent.left anchors.top: parent.top anchors.right: player.left @@ -97,43 +105,25 @@ anchors.leftMargin: parent.width * 0.08 anchors.topMargin: parent.height * 0.07 anchors.bottomMargin: parent.height * 0.45 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("LEFT") - } - } - } } // To enable tapping/clicking on top of goal - Rectangle { + Zones { id: rectTop + state: "CENTER" + progress: progressTop anchors.top: parent.top - anchors.left: rectLeft.right - anchors.right: rectRight.left + anchors.left: player.left + anchors.right: player.right anchors.bottom: player.top anchors.topMargin: parent.height * 0.07 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("CENTER") - } - } - } } // To enable tapping/clicking on right side of goal - Rectangle { + Zones { id: rectRight + state: "RIGHT" + progress: progressRight anchors.left: player.right anchors.right: parent.right anchors.top: parent.top @@ -141,17 +131,6 @@ anchors.rightMargin: parent.width * 0.06 anchors.topMargin: parent.height * 0.07 anchors.bottomMargin: parent.height * 0.45 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("RIGHT") - } - } - } } // Add here the QML items you need to access in javascript @@ -167,6 +146,10 @@ property alias bonus: bonus property int duration : 0 property int progressBarOpacity : 40 + property string saveBallState: "INITIAL" + property var progress: undefined + property double ballX: ball.parent.width/2 - ball.width/2; + property double ballY: ball.parent.height*0.77 - ball.height/2 } onStart: { Activity.start(items) } @@ -236,16 +219,14 @@ timerBonus.start() } } - PropertyAnimation - { + PropertyAnimation { target: progressLeft property: "ratio" from: 0 to: 100 duration: items.duration } - PropertyAnimation - { + PropertyAnimation { target: progressLeft property: "color" from: "#00FF00" @@ -274,16 +255,14 @@ timerBonus.start() } } - PropertyAnimation - { + PropertyAnimation { target: progressRight property: "ratio" from: 0 to: 100 duration: items.duration } - PropertyAnimation - { + PropertyAnimation { target: progressRight property: "color" from: "#00FF00" @@ -432,9 +411,11 @@ acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton onClicked: { ball.state = "INITIAL" + items.saveBallState = "INITIAL" progressRight.ratio = 0 progressLeft.ratio = 0 progressTop.ratio = 0 + items.progress = undefined } } } diff --git a/src/activities/penalty/Zones.qml b/src/activities/penalty/Zones.qml new file mode 100644 --- /dev/null +++ b/src/activities/penalty/Zones.qml @@ -0,0 +1,31 @@ +import QtQuick 2.0 + +Rectangle { + id: rectangle + state: "INITIAL" + property var progress: undefined + states: [ + State { + name: "LEFT" + }, + State { + name: "RIGHT" + }, + State { + name: "CENTER" + } + ] + color: "transparent" + + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton + enabled: { + background.isEnabled(this.parent) + } + + onClicked: { + background.changeBallState(this.parent) + } + } +} diff --git a/src/activities/penalty/penalty.js b/src/activities/penalty/penalty.js --- a/src/activities/penalty/penalty.js +++ b/src/activities/penalty/penalty.js @@ -31,6 +31,7 @@ function start(items_) { items = items_ currentLevel = 0 + initLevel() } @@ -54,9 +55,11 @@ function resetLevel() { items.ball.state = "INITIAL" + items.saveBallState = "INITIAL" items.progressRight.ratio = 0 items.progressLeft.ratio = 0 items.progressTop.ratio = 0 + items.progress = undefined } function nextLevel() {