Juicy Squash - Match-3 Game
import Felgo 4.0
import QtQuick 2.0
EntityBase {
id: block
entityType: "block"
poolingEnabled: true
enabled: opacity == 1
property int type
property int row
property int column
property int previousRow
property int previousColumn
signal swapBlock(int row, int column, int targetRow, int targetColumn)
signal swapFinished(int row, int column, int swapRow, int swapColumn)
signal fallDownFinished(var block)
Image {
anchors.fill: parent
source: {
if (type == 0)
return "../../assets/img/fruits/Apple.png"
else if(type == 1)
return "../../assets/img/fruits/Banana.png"
else if (type == 2)
return "../../assets/img/fruits/Orange.png"
else if (type == 3)
return "../../assets/img/fruits/Pear.png"
else if (type == 4)
return "../../assets/img/fruits/BlueBerry.png"
else if (type == 5)
return "../../assets/img/fruits/WaterMelon.png"
else if (type == 6)
return "../../assets/img/fruits/Coconut.png"
else
return "../../assets/img/fruits/Lemon.png"
}
}
MouseArea {
id: mouse
anchors.fill: parent
property bool dragging
property bool waitForRelease
property var dragStart
onPressed: mouse => {
if(!dragging && !waitForRelease) {
dragging = true
dragStart = { x: mouse.x, y: mouse.y }
}
}
onReleased: mouse => {
dragging = false
waitForRelease = false
}
onPositionChanged: mouse => {
if(!dragging || waitForRelease)
return
var xDistance = mouse.x - dragStart.x
var yDistance = mouse.y - dragStart.y
if((Math.abs(xDistance) < block.width/2)
&& (Math.abs(yDistance) < block.height/2))
return
var targetRow = block.row
var targetCol = block.column
if(Math.abs(xDistance) > Math.abs(yDistance)) {
if(xDistance > 0)
targetCol++
else
targetCol--
}
else {
if(yDistance > 0)
targetRow++
else
targetRow--
}
dragging = false
waitForRelease = true
block.swapBlock(row, column, targetRow, targetCol)
}
}
Rectangle {
id: highlightRect
color: "white"
anchors.fill: parent
anchors.centerIn: parent
opacity: 0
z: 1
}
Item {
id: particleItem
width: parent.width
height: parent.height
x: parent.width/2
y: parent.height/2
GameParticle {
id: sparkleParticle
fileName: Qt.resolvedUrl("../particles/FruitySparkle.json")
}
opacity: 0
visible: opacity > 0
enabled: opacity > 0
}
NumberAnimation {
id: fadeOutAnimation
target: block
property: "opacity"
duration: 500
from: 1.0
to: 0
onStopped: {
sparkleParticle.stop()
entityManager.removeEntityById(block.entityId)
}
}
NumberAnimation {
id: fadeInAnimation
target: block
property: "opacity"
duration: 1000
from: 0
to: 1
}
NumberAnimation {
id: fallDownAnimation
target: block
property: "y"
onStopped: {
fallDownFinished(block)
}
}
Timer {
id: fallDownTimer
interval: fadeOutAnimation.duration
repeat: false
running: false
onTriggered: {
fallDownAnimation.start()
}
}
Timer {
id: signalSwapFinished
interval: 50
onTriggered: swapFinished(block.previousRow, block.previousColumn, block.row, block.column)
}
NumberAnimation {
id: swapAnimation
target: block
duration: 150
onStopped: {
signalSwapFinished.start()
}
}
SequentialAnimation {
id: highlightAnimation
loops: Animation.Infinite
NumberAnimation {
target: highlightRect
property: "opacity"
duration: 750
from: 0
to: 0.35
}
NumberAnimation {
target: highlightRect
property: "opacity"
duration: 750
from: 0.35
to: 0
}
}
function remove() {
particleItem.opacity = 1
sparkleParticle.start()
fadeOutAnimation.start()
}
function fallDown(distance) {
fallDownAnimation.complete()
fallDownAnimation.duration = 100 * distance
fallDownAnimation.to = block.y + distance * block.height
fallDownTimer.start()
}
function swap(targetRow, targetCol) {
swapAnimation.complete()
block.previousRow = block.row
block.previousColumn = block.column
if(targetRow !== block.row) {
swapAnimation.property = "y"
swapAnimation.to = block.y +
(targetRow > block.row ? block.height : -block.height)
block.row = targetRow
}
else if(targetCol !== block.column) {
swapAnimation.property = "x"
swapAnimation.to = block.x +
(targetCol > block.column ? block.width : -block.width)
block.column = targetCol
}
else
return
swapAnimation.start()
}
function highlight(active) {
if(active) {
highlightRect.opacity = 0
highlightAnimation.start()
}
else {
highlightAnimation.stop()
highlightRect.opacity = 0
}
}
function fadeIn() {