-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugRendererSystem.go
75 lines (57 loc) · 2.15 KB
/
debugRendererSystem.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
package foodzy
import (
"fmt"
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/colornames"
"image/color"
)
var mouseDebugColor = color.RGBA{0, 255, 255, 150}
var collisionDebugColor = color.RGBA{178, 34, 34, 150}
type DebugRendererSystem struct {
manager *ecs.EntityManager
enabled bool
}
func NewDebugRendererSystem(manager *ecs.EntityManager) *DebugRendererSystem {
return &DebugRendererSystem{manager: manager}
}
func (s *DebugRendererSystem) Draw(screen *ebiten.Image) {
if !s.enabled {
return
}
mouseEntities := s.manager.QueryByComponents(component.MouseColliderType, component.TransformType)
for _, v := range mouseEntities {
boundingBox := v.GetComponent(component.MouseColliderType).(*component.MouseCollider)
transform := v.GetComponent(component.TransformType).(*component.Transform)
img := ebiten.NewImage(int(boundingBox.Width), int(boundingBox.Height))
img.Fill(mouseDebugColor)
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(transform.Scale, transform.Scale)
op.GeoM.Translate(transform.X, transform.Y)
screen.DrawImage(img, op)
}
collisionEntities := s.manager.QueryByComponents(component.CollisionType, component.TransformType)
for _, v := range collisionEntities {
boundingBox := v.GetComponent(component.CollisionType).(*component.Collision)
transform := v.GetComponent(component.TransformType).(*component.Transform)
img := ebiten.NewImage(int(boundingBox.Width), int(boundingBox.Height))
img.Fill(collisionDebugColor)
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(transform.Scale, transform.Scale)
op.GeoM.Translate(transform.X, transform.Y)
screen.DrawImage(img, op)
}
fps := fmt.Sprintf("FPS: %.1f", ebiten.CurrentFPS())
text.Draw(screen, fps, FontSmall, 10, 50, colornames.Firebrick)
tps := fmt.Sprintf("FPS: %.1f", ebiten.CurrentFPS())
text.Draw(screen, tps, FontSmall, 100, 50, colornames.Firebrick)
}
func (s *DebugRendererSystem) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyD) {
s.enabled = !s.enabled
}
return nil
}