#include "AttackExecutor.h" #include "AttackBehavior.h" #include "BehaviorKind.h" #include "EntityAdmin.h" #include "ModuleOwnerComponent.h" #include "MovementIntentComponent.h" #include "PositionComponent.h" #include "SelectedBehaviorComponent.h" #include "tracing.h" #include "WeaponComponent.h" void AttackExecutor::execute(EntityAdmin& admin) { TRACE(); // Ships: move toward the behavior target. admin.forEach( [&](entt::entity /*e*/, const AttackBehavior& attack, const SelectedBehaviorComponent& selected, const PositionComponent& pos, MovementIntentComponent& intent) { if (selected.winner != BehaviorKind::Attack) { return; } if (!attack.currentTarget) { return; } const entt::entity t = *attack.currentTarget; QVector2D dest = pos.value; if (admin.isValid(t) && admin.hasAll(t)) { dest = admin.get(t).value; } intent = MovementIntentComponent{true, dest}; }); // Weapons: assign the behavior target only if it is within this weapon's range. admin.forEach( [&](entt::entity /*we*/, WeaponComponent& weapon, const ModuleOwnerComponent& owner) { if (!admin.hasAll(owner.owner)) { return; } const SelectedBehaviorComponent& selected = admin.get(owner.owner); if (selected.winner != BehaviorKind::Attack) { return; } const AttackBehavior& attack = admin.get(owner.owner); if (!attack.currentTarget) { return; } const entt::entity t = *attack.currentTarget; if (!admin.isValid(t) || !admin.hasAll(t)) { return; } const QVector2D ownerPos = admin.get(owner.owner).value; const float dist = (admin.get(t).value - ownerPos).length(); if (dist <= weapon.range_tiles) { weapon.currentTarget = t; } }); }