mardi 23 mai 2017

reduce Duck-typing phenomenon in entity-component-system

How to reduce Duck-typing phenomenon in entity-component-system?

Example

(These are pseudo code. Many things are simplified and omitted.)

There are 2 systems in my ECS :-

System_Projectile : manage all projectile and bullet aspect.
System_Physic : manage physic's component.

There are 2 components-type : Com_Projectile , Physics.

Sometimes, I find that it is nice to cache pointer to another entity in some certain component :- .

class Com_Projectile{
    public: Entity* basePhysic;
};

Here is how I currently change position of the Com_Projectile via its field : basePhysic.

class System_Projectile{
    public: void manage(Entity* projectile){
        Com_Projectile* comP = getComponent<Com_Projectile>(projectile);
        //suffer duck-typing at "comP->basePhysic"
        System_Physic::setVelocity(comP->basePhysic,Vec3(1,0,0));
    }
};

getComponent<> is a function to request a certain component from an entity.

Problem

The real program based on the above snippet works OK.
However, when coding, Com_Projectile::basePhysic suffer duck-typing.

  • I get no clues about basePhysic's type at all.
  • I have to be conscious about type of basePhysic.
  • Then, I have to recall name of system (System_Physic::) that can do a thing I want (setVelocity()).
  • There are a lot of indirection for my brain.

In my old days, when I use a lot of (deep) inheritance, it is much easier, like this :-

    basePhysic->setVelocity(Vec3(1,0,0));

I really miss the cute content assists that list all the functions that related to physics.

enter image description here

Question

How to reduce duck-typing in some certain part of ECS system?
More specifically, what is a design pattern to enable the cute content-assist again?

My current workaround is to let Com_Projectile cache Physic* basePhysic instead of Entity*, but it will promote unwanted (?) coupling.

class Com_Projectile{
    public: Physic* basePhysic; //edited from "Entity* basePhysic"
};

Aucun commentaire:

Enregistrer un commentaire