/
character.go
81 lines (67 loc) · 2.12 KB
/
character.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
package game_map
import (
"github.com/steelx/go-rpg-cgm/state_machine"
)
type CharacterStateBase struct {
Character *Character
Map *GameMap
Entity *Entity
Controller *state_machine.StateMachine
}
type Character struct {
Id string
Anims map[string][]int
Facing string
Entity *Entity
Controller *state_machine.StateMachine //[Name] -> [function that returns state]
DefaultState, PrevDefaultState string //"wait"
PathIndex int
Path []string //e.g. ["up", "up", "up", "left", "right", "right",]
TalkIndex int //used during speech tracking
}
func CharacterCreate(def CharacterDefinition, controllerStates map[string]func() state_machine.State) *Character {
ch := &Character{
Id: def.Id,
Facing: def.FacingDirection,
Entity: CreateEntity(def.EntityDef),
Controller: state_machine.Create(controllerStates),
DefaultState: def.DefaultState,
}
ch.Anims = make(map[string][]int)
for k, v := range def.Animations {
ch.Anims[k] = v
}
return ch
}
func (ch Character) GetFacedTileCoords() (x, y float64) {
var xOff, yOff float64 = 0, 0
if ch.Facing == CharacterFacingDirection[3] {
xOff = -1 //"left"
} else if ch.Facing == CharacterFacingDirection[1] {
xOff = 1 //"right"
} else if ch.Facing == CharacterFacingDirection[0] {
yOff = -1 //"up"
} else if ch.Facing == CharacterFacingDirection[2] {
yOff = 1 //"down"
}
x = ch.Entity.TileX + xOff
y = ch.Entity.TileY + yOff
return
}
func (ch *Character) SetFacing(dir int) {
ch.Facing = CharacterFacingDirection[dir]
}
func (ch *Character) FollowPath(path []string) {
ch.PathIndex = 0
ch.Path = path
//ch.PrevDefaultState = ch.DefaultState //this is causing problem
ch.PrevDefaultState = "wait"
ch.DefaultState = "follow_path"
ch.Controller.Change("follow_path", nil)
}
func (ch *Character) GetCombatAnim(id string) []int {
if anims, ok := ch.Anims[id]; ok {
return anims
}
return []int{ch.Entity.StartFrame}
}