/
playerhandlingsystem.go
85 lines (72 loc) · 1.79 KB
/
playerhandlingsystem.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
package systems
import (
"github.com/Eshumkv/kvasir-go/scenes"
"github.com/eshumkv/Kvasir-go/ecs"
)
type PlayerHandlingSystem struct {
id string
mngr *ecs.SystemManager
}
// NewPlayerHandlingSystem returns a pointer to a new PlayerHandlingSystem.
func NewPlayerHandlingSystem() *PlayerHandlingSystem {
return &PlayerHandlingSystem{
id: "PlayerHandlingSystem",
}
}
// Init initializes the system.
func (s *PlayerHandlingSystem) Init(mngr *ecs.SystemManager) {
s.mngr = mngr
}
// Update handles the update of the system.
func (s *PlayerHandlingSystem) Update(dt float64) {
ret, err := s.mngr.SendMessage(MessageGetCommands, nil)
if err != nil {
return
}
commands := ret.([]bool)
if commands[CommandShoot] {
s.mngr.SendMessage(MessageGameChangeScene, scenes.TestScene(NewMyEntityManager()))
}
ret, err = s.mngr.SendMessage(MessageGetEntitiesOfSystem, s.id)
if err != nil {
return
}
entities := ret.([]ecs.Entity)
speed := 100.0
for _, entity := range entities {
xAdd, yAdd := 0.0, 0.0
if commands[CommandLeft] {
xAdd = -speed * dt
}
if commands[CommandRight] {
xAdd = speed * dt
}
if commands[CommandUp] {
yAdd = -speed * dt
}
if commands[CommandDown] {
yAdd = speed * dt
}
if xAdd != 0.0 || yAdd != 0.0 {
newValueX := entity.X() + xAdd
newValueY := entity.Y() + yAdd
s.mngr.SendMessage(
MessageSetEntityLocation,
struct {
id uint64
system string
x float64
y float64
}{entity.ID(), s.id, newValueX, newValueY})
}
}
}
// Priority defines the priority of this system.
func (s PlayerHandlingSystem) Priority() uint {
return 50
}
// HandleMessage handles any messages that need to be dealt with.
func (s *PlayerHandlingSystem) HandleMessage(
msg ecs.Message, data interface{}) interface{} {
return nil
}