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 <cmath>
#include <optional> #include <optional>
#include <QKeyEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QPainter> #include <QPainter>
#include <QPoint> #include <QPoint>
#include "ArenaSimulation.h" #include "ArenaSimulation.h"
#include "AttackBehavior.h"
#include "Building.h" #include "Building.h"
#include "BuildingSystem.h" #include "BuildingSystem.h"
#include "EntityHitTest.h" #include "EntityHitTest.h"
@@ -20,6 +22,7 @@
#include "HealthComponent.h" #include "HealthComponent.h"
#include "PositionComponent.h" #include "PositionComponent.h"
#include "ScrapSystem.h" #include "ScrapSystem.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h" #include "ShipIdentityComponent.h"
#include "StationBodyComponent.h" #include "StationBodyComponent.h"
@@ -38,6 +41,7 @@ ArenaView::ArenaView(ArenaSimulation* sim, const VisualsConfig* visuals,
, m_prevNonZeroSpeed(1.0) , m_prevNonZeroSpeed(1.0)
, m_rng(std::random_device{}()) , m_rng(std::random_device{}())
, m_finishedEmitted(false) , m_finishedEmitted(false)
, m_debugDraw(false)
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
@@ -167,6 +171,11 @@ void ArenaView::paintGL()
drawBuildings(painter); drawBuildings(painter);
drawStations(painter); drawStations(painter);
drawScrap(painter); drawScrap(painter);
if (m_debugDraw)
{
drawDebugSensorRanges(painter);
drawDebugTargetLines(painter);
}
drawShips(painter); drawShips(painter);
drawBeams(painter); drawBeams(painter);
} }
@@ -249,6 +258,16 @@ void ArenaView::mousePressEvent(QMouseEvent* event)
QOpenGLWidget::mousePressEvent(event); QOpenGLWidget::mousePressEvent(event);
} }
void ArenaView::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_M)
{
m_debugDraw = !m_debugDraw;
return;
}
QOpenGLWidget::keyPressEvent(event);
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Rendering // 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) void ArenaView::drawBeams(QPainter& painter)
{ {
painter.setPen(QPen(m_visuals->beams.color, m_visuals->beams.widthPx)); painter.setPen(QPen(m_visuals->beams.color, m_visuals->beams.widthPx));

View File

@@ -39,6 +39,7 @@ public:
protected: protected:
void paintGL() override; void paintGL() override;
void mousePressEvent(QMouseEvent* event) override; void mousePressEvent(QMouseEvent* event) override;
void keyPressEvent(QKeyEvent* event) override;
private slots: private slots:
void onFrame(); void onFrame();
@@ -51,6 +52,8 @@ private:
void drawStations(QPainter& painter); void drawStations(QPainter& painter);
void drawScrap(QPainter& painter); void drawScrap(QPainter& painter);
void drawShips(QPainter& painter); void drawShips(QPainter& painter);
void drawDebugSensorRanges(QPainter& painter);
void drawDebugTargetLines(QPainter& painter);
void drawBeams(QPainter& painter); void drawBeams(QPainter& painter);
float tilePx() const; float tilePx() const;
@@ -86,4 +89,6 @@ private:
bool m_finishedEmitted; bool m_finishedEmitted;
std::optional<entt::entity> m_selectedEntity; std::optional<entt::entity> m_selectedEntity;
bool m_debugDraw;
}; };