-
Notifications
You must be signed in to change notification settings - Fork 18
/
character_action_system.go
84 lines (68 loc) · 2.15 KB
/
character_action_system.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
package system
import (
"log"
"strconv"
"time"
"github.com/EngoEngine/ecs"
"github.com/project-midgard/midgarts/internal/character/actionindex"
"github.com/project-midgard/midgarts/internal/character/statetype"
"github.com/project-midgard/midgarts/internal/component"
"github.com/project-midgard/midgarts/internal/entity"
"github.com/project-midgard/midgarts/internal/fileformat/grf"
)
type CharacterActionable interface {
component.CharacterStateComponentFace
component.CharacterSpriteRenderInfoComponentFace
}
type CharacterActionSystem struct {
grfFile *grf.File
characters map[string]*entity.Character
}
func NewCharacterActionSystem(grfFile *grf.File) *CharacterActionSystem {
return &CharacterActionSystem{
grfFile,
map[string]*entity.Character{},
}
}
func (s *CharacterActionSystem) Add(char *entity.Character) {
cmp, e := component.NewCharacterAttachmentComponent(s.grfFile, component.CharacterAttachmentComponentConfig{
Gender: char.Gender,
JobSpriteID: char.JobSpriteID,
HeadIndex: char.HeadIndex,
})
if e != nil {
log.Fatal(e)
}
char.SetCharacterAttachmentComponent(cmp)
s.characters[strconv.Itoa(int(char.ID()))] = char
}
func (s *CharacterActionSystem) AddByInterface(o ecs.Identifier) {
char := o.(*entity.Character)
s.Add(char)
}
func (s *CharacterActionSystem) Update(dt float32) {
for _, c := range s.characters {
now := time.Now()
previousAnimationHasEnded := now.After(c.AnimationEndsAt)
stopPreviousAnimation := previousAnimationHasEnded
if c.PreviousState != statetype.Walking {
stopPreviousAnimation = true
}
c.ActionIndex = actionindex.GetActionIndex(c.State)
if (c.State != c.PreviousState && c.State != statetype.Idle) ||
(c.State == statetype.Idle && stopPreviousAnimation) {
c.AnimationStartedAt = now
// TODO: treat special case when attacking
var forcedDuration time.Duration
c.ForcedDuration = forcedDuration
c.FPSMultiplier = 1.0
if c.State == statetype.Walking {
c.FPSMultiplier = c.MovementSpeed
}
}
c.AnimationEndsAt = now.Add(c.AnimationDelay)
}
}
func (s *CharacterActionSystem) Remove(e ecs.BasicEntity) {
delete(s.characters, strconv.Itoa(int(e.ID())))
}