Overriding component behavior
Posted
by
deft_code
on Game Development
See other posts from Game Development
or by deft_code
Published on 2011-06-20T04:31:58Z
Indexed on
2011/06/20
16:40 UTC
Read the original article
Hit count: 325
component-based
|entity-system
I was thinking of how to implement overriding of behaviors in a component based entity system. A concrete example, an entity has a heath component that can be damaged, healed, killed etc. The entity also has an armor component that limits the amount of damage a character receives.
Has anyone implemented behaviors like this in a component based system before?
How did you do it?
If no one has ever done this before why do you think that is. Is there anything particularly wrong headed about overriding component behaviors?
Below is rough sketch up of how I imagine it would work. Components in an entity are ordered. Those at the front get a chance to service an interface first. I don't detail how that is done, just assume it uses evil dynamic_cast
s (it doesn't but the end effect is the same without the need for RTTI).
class IHealth
{
public:
float get_health( void ) const = 0;
void do_damage( float amount ) = 0;
};
class Health : public Component, public IHealth
{
public:
void do_damage( float amount )
{
m_damage -= amount;
}
private:
float m_health;
};
class Armor : public Component, public IHealth
{
public:
float get_health( void ) const
{
return next<IHealth>().get_health();
}
void do_damage( float amount )
{
next<IHealth>().do_damage( amount / 2 );
}
};
entity.add( new Health( 100 ) );
entity.add( new Armor() );
assert( entity.get<IHealth>().get_health() == 100 );
entity.get<IHealth>().do_damage( 10 );
assert( entity.get<IHealth>().get_health() == 95 );
Is there anything particularly naive about the way I'm proposing to do this?
© Game Development or respective owner