Battle Simulation Logic in ECS #3
Find Target Systems
In this article I will cover not just one but three systems, because each of them is so darn simple.
This is reactive, it is triggered when a group of units is deployed on the battlefield. It uses the findTownObjectTargetForGroup
function we described in Battle Simulation Logic in ECS #2 and ads the Target
component if the target is not null
.
If you thought the first system was simple, have a look at the second and third:
This two systems are there to trigger the TowerStartAttackService
. There are to of them because their triggers are different. In first one we call updateTargetForTowers
if a unit has a Position
component added or updated. In second case, it is triggered when a Destroyed
component is added. Nowadays I would define a multi reactive system to combine those two system into one, but back in 2013 we did not invent this concept yet 🤷♂️.
TowerStartAttackService
::updateTargetForTowers
This service is iterating over all entities which have TownObject
and StrikeRange
components and don’t have Destroyed
or UpgradeStartTime
components. When we iterate over the entities we check if the entity has Attacking
component and if so, is the target of the attacking component is not destroyed and still in range. If the town object is attacking a non destroyed in range target, we skip over it, as it still has something to do. Otherwise we search for units in range and set Attacking
component if search was successful. Easy, isn’t it? 😎