Skip to content

Commit

Permalink
update ecs package (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta authored Nov 6, 2022
1 parent a4ca6bd commit e67fd23
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 109 deletions.
35 changes: 9 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,19 @@ func SomeFunction(ecs *ecs.ECS) {
// ...
}

ecs.AddSystem(
ecs.System{
Update: SomeFunction,
}
)
ecs.AddSystem(SomeFunction)
```

Each `System` can have a `Draw()` function.
We can provide `Renderer` for certain system.

```go
ecs.AddSystem(
ecs.System{
Update: UpdateBackground,
Draw: DrawBackground,
}
)
ecs.AddRenderer(ecs.LayerDefault, DrawBackground)

// Draw all systems
ecs.Draw(screen)
```

The `Layer` option of the `System` allows control of the order of rendering systems and to which screen to render. A `Layer` is just an `int` value. The default value is just `0`.

The `Layer` parameter allows us to control the order of rendering systems and to which screen to render. A `Layer` is just an `int` value. The default value is just `0`.

For example:
```go
Expand All @@ -291,18 +281,11 @@ const (

// ...

ecs.AddSystem(
ecs.System{
Layer: LayerBackground,
Update: UpdateBackground,
Draw: DrawBackground,
}
ecs.System{
Layer: LayerActors,
Update: UpdateActors,
Draw: DrawActors,
}
)
ecs.
AddSystem(UpdateBackground).
AddSystem(UpdateActors).
AddRenderer(LayerBackground, DrawBackground).
AddRenderer(LayerActors, DrawActors)

// ...

Expand Down
42 changes: 14 additions & 28 deletions ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
// LayerID is used to specify a layer.
type LayerID int

// LayerDefault is the default layer.
const LayerDefault LayerID = 0

// ECS represents an entity-component-system.
type ECS struct {
// World is the underlying world of the ECS.
World donburi.World
// Time manages the time of the world.
Time *Time

layers []*Layer
systems []UpdateSystem
startupSystems []UpdateSystem
layers []*Layer
systems []System
}

// NewQuery creates a new query.
Expand All @@ -38,30 +40,21 @@ func NewECS(w donburi.World) *ECS {
World: w,
Time: NewTime(),

systems: []UpdateSystem{},
layers: []*Layer{},
startupSystems: []UpdateSystem{},
systems: []System{},
layers: []*Layer{},
}

return ecs
}

// AddSystems adds systems.
func (ecs *ECS) AddSystems(s ...System) *ECS {
for _, ss := range s {
ecs.AddSystem(ss)
}
// AddSystem adds a system.
func (ecs *ECS) AddSystem(s System) *ECS {
ecs.systems = append(ecs.systems, s)
return ecs
}

// AddSystem adds a system.
func (ecs *ECS) AddSystem(s System) *ECS {
if s.Update != nil {
ecs.addUpdateSystem(s.Update)
}
if s.Draw != nil {
ecs.addDrawSystem(s.Layer, s.Draw)
}
func (ecs *ECS) AddRenderer(l LayerID, r Renderer) *ECS {
ecs.getLayer(l).addRenderer(r)
return ecs
}

Expand Down Expand Up @@ -123,14 +116,7 @@ func (ecs *ECS) getLayer(layerID LayerID) *Layer {
return ecs.layers[layerID]
}

func (ecs *ECS) addUpdateSystem(systems ...UpdateSystem) *ECS {
for _, s := range systems {
ecs.systems = append(ecs.systems, s)
}
return ecs
}

func (ecs *ECS) addDrawSystem(l LayerID, s DrawSystem) *ECS {
ecs.getLayer(l).addDrawSystem(s)
func (ecs *ECS) addRenderer(l LayerID, r Renderer) *ECS {
ecs.getLayer(l).addRenderer(r)
return ecs
}
14 changes: 4 additions & 10 deletions ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ func TestECS(t *testing.T) {
}

for _, sys := range systems {
ecs.AddSystem(System{
Update: sys.system.Update,
Layer: sys.layer,
Draw: sys.system.Draw,
})
ecs.AddSystem(sys.system.Update)
ecs.addRenderer(sys.layer, sys.system.Draw)
}

ecs.Update()
Expand Down Expand Up @@ -102,11 +99,8 @@ func TestECSLayer(t *testing.T) {
}

for _, sys := range systems {
ecs.AddSystem(System{
Update: sys.system.Update,
Layer: sys.layer,
Draw: sys.system.Draw,
})
ecs.AddSystem(sys.system.Update)
ecs.addRenderer(sys.layer, sys.system.Draw)
}

ecs.DrawLayer(0, ebiten.NewImage(1, 1))
Expand Down
10 changes: 5 additions & 5 deletions ecs/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (

type Layer struct {
*layer
systems []DrawSystem
renderers []Renderer
}

func newLayer(l *layer) *Layer {
return &Layer{l, []DrawSystem{}}
return &Layer{l, []Renderer{}}
}

func (l *Layer) draw(e *ECS, i *ebiten.Image) {
screen := i
for _, s := range l.systems {
for _, s := range l.renderers {
s(e, screen)
}
}

func (l *Layer) addDrawSystem(s DrawSystem) {
l.systems = append(l.systems, s)
func (l *Layer) addRenderer(r Renderer) {
l.renderers = append(l.renderers, r)
}

var (
Expand Down
11 changes: 2 additions & 9 deletions ecs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import (
)

// UpdateSystem is a system that updates the world.
type UpdateSystem func(ecs *ECS)
type System func(ecs *ECS)

// DrawSystem is a system that draws the world.
type DrawSystem func(ecs *ECS, screen *ebiten.Image)

// System represents a system.
type System struct {
Layer LayerID
Update UpdateSystem
Draw DrawSystem
}
type Renderer func(ecs *ECS, screen *ebiten.Image)
37 changes: 9 additions & 28 deletions examples/bunnymark_ecs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,15 @@ func NewGame() *Game {

metrics := system.NewMetrics(&g.bounds)

g.ecs.AddSystems(
// Systems are executed in the order they are added.
ecs.System{
Update: system.NewSpawn().Update,
},
ecs.System{
Layer: layers.LayerBackground,
Draw: system.DrawBackground,
},
ecs.System{
Layer: layers.LayerMetrics,
Update: metrics.Update,
Draw: metrics.Draw,
},
ecs.System{
Update: system.NewBounce(&g.bounds).Update,
},
ecs.System{
Update: system.Velocity.Update,
},
ecs.System{
Update: system.Gravity.Update,
},
ecs.System{
Layer: layers.LayerBunnies,
Draw: system.Render.Draw,
},
)
g.ecs.
AddSystem(system.NewSpawn().Update).
AddSystem(metrics.Update).
AddSystem(system.NewBounce(&g.bounds).Update).
AddSystem(system.Velocity.Update).
AddSystem(system.Gravity.Update).
AddRenderer(layers.LayerBackground, system.DrawBackground).
AddRenderer(layers.LayerMetrics, metrics.Draw).
AddRenderer(layers.LayerBunnies, system.Render.Draw)

return g
}
Expand Down
4 changes: 1 addition & 3 deletions features/hierarchy/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ func TestHierarchy(t *testing.T) {
w := donburi.NewWorld()
ecs := ecslib.NewECS(w)

ecs.AddSystem(ecslib.System{
Update: HierarchySystem.RemoveChildren,
})
ecs.AddSystem(HierarchySystem.RemoveChildren)

parent := donburi.NewTag().SetName("parent")
child := donburi.NewTag().SetName("child")
Expand Down

0 comments on commit e67fd23

Please sign in to comment.