forked from x-hgg-x/goecsengine
/
systems.go
61 lines (52 loc) · 2.11 KB
/
systems.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
package main
import (
"fmt"
"math/rand"
c "github.com/ghtalpo/goecsengine/components"
m "github.com/ghtalpo/goecsengine/math"
w "github.com/ghtalpo/goecsengine/world"
"github.com/hajimehoshi/ebiten/v2"
ecs "github.com/x-hgg-x/goecs/v2"
)
// DemoSystem is a demo system
func DemoSystem(world w.World) {
gameComponents := world.Components.Game.(*Components)
gameResources := world.Resources.Game.(*Game)
gameResources.Rotation += world.Resources.InputHandler.Axes[RotationAxis] * 5 / ebiten.DefaultTPS
gameResources.Depth += world.Resources.InputHandler.Axes[DepthAxis] * 2 / ebiten.DefaultTPS
world.Manager.Join(gameComponents.Gopher, world.Components.Engine.Transform).Visit(ecs.Visit(func(entity ecs.Entity) {
transform := world.Components.Engine.Transform.Get(entity).(*c.Transform)
transform.Rotation = gameResources.Rotation
transform.Depth = gameResources.Depth
}))
// Add a gopher entity
if world.Resources.InputHandler.Actions[AddEntityAction] {
gopherEntity := LoadEntities("metadata/gopher.toml", world)
for iEntity := range gopherEntity {
transform := world.Components.Engine.Transform.Get(gopherEntity[iEntity]).(*c.Transform)
transform.Rotation = gameResources.Rotation
transform.Depth = gameResources.Depth
transform.Translation = m.Vector2{
X: float64(rand.Intn(world.Resources.ScreenDimensions.Width)),
Y: float64(rand.Intn(world.Resources.ScreenDimensions.Height)),
}
}
}
// Delete a gopher entity
if world.Resources.InputHandler.Actions[DeleteEntityAction] {
gophers := world.Manager.Join(gameComponents.Gopher, gameComponents.Sticky.Not())
if firstGopher := ecs.GetFirst(gophers); firstGopher != nil {
world.Manager.DeleteEntity(*firstGopher)
}
}
// Update text info
world.Manager.Join(world.Components.Engine.Text, world.Components.Engine.UITransform).Visit(ecs.Visit(func(entity ecs.Entity) {
text := world.Components.Engine.Text.Get(entity).(*c.Text)
if text.ID == "rotation" {
text.Text = fmt.Sprintf("Gopher rotation: %.2f", gameResources.Rotation)
}
if text.ID == "depth" {
text.Text = fmt.Sprintf("Gopher depth: %.2f", gameResources.Depth)
}
}))
}