Game Development Reference
In-Depth Information
Component-based entities
There is another way to implement our entities as well. It implements an Entity
class that is used to represent any possible entity. The difference is that this Entity
class contains a collection of Components . A component is a class that represents
a certain action or feature that an object in the game world can have. So, for ex-
ample, you might have an Armor component that allows an entity to have an armor
value, or a Health component that allows the entity to have health and the ability
to take damage. This Health component would probably have a property to set the
maximum health for the entity and another one that is used to get the current health
value for the entity.
This is a very powerful approach because you can give any entity health (and the
ability to take damage) just by adding the Health component into that entity. So, as
you can see, each entity is represented by the basic Entity class and gets all of
its features and properties from the components that are added into it. This is what
makes this approach so powerful. You write the Health code once and then you can
re-use it on any number of entities without having to rewrite it for each one. The
component-based entities are a bit trickier to program than regular entities though.
For example, we would need to add a method on the Entity class that lets you
pass in a component type to specify which component you would like to access. It
would then find the component of the specified type and return it for you to use. You
would usually make your entity system such that it will not allow an entity to have
more than one component of any given type as this generally wouldn't make much
sense anyway. For example, giving one entity two Health components doesn't make
much sense.
Search WWH ::




Custom Search