Comments (4)
A possible fix for the downside, is doing this, when adding an entity:
// Create the entity
e := &MyEntity{}
// Loop over all systems. This way we loop only once, and add it to all systems we like
for _, system := range systems {
switch sys := system.(type) {
case *RenderSystem:
sys.Add(&e.BasicEntity, &e.SpaceComponent, &e.RenderComponent)
case *MouseSystem:
sys.Add(&e.BasicEntity, &e.SpaceComponent, &e.MouseComponent)
}
}
from ecs.
Just to illustrate, I re-wrote the AnimationSystem
to work with new syntax:
Old / current:
type AnimationSystem struct {
ecs.LinearSystem
}
func (a *AnimationSystem) New(*ecs.World) {}
func (*AnimationSystem) Type() string { return "AnimationSystem" }
func (*AnimationSystem) Pre() {}
func (*AnimationSystem) Post() {}
func (a *AnimationSystem) UpdateEntity(entity *ecs.Entity, dt float32) {
var (
ac *AnimationComponent
r *RenderComponent
ok bool
)
if ac, ok = entity.ComponentFast(ac).(*AnimationComponent); !ok {
return
}
if r, ok = entity.ComponentFast(r).(*RenderComponent); !ok {
return
}
ac.change += dt
if ac.change >= ac.Rate {
ac.NextFrame()
r.SetDrawable(ac.Cell())
}
}
New:
type animationEntity struct {
*ecs.BasicEntity
*AnimationComponent
*RenderComponent
}
type AnimationSystem struct {
entities []animationSystemEntity
}
func (a *AnimationSystem) Add(e *ecs.BasicEntity, anim *AnimationComponent, rend *RenderComponent) {
a.entities = append(a.entities, []animationEntity{e, anim, rend})
}
func (a *AnimationSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range a.entities {
if e.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
a.entities = append(a.entities[:delete], a.entities[delete+1:]...)
}
}
func (a *AnimationSystem) Update(dt float32) {
for _, e := range a.entities {
e.AnimationComponent.change += dt
if e.AnimationComponent.change >= e.AnimationComponent.Rate {
e.AnimationComponent.NextFrame()
e.RenderComponent.SetDrawable(e.AnimationComponent.Cell())
}
}
}
So this basically made the Pre()
and Post()
functions not-needed, but requires some copying the Add
and Remove
code (which makes sense, because this code is different when you use an array, then when you use a map
, or you might use anything completely different)
from ecs.
On the Gitter I have expressed that I would like to get this into master.
LETS DO IT!
from ecs.
^ Please, thank you 💃
v
from ecs.
Related Issues (20)
- Extensify tests HOT 3
- Duplicate Entities HOT 1
- Require tests to succeed before merging HOT 1
- `...string` instead of `[]string` for NewEntity HOT 3
- Consider unexporting the Systems type HOT 2
- Sync/atom instead of sync.Mutex in ecs.NewBasic
- Add Identifier interface, targeted at BasicEntity HOT 1
- Add a CONTRIBUTING.md
- move tests into their own package where possible
- Feature Request: Allow more than one interface in `World.AddSystemInterface` in and ex.
- Update to GitHub actions
- Add a go mod
- example that uses ecs.BasicFace HOT 2
- ecs.BasicFace does not exist HOT 3
- Update readme to utilize multiple interfaces
- Mistake in README
- World System "auto-register" HOT 1
- Question: What is the rational behind dt being a `float32` instead of `time.Duration`/`float64`? HOT 3
- Exclusion interface only matches if all components are present
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ecs.