import QtQuick 2.5
import fbx.application 1.0

Application {
color: "white"
property int aX : 50
property int aY : 50
property int point : 0
property int index
property int indexD
property bool dX : false
property bool cX : false
property bool dY : false
property bool cY : false
property variant tab : [bluerect, greenrect, redrect]
property variant tabPoint : [1, 5, 10]
property variant tabSens: [true, true, true]
property variant tabVit: [2, 4, 7]

Text{
text: "Point : " + point
color: "black"
font.pixelSize: 30
font.bold: true
anchors.left: parent.left
anchors.top: parent.top
anchors.topMargin: 15
anchors.leftMargin: 10
}

Rectangle {
id: bluerect
width: 50; height: 50
color: "blue"
x: 50
y: 500
}

Rectangle {
id: greenrect
width: 30; height: 30
color: "green"
x: 250
y: 375
}

Rectangle {
id: redrect
width: 20; height: 20
color: "red"
x: 750
y: 250
}

Rectangle {
focus: true
id: rect
width: 20; height: 20
color: "purple"
x: aX
y: aY

Keys.onRightPressed: aX += 10
Keys.onLeftPressed: aX -= 10
Keys.onUpPressed: aY-= 10
Keys.onDownPressed: aY+= 10

Keys.onReturnPressed: {
for(index = 0; index < tab.length; index++)
{
dX = rect.x >= tab[index].x
cX = rect.x <= tab[index].x + tab[index].width - rect.width
dY = rect.y >= tab[index].y
cY = rect.y <= tab[index].y + tab[index].height - rect.height
if(dX && cX && dY && cY)
{
point += tabPoint[index]
break
}

else {
dX = false
cX = false
dY = false
cY = false
}
}
}
}

Timer {
interval: 25
running: true
repeat: true
onTriggered: deplacement()
}

function deplacement(){
for(indexD = 0; indexD < tab.length; indexD++){
if(tab[indexD].x <= width - (tab[indexD].width + 50) && tabSens[indexD]){
tab[indexD].x += tabVit[indexD]
if(tab[indexD].x >= width - (tab[indexD].width + 50))
tabSens[indexD]= false
}
else if(tab[indexD].x >= 50 && !tabSens[indexD]){
tab[indexD].x -= tabVit[indexD]
if(tab[indexD].x <= 50)
tabSens[indexD]= true
}
}
}
}