Hello!
Currently i am working in a Tower Defense Game (C++, SFML), which i started like 2 years ago, and i stopped developing it since i have a job.
Now i started again to teach a friend about C++, and i should rewrite alot of part.
A key question is how to store the Tower (and Minion) hierarchy.
Now i have a TowerBase class, which have the Derived types, like ArcherTower, MageTower etc.
This is pretty bad, because TowerBase have the values like Range, Damage etc, which should be initialized there. Now i reassign it in the derived constructor, and it seems like a terrible pattern.
It's also bad, because in the game i use all towers as a TowerBase, so it doesn't make much sense to derive them just because they can attack differently.
Okay, so the attack part i will use lambdas as template parameters, which will define how should it fire. It's okay.
I like the idea to template the Tower class instead of the class hierarchy (it's also faster, because there will be no virtual calls etc).
The problem come when i should define the towers. They have different damage, range etc.
Solition 1.: I would like the idea to somehow make it possible to typedef the towers with different values.
So when i say:
ArcherTower archerTower;
and
MageTower mageTower;
it would call a constructor, which will set different base values (like for archerTower, 30 damage, for mage, 40), while ArcherTower and MageTower are just different typedefs for the same templated class.
Okay so, that's the first solution, writing a template metaprogrammed class, and typedef it for different towers.
In this case, i must use a TowerBase class, which will let me use containers that store Towers.
This TowerBase should have at least a virtual Attack() function, which is again, problematic. This is because different Towers attack differently, but i just store TowerBase ptr-s, so i can't call the derived functions. How can i solve this?
Solution 2.: The other solution that come to my mind is to make factory functions, like "MakeArcherTower ()", which will do the things for me. It's slower and a bit harder to use, but i am sure i can do this, the solution one is something i don't exactly know, because i don't really have experience in metaprogramming. In this case, i shouldn't use a base class.
What do you think, which is the better? (or is it a 3rd option?)
I would also use this pattern to minions, so it would possible used instead of more than a hundred class.
Thank you for help!