65 lines
2.5 KiB
C++
65 lines
2.5 KiB
C++
#include "RepairExecutor.h"
|
|
|
|
#include "BehaviorKind.h"
|
|
#include "EntityAdmin.h"
|
|
#include "ModuleOwnerComponent.h"
|
|
#include "MovementIntentComponent.h"
|
|
#include "OrbitMath.h"
|
|
#include "PositionComponent.h"
|
|
#include "RepairBehavior.h"
|
|
#include "RepairToolComponent.h"
|
|
#include "SelectedBehaviorComponent.h"
|
|
#include "tracing.h"
|
|
|
|
void RepairExecutor::execute(EntityAdmin& admin)
|
|
{
|
|
TRACE();
|
|
|
|
// Ships: move toward the repair target.
|
|
admin.forEach<RepairBehavior, SelectedBehaviorComponent, PositionComponent,
|
|
MovementIntentComponent>(
|
|
[&](entt::entity /*e*/, const RepairBehavior& repair,
|
|
const SelectedBehaviorComponent& selected, const PositionComponent& pos,
|
|
MovementIntentComponent& intent)
|
|
{
|
|
if (selected.winner != BehaviorKind::Repair) { return; }
|
|
if (!repair.currentTarget) { return; }
|
|
|
|
const entt::entity t = *repair.currentTarget;
|
|
QVector2D dest = pos.value;
|
|
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
|
|
{
|
|
const QVector2D targetPos = admin.get<PositionComponent>(t).value;
|
|
dest = OrbitMath::computeOrbitDestination(pos.value, targetPos,
|
|
repair.orbitRadius_tiles);
|
|
}
|
|
intent = MovementIntentComponent{true, dest};
|
|
});
|
|
|
|
// Repair tools: prefer the behavior target if it is within tool range.
|
|
admin.forEach<RepairToolComponent, ModuleOwnerComponent>(
|
|
[&](entt::entity /*re*/, RepairToolComponent& tool, const ModuleOwnerComponent& owner)
|
|
{
|
|
if (!admin.hasAll<RepairBehavior, SelectedBehaviorComponent>(owner.owner))
|
|
{
|
|
return;
|
|
}
|
|
const SelectedBehaviorComponent& selected =
|
|
admin.get<SelectedBehaviorComponent>(owner.owner);
|
|
if (selected.winner != BehaviorKind::Repair) { return; }
|
|
|
|
const RepairBehavior& repair = admin.get<RepairBehavior>(owner.owner);
|
|
if (!repair.currentTarget) { return; }
|
|
|
|
const entt::entity t = *repair.currentTarget;
|
|
if (!admin.isValid(t) || !admin.hasAll<PositionComponent>(t)) { return; }
|
|
|
|
const QVector2D ownerPos = admin.get<PositionComponent>(owner.owner).value;
|
|
const float dist = (admin.get<PositionComponent>(t).value - ownerPos).length();
|
|
if (dist <= tool.range_tiles)
|
|
{
|
|
tool.currentTarget = t;
|
|
}
|
|
});
|
|
}
|