The object pool part makes sense. I assume the Hierarchy is like this?
ObjectPools (root)
> ObjectPool
>> Enemy
>> Enemy
>> Enemy
> ObjectPool
>> Enemy
> ObjectPool
>> Enemy
>> Enemy
This means there are 3 ObjectPool game objects parented to ObjectPools (root). And the ObjectPool game objects have got children.
Is the PoolManager assigned to the root game object?
There are several questions you’ll have to answer to find a solution for your problem:
- What exactly is the role of an ObjectPool object?
- What exactly is the purpose of a Pathfinder object?
- What exactly is the purpose of an EnemyMover object?
- Whose Recalculate method is supposed to get called in which case? (Are all Recalculate path methods supposed to be called? Or just a subset of them?)
If you know exactly how the objects are supposed to interact with one another in which case, you will be able to make your idea work. The implementation is simple because you know everything you need: classes, objects, methods, arrays, if-statements.
You do not have to use Gary’s solution if it makes implementing your ideas difficult. Gary’s solution solves Gary’s problems.
Get rid of the Broadcaster (BroadcastMessage). It is not only very slow but you also cannot “see” what happens here. Since you have the reference to all Pathfinder objects, you could recalculate the path by calling the RecalculatePath method on them.
A potential concept
Each Pathfinder object contains a valid path (pool.path), which is associated with one ObjectPool object. Each Enemy object has got its own path (enemy.path). When a new enemy gets spawned, the pool.path gets assigned to its enemy.path. No new calculations.
When a new tower is supposed to get spawned, each Pathfinder checks the assumed tower.coordinates with the coordinates in its pool.path. No calculation here. If the tower.coordinates are not in the pool.path, there is no need to recalculate anything.
If the tower.coordinates are in the pool.path, you recalculate the pool.path. If the new pool.path is valid, you have to ensure that your enemies have got valid paths too. You have multiple options:
a) You recalculate an individual enemy.path for all enemies. (Worst performance but it covers all cases)
b) You recalculate an individual enemy.path for all enemies whose path is about to be blocked by the tower. (Better performance than a))
c) You recalculate an individual enemy.path for all enemies whose path is about to be blocked by the tower. And the recalculated path is just a path to the closest waypoint of the new pool.path.
d) You analyse the situation and try to figure out which paths or parts of paths need to be recalculated to make your game not only performant but also look/feel good.
No tower if one of the enemy gets an invalid path. Then the enemy continues using its enemy.path. In that case, you don’t want to recalculate the enemy.path but reuse the original data.