Hanoi-Towers/source/game.qml

318 lines
7.6 KiB
QML
Raw Normal View History

2018-01-27 15:21:14 +03:00
import QtQuick 2.9
import QtQuick.Controls 2.2
2018-01-20 17:44:28 +03:00
import QtQuick.Dialogs 1.2
2018-04-11 20:27:22 +03:00
import BackEnd 1.0
2018-04-06 14:08:14 +03:00
import "./base" as Base
2018-01-20 17:44:28 +03:00
Rectangle {
visible: true
id: gameWindow
width: 640
height: 480
color: "#ffffff"
property int all: 1
2018-04-11 20:27:22 +03:00
property var oldTower
2018-04-11 22:13:34 +03:00
BackEnd {
id: backEnd
}
2018-01-20 17:44:28 +03:00
MouseArea {
id: mouse
}
2018-04-06 14:08:14 +03:00
Base.BaseButton {
2018-01-20 17:44:28 +03:00
id: b_start
text: "Start"
onClicked: {
gameWindow.start(spin.value)
}
anchors.right: about.left
2018-01-27 15:05:45 +03:00
anchors.rightMargin: 5;
2018-01-20 17:44:28 +03:00
width: mouseContener.width
height: mouseContener.height
}
2018-04-06 14:08:14 +03:00
Base.BaseButton {
2018-01-20 17:44:28 +03:00
id: b_exit
text: "Exit"
anchors.right: gameWindow.right
2018-01-27 15:05:45 +03:00
anchors.rightMargin: 5;
2018-01-20 17:44:28 +03:00
width: mouseContener.width
height: mouseContener.height
onClicked: {
Qt.quit()
}
}
2018-04-06 14:08:14 +03:00
Base.BaseButton {
2018-01-20 17:44:28 +03:00
id: about
text: "About"
anchors.right: b_exit.left
2018-01-27 15:05:45 +03:00
anchors.rightMargin: 5;
2018-01-20 17:44:28 +03:00
width: mouseContener.width
height: mouseContener.height
onClicked: {
gameWindow.parent.source = "about.qml"
}
}
Rectangle {
id: s_start
Rectangle {
2018-04-06 14:08:14 +03:00
Base.BaseText {
2018-01-20 17:44:28 +03:00
font.bold: true
font.pointSize: height / text.length * 2
horizontalAlignment: Text.AlignHCenter
styleColor: "#973c3c"
verticalAlignment: Text.AlignVCenter
text: "Tower height:"
anchors.fill: parent
}
anchors.left: parent.left
width: parent.width / 2
height: parent.height
}
2018-04-06 14:08:14 +03:00
Base.BaseButton {
2018-01-20 17:44:28 +03:00
id: frame
2018-01-22 19:48:18 +03:00
text: "" + (spin.currentIndex + 1)
2018-01-20 17:44:28 +03:00
width: parent.width / 2
height: parent.height
2018-01-22 19:48:18 +03:00
onClicked: {
tumbler.visible = true;
}
2018-01-20 17:44:28 +03:00
anchors.right: parent.right
2018-01-27 15:05:45 +03:00
anchors.rightMargin: 5;
2018-01-20 17:44:28 +03:00
}
anchors.right: b_start.left
width: mouseContener.width * 2
height: mouseContener.height
}
Rectangle {
id: step
property int ste: 0
width: mouseContener.width
height: mouseContener.height
Text {
font.bold: true
font.pointSize: 14
horizontalAlignment: Text.AlignHCenter
styleColor: "#973c3c"
verticalAlignment: Text.AlignVCenter
text: "" + step.ste
anchors.fill: parent
}
anchors.left: mouseContener.right
}
Item {
id: mouseContener
property var mouseObj: null
width: parent.width * 0.14
height: parent.height * 0.1
x: mouse.mouseX
y: mouse.mouseY
function clear() {
if (mouseObj) {
mouseObj.destroy()
}
}
function push(obj) {
mouseObj = obj
obj.parent = this
return true
}
function top() {
return mouseObj
}
function pop() {
mouseObj = null
}
}
function start(value) {
2018-04-11 22:13:34 +03:00
if (backEnd.isFirst) {
help.open();
}
2018-04-11 20:27:22 +03:00
spin.maximumValue = backEnd.reed
if (backEnd.reed <= value || value < 0)
spin.value = all = value = backEnd.reed
2018-01-20 17:44:28 +03:00
else {
spin.value = all = value
}
step.ste = 0
tower1.clear()
tower2.clear()
tower3.clear()
mouseContener.clear()
while (value--) {
var temp = Qt.createComponent("plate.qml")
if (temp.status === Component.Ready) {
var obj = temp.createObject(parent)
obj.mass = value + 1
obj.value = all
tower1.push(obj)
}
}
}
function move(from, into) {
if (from[from.lenght - 1] < into[into.lenght - 1]) {
tower1.push()
}
}
function trigered(obj) {
if (mouseContener.mouseObj) {
if (obj.push(mouseContener.top())) {
2018-04-11 20:27:22 +03:00
if(oldTower !== obj) step.ste++
2018-01-20 17:44:28 +03:00
mouseContener.pop()
}
} else {
2018-04-11 20:27:22 +03:00
if (mouseContener.push((obj.top()))){
oldTower = obj;
2018-01-20 17:44:28 +03:00
obj.pop()
2018-04-11 20:27:22 +03:00
}
2018-01-20 17:44:28 +03:00
}
if (tower2.items.length === all || tower3.items.length === all) {
if (all == spin.maximumValue) {
2018-04-11 20:27:22 +03:00
backEnd.save(spin.value = spin.maximumValue = all + 1)
2018-04-06 14:08:14 +03:00
popUp.text = "You have passed the level in " + step.ste
2018-04-11 20:27:22 +03:00
+ " minimum steps for this lvl: " + backEnd.getMinSteps(all)
2018-04-06 14:08:14 +03:00
+ " steps\n and unlocked level " + all + ".";
popUp.open()
2018-01-20 17:44:28 +03:00
start(spin.value)
} else {
2018-04-11 20:27:22 +03:00
popUp.text = "You have passed the level in " + step.ste + " steps." +
+ " minimum steps for this lvl: " + backEnd.getMinSteps(all);
2018-04-06 14:08:14 +03:00
popUp.open()
2018-01-20 17:44:28 +03:00
start(++spin.value)
}
}
}
2018-04-06 14:08:14 +03:00
2018-01-20 17:44:28 +03:00
2018-04-06 14:08:14 +03:00
PopUp {
id:popUp;
2018-01-20 17:44:28 +03:00
}
2018-04-06 14:08:14 +03:00
2018-01-20 17:44:28 +03:00
Tower {
id: tower1
width: gameWindow.width * 0.33
height: gameWindow.height * 0.9
anchors.left: gameWindow.left
anchors.bottom: gameWindow.bottom
onClick: {
trigered(obj)
}
}
Tower {
id: tower2
width: tower1.width
height: tower1.height
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: tower1.bottom
onClick: {
trigered(obj)
}
}
Tower {
id: tower3
width: tower2.width
height: tower2.height
anchors.right: gameWindow.right
anchors.bottom: tower2.bottom
onClick: {
trigered(obj)
}
}
2018-01-22 19:48:18 +03:00
Rectangle {
id: tumbler
color: "#ffffff"
border.color: "#d5d4d4"
visible: false
Tumbler {
visibleItemCount : 5;
id: spin
model: 99
property int value: 4
property int maximumValue: 99
function format(){
}
delegate: Text {
color: Qt.rgba(0.5,0.5,0.5,1 / (Math.abs(spin.currentIndex - modelData)))
text: "" + (modelData + 1)
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
2018-01-27 15:05:45 +03:00
font.pixelSize: (spin.height / 4) / (Math.abs(spin.currentIndex - modelData) + 1 )
2018-01-22 19:48:18 +03:00
}
onValueChanged: {
currentIndex = value - 1
}
onCurrentIndexChanged: {
value = currentIndex + 1;
}
onMaximumValueChanged: {
model = maximumValue
}
anchors.fill: parent
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: closeTumbler.height + 5
}
2018-04-06 14:08:14 +03:00
Base.BaseButton{
2018-01-22 19:48:18 +03:00
id: closeTumbler
text: qsTr("Ok")
onClicked: {
tumbler.visible = false;
}
anchors.right: parent.right
anchors.left: parent.left
anchors.bottom: parent.bottom
}
width: (height / 4)
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 20
anchors.bottomMargin: 20
}
2018-04-11 22:13:34 +03:00
Help{
id: help
}
2018-01-20 17:44:28 +03:00
}