70 lines
2.7 KiB
C++
70 lines
2.7 KiB
C++
#include "RepairExecutor.h"
|
|
|
|
#include "BehaviorKind.h"
|
|
#include "DynamicBodyComponent.h"
|
|
#include "EntityAdmin.h"
|
|
#include "ModuleOwnerComponent.h"
|
|
#include "MovementIntentComponent.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 center = pos.value;
|
|
float radius = 0.0f;
|
|
QVector2D centerVelocity;
|
|
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
|
|
{
|
|
center = admin.get<PositionComponent>(t).value;
|
|
radius = repair.orbitRadius_tiles;
|
|
if (admin.hasAll<DynamicBodyComponent>(t))
|
|
{
|
|
centerVelocity = admin.get<DynamicBodyComponent>(t).velocity_tpt;
|
|
}
|
|
}
|
|
intent = MovementIntentComponent{true, center, radius, centerVelocity};
|
|
});
|
|
|
|
// 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;
|
|
}
|
|
});
|
|
}
|