I am using entity-base-components system.
I have many type of stationary objects e.g.
- Wall = blocks
- Fire Turret = blocks + shooter
- Water Turret = blocks + shooter
- Bunker = blocks + spawner
Here is the factory of stationary objects :-
class StationaryObject{
enum typeOfObject_enum{WALL,FIRE_TURRET, ....};
Entity* create(typeOfObject_enum theType){ //it is enum
switch(theType){
case WALL: ... create some mesh, some physic body ....
case FIRE_TURRET: .... create some mesh, some physic body+ unique logic 20 lines ....
....
}
}
}
It works really good.
Question:
Now I want to create 100 types of Stationary objects, where should I store it?
Store all of them in class StationaryObjectwill make the class too big (?).
Note that there are tiny-but-unique logic in each type of object.
You could create a map from
typeOfObject_enumto each object factory and then you can register factories in the map as you wish.Each object factory could be something like a
std::function<std::unique_ptr<Entity>()>:Live demo.
Or if you prefer, you could use implementations of an abstract factory class:
Live demo.