Friday, August 19, 2016

Component based architecture: "pure" or some inheritance?


(I asked a similar question, but it had more questions inside it and I feel it wasn't clear enough, so I'm opening a new one).



Until recently I implemented all my games using an inheritance hierarchy. Those were simple games.


For example:


          Entity
/ \
Movable Static
/ \ / \
Orc Mage Tree Wall

This worked for simple games.


However recently I read about 'entity component systems'. Using this approach, a class doesn't inherit from another class in order to receive functionality. Instead it is composed with it, which allows for more flexible designs.



My question is this:


I know that each component is a class (derived from a common base class).


However, there is one thing I don't understand: In the 'component system' approach, is there one class that all entities are instances of - and the only difference between entities is the components they contain?


For example: There is a class Entity. All entities in the game are instances of this class.


The only differences between them is that they are composed with different Component objects.


For example:


Component aiComponent = new AIComponent();
Component movementComponent = new MovementComponent();
Component physicsComponent = new PhysicsComponent();
Component rangedAttackComponent = new RangedAttackComponent();


Entity orc = new Entity();
orc.addComponent(aiComponent);
orc.addComponent(movementComponent);
orc.addComponent(physicsComponent);

Entity shootingCastle = new Entity();
shootingCastle.addComponent(aiComponent);
shootingCastle.addComponent(rangedAttackComponent);
shootingCastle.addComponent(physicsComponent);


All of this as opposed to my old approach:


Orc orc1 = new Orc();
ShootingCastle castle1 = new ShootingCastle();

As you can see, both orc and shootingCastle are instances of the Entity class. But they are composed with different components.


Is this how 'entity component systems' are supposed to be? No inheritance hierarchy at all? Or is there still a basic inheritance hierarchy, but most of the logic is inside components?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...