-
Notifications
You must be signed in to change notification settings - Fork 4
/
states.go
242 lines (213 loc) · 5.5 KB
/
states.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
var screenW, screenH = ebiten.ScreenSizeInFullscreen()
type StateMachine struct {
state State
anim *Anim
animFrameCount int // number of frames in current anim
frameIdx int // frame index in current animation
ticks int // ticks since last animation frame change
lastFed time.Time
x, y int // logical window position
isXYInit bool
}
func NewStateMachine() *StateMachine {
sm := StateMachine{}
sm.SetState(&StateIdle{})
sm.lastFed = time.Now()
return &sm
}
func (sm *StateMachine) SetAnim(anim *Anim) {
sm.anim = anim
sm.animFrameCount = len(anim.Frames)
sm.frameIdx = 0
sm.ticks = 0
}
func (sm *StateMachine) Frame() *ebiten.Image {
return sm.anim.Frames[sm.frameIdx]
}
func (sm *StateMachine) SetState(s State) {
sm.state = s
sm.state.Enter(sm)
}
func (sm *StateMachine) Update() error {
if !sm.isXYInit {
sm.x, sm.y = ebiten.WindowPosition()
sm.isXYInit = true
}
sm.state.Update(sm)
sm.x = min(max(sm.x, 0), screenW-WindowWidth)
sm.y = min(max(sm.y, 0), screenH-WindowHeight)
ebiten.SetWindowPosition(sm.x, sm.y)
// Advance to next animation frame
sm.ticks += 1
if sm.ticks < 10 {
return nil
}
sm.ticks = 0
if sm.frameIdx < sm.animFrameCount-1 {
sm.frameIdx += 1
return nil
}
// At end of current anim, restart the animation,
// execute state-specific hook if any.
sm.frameIdx = 0
sm.state.EndAnimHook(sm)
return nil
}
func (sm *StateMachine) Draw(screen *ebiten.Image) {
screen.DrawImage(sm.Frame(), nil)
}
func (sm *StateMachine) Layout(ow, oh int) (sw, sh int) {
return SPRITE_X, SPRITE_Y
}
type State interface {
Enter(sm *StateMachine)
Update(sm *StateMachine)
EndAnimHook(sm *StateMachine)
}
type StateIdle struct{}
func (s *StateIdle) Enter(sm *StateMachine) { sm.SetAnim(Idle) }
func (s *StateIdle) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
sm.SetState(&StateRClick{})
return
}
}
func (s *StateIdle) EndAnimHook(sm *StateMachine) {
if randBool(WalkChance) {
sm.SetState(&StateWalk{isLeft: randBool(50)})
}
}
type StateDrag struct {
PreviousMousePos Vector
WinStartPos Vector
MouseStartPos Vector
}
func (s *StateDrag) Enter(sm *StateMachine) {
sm.SetAnim(Drag)
s.PreviousMousePos = GlobalCursorPosition()
s.WinStartPos = CreateVector(ebiten.WindowPosition())
s.MouseStartPos = GlobalCursorPosition()
}
func (s *StateDrag) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
sm.SetState(&StateIdle{})
return
}
mousePos := GlobalCursorPosition()
if mousePos != s.PreviousMousePos {
winPos := s.WinStartPos.Add(mousePos.Subtract(s.MouseStartPos))
sm.x, sm.y = winPos.x, winPos.y
}
s.PreviousMousePos = mousePos
}
func (s *StateDrag) EndAnimHook(sm *StateMachine) {
syncWinPos(sm)
}
type StateRClick struct{}
func (s *StateRClick) Enter(sm *StateMachine) {
sm.SetAnim(RightClick)
}
func (s *StateRClick) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
sm.SetState(&StateRClick{})
return
}
}
func (s *StateRClick) EndAnimHook(sm *StateMachine) {
sm.SetState(&StateIdle{})
}
type StateHungry struct{}
func (s *StateHungry) Enter(sm *StateMachine) { sm.SetAnim(Hungry) }
func (s *StateHungry) Update(sm *StateMachine) {
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
sm.SetState(&StateFeed{})
return
}
}
func (s *StateHungry) EndAnimHook(sm *StateMachine) {}
func checkHunger(sm *StateMachine) (isHungry bool) {
now := time.Now()
if now.Sub(sm.lastFed) >= DurationTillHungry {
sm.SetState(&StateHungry{})
return true
}
return false
}
type StateFeed struct{}
func (s *StateFeed) Enter(sm *StateMachine) { sm.SetAnim(Feed) }
func (s *StateFeed) Update(sm *StateMachine) {}
func (s *StateFeed) EndAnimHook(sm *StateMachine) {
sm.SetState(&StateIdle{})
sm.lastFed = time.Now()
}
type StateWalk struct {
isLeft bool
}
func (s *StateWalk) Enter(sm *StateMachine) {
if s.isLeft {
sm.SetAnim(WalkLeft)
} else {
sm.SetAnim(WalkRight)
}
}
func (s *StateWalk) Update(sm *StateMachine) {
if checkHunger(sm) {
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
sm.SetState(&StateDrag{})
return
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
sm.SetState(&StateRClick{})
return
}
if s.isLeft {
sm.x -= 1
} else {
sm.x += 1
}
}
func (s *StateWalk) EndAnimHook(sm *StateMachine) {
// On KDE, our window isn't allowed to walk over a vertical taskbar
// ("panel" in KDE parlance), so when gura walks agains a taskbar, the
// in-game position (sm.x) will get out of sync with the actual window
// position. Therefore, we must run a sync:
syncWinPos(sm)
if randBool(StopChance) {
sm.SetState(&StateIdle{})
}
}
// Sometimes the sm.x/y doesn't match the window's actual position on screen.
// Run this at the end of States that might end up in that situation.
func syncWinPos(sm *StateMachine) {
actualX, actualY := ebiten.WindowPosition()
if actualX != sm.x || actualY != sm.y {
//fmt.Printf("ingame: %d,%d - actual: %d,%d\n", sm.x, sm.y, actualX, actualY)
sm.x = actualX
sm.y = actualY
}
}