Hierarchy of my classes

I am working on a project and I’m not totally sure how to best structure and organize the hierarchy of its classes. It’s a simple turn-based game where your units can attack enemy units.

The structure is something like this.
There’s a main class which acts as a manager: it handles turns, score and also has a list of your squadrons and the enemy’s squadrons. Squadrons are classes that hold a group of units together.

Now, the thing is: say a click on one of my units and order it to attack an enemy unit, what exactly should happen in this case? My thought is to retrieve the manager and tell him who I want to attack, but I’m not sure this is the right approach.

  • Should a unit have a reference to the manager?
  • And if it should, is it correct to retrieve by searching for it (Camera.main.getComponent() ) or should it be passed from the manager to the squadron and then from the squadron to the unit?
  • Should it tell its squadron it wants to attack instead?

I really want to try to keep the code as bulletproof as possible from the start to try to learn something more about how to organize my projects. Thank you for your time and help!

##In your case, your class is fine, but would benefit from tweaking##

Your manager class does what a manager class should do, but it should be implemented like a singleton. Please check out this link to see how this is done. This will eliminate the need for GetComponent() and provide a minor performance increase.

It’s also fine if your unit needs to access your Manager class, as long as you are sure it needs to. For instance, if a character is looking for a target, it would make sense to access the Manager class to populate a list of all the available targets by calling “GetAllValidTargets()”. It would not make sense if your Manager class is being used to hold functions like “GetCharacterName()” or “IsCharacterAlive().”

It makes sense that you would have to check with the Manager before attacking a squadron, however just make sure that you aren’t stuffing any code in there that can be handled by a unit instead.