blob: b8b08ad7f5bdc2da6304999592d1ad61063df303 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
import QtQuick 1.0
Item {
id: neko
height: 32
width: 32
clip: true
property int targetX;
property int targetY;
property alias imageSource: sprite.source
property bool running: true
property int maxSpeed: 6
property int _anim
property int _animFrames
property int _animCurFrame
property int _distanceX: targetX - x - (width / 2)
property int _distanceY: targetY - y - (5 * height / 6)
property int _distance: Math.sqrt(_distanceX * _distanceX + _distanceY * _distanceY)
property int _calcSpeedX: _distance < maxSpeed ?
_distanceX :
((maxSpeed * _distanceX) / _distance)
property int _calcSpeedY: _distance < maxSpeed ?
_distanceY :
((maxSpeed * _distanceY) / _distance)
property real _calcAngle: Math.atan2(_calcSpeedY, _calcSpeedX)
property int _speedX: 0
property int _speedY: 0
signal arrived
function chooseWalkAnimation(speedX, speedY, angle) {
var PiPer8 = Math.PI / 8;
if (angle > 7 * PiPer8) {
return 6; // left
} else if (angle > 5 * PiPer8) {
return 2; // dwleft
} else if (angle > 3 * PiPer8) {
return 0; // down
} else if (angle > 1 * PiPer8) {
return 3; // dwright
} else if (angle > -1 * PiPer8) {
return 9; // right
} else if (angle > -3 * PiPer8) {
return 14; // upright
} else if (angle > -5 * PiPer8) {
return 12; // up
} else {
return 13; // upleft
}
}
Image {
id: sprite
source: "neko.png"
x: -(neko.width * neko._animCurFrame)
y: -(neko.height * neko._anim)
}
Timer {
id: timer
interval: 250
running: neko.running
repeat: true
onTriggered: {
neko._animCurFrame = (_animCurFrame + 1) % _animFrames
neko.x = neko.x + _speedX;
neko.y = neko.y + _speedY;
if (state == "WALKING" && _distance == 0) {
neko.arrived();
}
}
}
Timer {
id: idleToScratchTimer
onTriggered: neko.state = "SCRATCHING"
}
Timer {
id: idleToYawnTimer
onTriggered: neko.state = "YAWNING"
}
Timer {
id: scrachTimer
interval: timer.interval * 2
running: neko.running && state === "SCRATCHING"
onTriggered: neko.state = "IDLING"
}
Timer {
id: yawnTimer
interval: timer.interval * 3
running: neko.running && state === "YAWNING"
onTriggered: neko.state = "IDLING"
}
onTargetXChanged: state = "WALKING"
onTargetYChanged: state = "WALKING"
onArrived: state = "IDLING"
states: [
State {
name: "SLEEPING"
PropertyChanges {
target: neko
_anim: 11 // sleep
_animFrames: 2
_speedX: 0
_speedY: 0
}
},
State {
name: "IDLING"
PropertyChanges {
target: neko
_anim: 4 // jare
_animFrames: 1
_animCurFrame: 0
_speedX: 0
_speedY: 0
}
PropertyChanges {
target: idleToScratchTimer
interval: Math.random() * 10000
running: neko.running && state == "IDLING"
}
PropertyChanges {
target: idleToYawnTimer
interval: Math.random() * 20000
running: neko.running && state == "IDLING"
}
},
State {
name: "SCRATCHING"
PropertyChanges {
target: neko
_anim: 5 // kaki
_animFrames: 2
_speedX: 0
_speedY: 0
}
},
State {
name: "YAWNING"
PropertyChanges {
target: neko
_anim: 8 // mati
_animFrames: 2
_speedX: 0
_speedY: 0
}
},
State {
name: "WALKING"
PropertyChanges {
target: neko
_anim: chooseWalkAnimation(_calcSpeedX, _calcSpeedY, _calcAngle)
_animFrames: 2
_speedX: _calcSpeedX
_speedY: _calcSpeedY
}
}
]
}
|