-
Notifications
You must be signed in to change notification settings - Fork 2
/
collisionSystem.go
49 lines (43 loc) · 1.12 KB
/
collisionSystem.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
package overworld
import (
"github.com/griffithsh/squads/ecs"
"github.com/griffithsh/squads/event"
"github.com/griffithsh/squads/game"
)
// CollisionSystem generates collision events when two Tokens occupy the same node.
type CollisionSystem struct {
mgr *ecs.World
bus *event.Bus
}
// NewCollisionSystem creates a new Collision System.
func NewCollisionSystem(mgr *ecs.World, bus *event.Bus) *CollisionSystem {
cs := CollisionSystem{
mgr: mgr,
bus: bus,
}
bus.Subscribe(TokenMoved{}.Type(), cs.handleTokenMoved)
return &cs
}
func (cs *CollisionSystem) handleTokenMoved(t event.Typer) {
ev := t.(*TokenMoved)
team := cs.mgr.Component(ev.E, "Team").(*game.Team)
for _, e := range cs.mgr.Get([]string{"Token", "Team"}) {
// Entities don't collide with themselves.
if ev.E == e {
continue
}
// If they're on the same team, no collision.
otherTeam := cs.mgr.Component(e, "Team").(*game.Team)
if team.ID == otherTeam.ID {
continue
}
otherToken := cs.mgr.Component(e, "Token").(*Token)
if otherToken.Key == ev.To {
cs.bus.Publish(&TokensCollided{
E1: ev.E,
E2: e,
At: ev.To,
})
}
}
}