Git Product home page Git Product logo

Comments (4)

EtienneBruines avatar EtienneBruines commented on August 9, 2024

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.

EtienneBruines avatar EtienneBruines commented on August 9, 2024

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.

paked avatar paked commented on August 9, 2024

On the Gitter I have expressed that I would like to get this into master.

LETS DO IT!

from ecs.

Newbrict avatar Newbrict commented on August 9, 2024

^ Please, thank you 💃
v

from ecs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.