draw debug lines to repair and salvage behavior targets

This commit is contained in:
2026-06-17 21:41:35 +02:00
parent 7f4ea93a70
commit 41c8ed2938
2 changed files with 89 additions and 19 deletions

View File

@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
#include <functional>
#include <optional>
#include <QKeyEvent>
@@ -21,6 +22,8 @@
#include "GameSpeedChangedEvent.h"
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "RepairBehavior.h"
#include "SalvageScrapBehavior.h"
#include "ScrapSystem.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h"
@@ -460,6 +463,25 @@ void ArenaView::drawDebugSensorRanges(QPainter& painter)
void ArenaView::drawDebugTargetLines(QPainter& painter)
{
// Draw a thin translucent line from a ship to a target, colored by the ship's
// team to match the per-side HQ/station colors used elsewhere in the arena
// (team 1 player, team 2 enemy). Shared by the attack, repair and salvage lines.
const std::function<void(bool, const QVector2D&, const QVector2D&)> drawTargetLine =
[&](bool isEnemy, const QVector2D& from, const QVector2D& to)
{
const BuildingType visType = isEnemy
? BuildingType::EnemyDefenceStation
: BuildingType::PlayerDefenceStation;
const std::map<BuildingType, BuildingVisuals>::const_iterator it =
m_visuals->buildings.find(visType);
if (it == m_visuals->buildings.end()) { return; }
QColor lineColor = it->second.fill;
lineColor.setAlpha(128);
painter.setPen(QPen(lineColor, 1));
painter.drawLine(worldToWidget(from), worldToWidget(to));
};
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent,
FactionComponent, AttackBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
@@ -472,19 +494,33 @@ void ArenaView::drawDebugTargetLines(QPainter& painter)
entityPosition(*attack.currentTarget);
if (!targetPos.has_value()) { return; }
// Color the line by the ship's team, matching the per-side HQ/station
// colors used elsewhere in the arena (team 1 player, team 2 enemy).
const BuildingType visType = fac.isEnemy
? BuildingType::EnemyDefenceStation
: BuildingType::PlayerDefenceStation;
const std::map<BuildingType, BuildingVisuals>::const_iterator it =
m_visuals->buildings.find(visType);
if (it == m_visuals->buildings.end()) { return; }
drawTargetLine(fac.isEnemy, pos.value, *targetPos);
});
QColor lineColor = it->second.fill;
lineColor.setAlpha(128);
painter.setPen(QPen(lineColor, 1));
painter.drawLine(worldToWidget(pos.value), worldToWidget(*targetPos));
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent,
FactionComponent, RepairBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& fac,
const RepairBehavior& repair)
{
if (!repair.currentTarget.has_value()) { return; }
const std::optional<QVector2D> targetPos =
entityPosition(*repair.currentTarget);
if (!targetPos.has_value()) { return; }
drawTargetLine(fac.isEnemy, pos.value, *targetPos);
});
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent,
FactionComponent, SalvageScrapBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& fac,
const SalvageScrapBehavior& salvage)
{
if (!salvage.scrapTarget.has_value()) { return; }
drawTargetLine(fac.isEnemy, pos.value, *salvage.scrapTarget);
});
}

View File

@@ -4,6 +4,7 @@
#include <cctype>
#include <climits>
#include <cmath>
#include <functional>
#include <map>
#include <string>
@@ -31,6 +32,8 @@
#include "GameOverEvent.h"
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "RepairBehavior.h"
#include "SalvageScrapBehavior.h"
#include "ScrapSystem.h"
#include "SelectionChangedEvent.h"
#include "SensorRangeComponent.h"
@@ -927,6 +930,22 @@ void GameWorldView::drawDebugSensorRanges(QPainter& painter)
void GameWorldView::drawDebugTargetLines(QPainter& painter)
{
// Draw a thin translucent line from a ship to a target, colored by the ship's
// own schematic fill. Shared by the attack, repair and salvage target lines.
const std::function<void(const std::string&, const QVector2D&, const QVector2D&)>
drawTargetLine = [&](const std::string& schematicId, const QVector2D& from,
const QVector2D& to)
{
const std::map<std::string, ShipVisuals>::const_iterator it =
m_visuals->ships.find(schematicId);
if (it == m_visuals->ships.end()) { return; }
QColor lineColor = it->second.fill;
lineColor.setAlpha(128);
painter.setPen(QPen(lineColor, 1));
painter.drawLine(worldToWidget(from), worldToWidget(to));
};
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent, AttackBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& si,
const PositionComponent& pos, const AttackBehavior& attack)
@@ -937,14 +956,29 @@ void GameWorldView::drawDebugTargetLines(QPainter& painter)
entityPosition(*attack.currentTarget);
if (!targetPos.has_value()) { return; }
const std::map<std::string, ShipVisuals>::const_iterator it =
m_visuals->ships.find(si.schematicId);
if (it == m_visuals->ships.end()) { return; }
drawTargetLine(si.schematicId, pos.value, *targetPos);
});
QColor lineColor = it->second.fill;
lineColor.setAlpha(128);
painter.setPen(QPen(lineColor, 1));
painter.drawLine(worldToWidget(pos.value), worldToWidget(*targetPos));
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent, RepairBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& si,
const PositionComponent& pos, const RepairBehavior& repair)
{
if (!repair.currentTarget.has_value()) { return; }
const std::optional<QVector2D> targetPos =
entityPosition(*repair.currentTarget);
if (!targetPos.has_value()) { return; }
drawTargetLine(si.schematicId, pos.value, *targetPos);
});
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent, SalvageScrapBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& si,
const PositionComponent& pos, const SalvageScrapBehavior& salvage)
{
if (!salvage.scrapTarget.has_value()) { return; }
drawTargetLine(si.schematicId, pos.value, *salvage.scrapTarget);
});
}