Warning, /education/gcompris/src/activities/gnumch-equality/Creature.qml is written in an unsupported language. File is not indexed.
0001 /* GCompris - Creature.qml
0002 *
0003 * SPDX-FileCopyrightText: 2014 Manuel Tondeur <manueltondeur@gmail.com>
0004 *
0005 * Authors:
0006 * Joe Neeman (spuzzzzzzz@gmail.com) (GTK+ version)
0007 * Manuel Tondeur <manueltondeur@gmail.com> (Qt Quick port)
0008 *
0009 * SPDX-License-Identifier: GPL-3.0-or-later
0010 */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 import "../../core"
0014
0015 Item {
0016 id: creature
0017
0018 property int index
0019 property string monsterType
0020 property bool movable
0021 property bool movingOn: false
0022 property bool eating: false
0023 property int frames
0024 property int frameSize: 320
0025 property int animCount: 0
0026 property GCSfx audioEffects
0027 readonly property int moveRight: 0
0028 readonly property int moveLeft: 1
0029 readonly property int moveDown: 2
0030 readonly property int moveUp: 3
0031
0032 function moveTo(direction) {
0033 if (!movable)
0034 return true
0035
0036 if (!hasReachLimit(direction)) {
0037 movementOn(direction)
0038 return true
0039 } else {
0040
0041 return false
0042 }
0043 }
0044
0045 function init() {
0046 index = 0
0047 x = 0
0048 y = 0
0049 }
0050
0051 function hasReachLimit(direction) {
0052 switch (direction) {
0053 case 0:
0054 if ((index + 1) % 6 > 0)
0055 return false
0056 break
0057 case 1:
0058 if ((index % 6) > 0)
0059 return false
0060 break
0061 case 2:
0062 if (index < 30)
0063 return false
0064 break
0065 case 3:
0066 if (index > 5)
0067 return false
0068 break
0069 }
0070 return true
0071 }
0072
0073 function movementOn(direction) {
0074 // Compute if the direction is vertical (1) or not (0)
0075 var vertical = Math.floor(direction / 2)
0076 var sign = Math.pow(-1, (direction))
0077 index += sign * (1 + 5 * vertical)
0078 var restIndex = index % 6
0079 y = Math.floor(((index - restIndex) / 6) * grid.cellHeight)
0080 x = Math.floor(restIndex * grid.cellWidth)
0081 }
0082
0083 function updatePosition() {
0084 var restIndex = index % 6
0085 y = Math.floor(((index - restIndex) / 6) * grid.cellHeight)
0086 x = Math.floor(restIndex * grid.cellWidth)
0087 }
0088
0089 index: 0
0090 z: 0
0091 movable: true
0092 width: grid.cellWidth
0093 height: grid.cellHeight
0094
0095 onEatingChanged: {
0096 if (eating == true) {
0097 creatureImage.restart()
0098 creatureImage.resume()
0099 creature.audioEffects.play("qrc:/gcompris/src/activities/gnumch-equality/resource/eat.wav")
0100 }
0101 }
0102
0103 AnimatedSprite {
0104 id: creatureImage
0105
0106 property int turn: 0
0107
0108 anchors.horizontalCenter: parent.horizontalCenter
0109 anchors.verticalCenter: parent.verticalCenter
0110 width: Math.min(parent.width, parent.height)
0111 height: width
0112 source: "qrc:/gcompris/src/activities/gnumch-equality/resource/"
0113 + monsterType + ".svg"
0114
0115 frameCount: creature.frames
0116 frameWidth: creature.frameSize
0117 frameHeight: creature.frameSize
0118 frameDuration: 50
0119 currentFrame: 0
0120 running: false
0121 interpolate: false
0122
0123 onCurrentFrameChanged: {
0124 creature.animCount++
0125 if (creature.animCount == creature.frames) {
0126 creature.animCount = 0
0127 turn++
0128 }
0129 }
0130
0131 onTurnChanged: {
0132 if (turn == 2) {
0133 creature.eating = false
0134 turn = 0
0135 currentFrame = 0
0136 creature.animCount = 0
0137 pause()
0138 }
0139 }
0140 }
0141
0142 Behavior on x {
0143 NumberAnimation {
0144 id: xAnim
0145 duration: 300
0146 onRunningChanged: {
0147 movingOn = !movingOn
0148 }
0149 }
0150 }
0151
0152 Behavior on y {
0153 NumberAnimation {
0154 id: yAnim
0155 duration: 300
0156 onRunningChanged: {
0157 movingOn = !movingOn
0158 }
0159 }
0160 }
0161
0162 Behavior on opacity {
0163 NumberAnimation {
0164 duration: 500
0165 }
0166 }
0167 }