/
state_hero_wait.go
86 lines (73 loc) · 2.14 KB
/
state_hero_wait.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
package game_map
import (
"github.com/faiface/pixel/pixelgl"
"github.com/steelx/go-rpg-cgm/globals"
"github.com/steelx/go-rpg-cgm/state_machine"
"reflect"
)
type WaitState struct {
Character *Character
Map *GameMap
Entity *Entity
Controller *state_machine.StateMachine
mFrameResetSpeed, FrameCount float64
}
//character *Character, gMap *GameMap
func WaitStateCreate(args ...interface{}) *WaitState {
charV := reflect.ValueOf(args[0])
character := charV.Interface().(*Character)
gMapV := reflect.ValueOf(args[1])
gMap := gMapV.Interface().(*GameMap)
s := &WaitState{}
s.Character = character
s.Map = gMap
s.Entity = character.Entity
s.Controller = character.Controller
s.mFrameResetSpeed = 0.015
s.FrameCount = 0
return s
}
//The StateMachine requires each state to have
// four functions: Enter, Exit, Render and Update
func (s WaitState) IsFinished() bool {
return true
}
func (s *WaitState) Enter(data ...interface{}) {
// Reset to default frame
s.FrameCount = 0
s.Entity.SetFrame(s.Entity.StartFrame)
//check if an EXIT Trigger exists on given tile coords
//tileX, tileY := s.Map.GetTileIndex(s.Entity.TileX, s.Entity.TileY)
tileX, tileY := s.Entity.TileX, s.Entity.TileY
if trigger := s.Map.GetTrigger(tileX, tileY); trigger.OnExit != nil {
trigger.OnExit(s.Map, s.Entity, tileX, tileY)
}
}
func (s *WaitState) Render(win *pixelgl.Window) {
//pixelgl renderer
//s.Entity.TeleportAndDraw(s.Map)
}
func (s *WaitState) Exit() {}
func (s *WaitState) Update(dt float64) {
// If we're in the wait state for a few frames, reset the frame to
// the starting frame.
if s.FrameCount == 0 {
s.FrameCount = s.FrameCount + dt
if s.FrameCount >= s.mFrameResetSpeed {
s.FrameCount = 0
s.Entity.SetFrame(s.Entity.StartFrame)
}
}
if globals.Global.Win.Pressed(pixelgl.KeyLeft) {
s.Controller.Change("move", Direction{-1, 0})
}
if globals.Global.Win.Pressed(pixelgl.KeyRight) {
s.Controller.Change("move", Direction{1, 0})
}
if globals.Global.Win.Pressed(pixelgl.KeyDown) {
s.Controller.Change("move", Direction{0, 1})
}
if globals.Global.Win.Pressed(pixelgl.KeyUp) {
s.Controller.Change("move", Direction{0, -1})
}
}