I’d like some high-level advice about how best to structure the scripts, classes, and code in my game.
Let’s say I have an RTS game with many different types of units arranged in tiers. For example:
Units:
- Worker:
A) Farmer
– Apple Farmer
– Peach Farmer
– Banana Farmer
B) Miner
– Stone Miner
– Gold Miner
– Crystal Miner
C) Smith
– Blacksmith
– Goldsmith
– Carpenter - Combat
A) Melee
– Swordsman
---- 1h Swordsman
---- 2h Swordsman
---- Heavy Swordsman
– Pikeman
– Spearman
– Mace
B) Archer
– Normal
– Heavy
– Fire
– Ice
C) Magic
– Wizard
---- Fire Wizard
---- Ice Wizard
– Priest
– Shaman - Monsters
A) Skeleton
– Skeleton Swordsman
– Skeleton Archer
– Skeleton Mage
---- Skeleton Fire Mage
---- Skeleton Ice Mage
B) Goblin
C) Orc
So there is code that is common to all the units, and some code which is very specific to only one kind of sub-sub-unit.
What is the best way to structure this? Should I create a script for each type and sub-type and then attach a bunch of script components to each unit gameobject? For example, the Heavy Swordsman would have 5 scripts attached: unit script, combat script, melee script, swordmans script, heavy script.
Is this correct? Somehow it seems excessively cumbersome because it would be hard to remember which properties and variables and functions are contained in which script, at which level.
Here’s a specific problem I’m thinking might arise: let’s say I have 1 of each type of unit standing near each other and they all get hit with a giant fireball spell. Now the fireball object needs to communicate with each of the units what should occur. Perhaps the Ice Wizard is immune, but the Pikeman dies, but he needs to play a specific Pikeman death animation, and so on across all the units. How would I even grab references to all the different script components in this case? I’d need to reference each script component by its type name which I would not easily know.
Is there a better way? What is the proper way to structure this?
Also, these units would all have a lot of data associated with them which would probably be better to define or load via some text file. For example: name, health, armor, damage type, flavor text, etc. It seems this kind of data should not be in the unit’s logic code but outside of it and loaded in. Can you give a high-level explanation of the most elegant way to handle this?
Thanks