In my 2d game in unity and c sharp, my friendly units are squares. Each square has a grid on it. Each cell of the grid can have one enemy attached to it at a time.
This means that my enemy's targets are not just the friendly game object, but the cell as well. Because of this, I've created a Target class for this (just contains a gameobject and cell). Enemies carry their Target reference so they know where to move to and who to attack.
However, my friendlies do not need the cell to target an enemy. Enemies are small and don't have grids on them. I cannot figure out a nice way to make this polymorphic(?) Target concept work without doing checks for "if cell is not null" in all of my targeting code.
For example, code in my behavior tree for checks like "HasTarget" are tightly coupled to check for existence of a gameobject and cell. It just feels wrong. Am I stuck using custom checks and code for an enemy vs friendly target?
I tried interfaces but they require the same return types. That differs depending on if they are an enemy or friendly. Something like unit.Itargettable.GetTarget() won't work.
Help!
cell
,square
,enemy
implement an interfaceITarget
, what goes wrong? ITarget could haveGetTarget()
withITarget
as return type, though friendlies could returnenemy
whileenemy
returnssquare
orgrid
. (pardon me if I misunderstood the target relations.) The pool to pick a new target from, could be split into the invididual objects rather than ITarget (or you can make other solutions like a bool IsFriendly). \$\endgroup\$GetTargetPos()
wherecell.GetTargetPos()
returns something different thanenemy.GetTargetPos()
? You could also turn it around, if a friendly or enemy has a target, you can saytarget.GetPosition()
and based on whatever target is, it returns a different position. The Enemy class can reference a Friendly (unless they have to be able to attack eachother) while friendly can hold an Enemy. Unless there are some things I don't know about yet, it feels like abstraction (\w interface) would be fine for you. (Also, wouldn't thetarget.transform.position
suffice?) \$\endgroup\$target.GetPosition()
and only reference a Unit, not a Friendly or Enemy. I wanted to keep it super abstract. I suppose I could do atarget.GetPosition()
that has some logic to check if cell is null and return the position of the gameobject instead. I waa hoping for this level of abstraction so that I can share AI nodes across Units. If I don't do this, I will need an EnemyAI and a FriendlyAI instead of just a UnitAI. Also, cells are not objects/monobehaviors. \$\endgroup\$