-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactionSystem.go
44 lines (32 loc) · 1.2 KB
/
interactionSystem.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
package foodzy
import (
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
)
type InteractionSystem struct {
manager *ecs.EntityManager
}
func NewInteractionSystem(manager *ecs.EntityManager) *InteractionSystem {
return &InteractionSystem{manager: manager}
}
func (s *InteractionSystem) Draw(screen *ebiten.Image) {}
func (s *InteractionSystem) Update() error {
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
return nil
}
mx, my := ebiten.CursorPosition()
entities := s.manager.QueryByComponents(component.MouseColliderType, component.InteractionType, component.TransformType)
for _, v := range entities {
boundingBox := v.GetComponent(component.MouseColliderType).(*component.MouseCollider)
pos := v.GetComponent(component.TransformType).(*component.Transform)
if mouseIsInBoundingbox(pos.X, pos.Y, boundingBox.Width, boundingBox.Height, float64(mx), float64(my)) {
interaction := v.GetComponent(component.InteractionType).(*component.Interaction)
interaction.Action(s.manager)
}
}
return nil
}
func mouseIsInBoundingbox(px, py, w, h, mx, my float64) bool {
return mx >= px && mx <= (px+w) && my >= py && my <= (py+h)
}