add debug draw mode for balancing target

This commit is contained in:
2026-06-16 21:39:23 +02:00
parent bd2391876c
commit 74615f5293
2 changed files with 73 additions and 0 deletions

View File

@@ -4,11 +4,13 @@
#include <cmath>
#include <optional>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPoint>
#include "ArenaSimulation.h"
#include "AttackBehavior.h"
#include "Building.h"
#include "BuildingSystem.h"
#include "EntityHitTest.h"
@@ -20,6 +22,7 @@
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "ScrapSystem.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h"
#include "StationBodyComponent.h"
@@ -38,6 +41,7 @@ ArenaView::ArenaView(ArenaSimulation* sim, const VisualsConfig* visuals,
, m_prevNonZeroSpeed(1.0)
, m_rng(std::random_device{}())
, m_finishedEmitted(false)
, m_debugDraw(false)
{
setFocusPolicy(Qt::StrongFocus);
@@ -167,6 +171,11 @@ void ArenaView::paintGL()
drawBuildings(painter);
drawStations(painter);
drawScrap(painter);
if (m_debugDraw)
{
drawDebugSensorRanges(painter);
drawDebugTargetLines(painter);
}
drawShips(painter);
drawBeams(painter);
}
@@ -249,6 +258,16 @@ void ArenaView::mousePressEvent(QMouseEvent* event)
QOpenGLWidget::mousePressEvent(event);
}
void ArenaView::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_M)
{
m_debugDraw = !m_debugDraw;
return;
}
QOpenGLWidget::keyPressEvent(event);
}
// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------
@@ -418,6 +437,55 @@ void ArenaView::drawShips(QPainter& painter)
});
}
void ArenaView::drawDebugSensorRanges(QPainter& painter)
{
painter.setBrush(Qt::NoBrush);
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent, SensorRangeComponent>(
[&](entt::entity /*e*/, const ShipIdentityComponent& si,
const PositionComponent& pos, const SensorRangeComponent& sensor)
{
const std::map<std::string, ShipVisuals>::const_iterator it =
m_visuals->ships.find(si.schematicId);
if (it == m_visuals->ships.end()) { return; }
const QPointF center = worldToWidget(pos.value);
const qreal radiusPx = static_cast<qreal>(sensor.value_tiles)
* static_cast<qreal>(tilePx());
painter.setPen(QPen(it->second.outline, 1));
painter.drawEllipse(center, radiusPx, radiusPx);
});
}
void ArenaView::drawDebugTargetLines(QPainter& painter)
{
m_sim->admin().forEach<ShipIdentityComponent, PositionComponent,
FactionComponent, AttackBehavior>(
[&](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& fac,
const AttackBehavior& attack)
{
if (!attack.currentTarget.has_value()) { return; }
const std::optional<QVector2D> targetPos =
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; }
QColor lineColor = it->second.fill;
lineColor.setAlpha(128);
painter.setPen(QPen(lineColor, 1));
painter.drawLine(worldToWidget(pos.value), worldToWidget(*targetPos));
});
}
void ArenaView::drawBeams(QPainter& painter)
{
painter.setPen(QPen(m_visuals->beams.color, m_visuals->beams.widthPx));