174 lines
4.8 KiB
C++
174 lines
4.8 KiB
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <random>
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
#include <QElapsedTimer>
|
|
#include <QOpenGLWidget>
|
|
#include <QPoint>
|
|
#include <QRectF>
|
|
#include <QTimer>
|
|
#include <QVector2D>
|
|
|
|
#include "Blueprint.h"
|
|
#include "SchematicDropEvent.h"
|
|
#include "BuildingType.h"
|
|
#include "EntityId.h"
|
|
#include "FireEvent.h"
|
|
#include "GameConfig.h"
|
|
#include "Rotation.h"
|
|
#include "Tick.h"
|
|
#include "TickDriver.h"
|
|
#include "VisualsConfig.h"
|
|
|
|
class Simulation;
|
|
class QPainter;
|
|
|
|
struct QPointCompare
|
|
{
|
|
bool operator()(const QPoint& a, const QPoint& b) const
|
|
{
|
|
if (a.x() != b.x()) { return a.x() < b.x(); }
|
|
return a.y() < b.y();
|
|
}
|
|
};
|
|
|
|
class GameWorldView : public QOpenGLWidget
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
GameWorldView(Simulation* sim, const GameConfig* config,
|
|
const VisualsConfig* visuals, QWidget* parent = nullptr);
|
|
|
|
signals:
|
|
void selectionChanged(const std::vector<EntityId>& ids);
|
|
void stateUpdated(Tick tick, int blocks, double speed);
|
|
void gameOver();
|
|
void builderModeExited();
|
|
void blueprintModeExited();
|
|
void escapeMenuRequested();
|
|
void demolishModeChanged(bool active);
|
|
|
|
public:
|
|
double gameSpeed() const;
|
|
|
|
public slots:
|
|
void enterBuilderMode(BuildingType type);
|
|
void exitBuilderMode();
|
|
void enterBlueprintMode(Blueprint blueprint);
|
|
void exitBlueprintMode();
|
|
void toggleDemolishMode();
|
|
void setGameSpeed(double multiplier);
|
|
void resetForNewGame();
|
|
|
|
protected:
|
|
void initializeGL() override;
|
|
void paintGL() override;
|
|
void keyPressEvent(QKeyEvent* event) override;
|
|
void keyReleaseEvent(QKeyEvent* event) override;
|
|
void mousePressEvent(QMouseEvent* event) override;
|
|
void mouseMoveEvent(QMouseEvent* event) override;
|
|
void mouseReleaseEvent(QMouseEvent* event) override;
|
|
|
|
private slots:
|
|
void onFrame();
|
|
|
|
private:
|
|
void drawTiles(QPainter& painter);
|
|
void drawBuildings(QPainter& painter);
|
|
void drawBeltItems(QPainter& painter);
|
|
void drawScrap(QPainter& painter);
|
|
void drawShips(QPainter& painter);
|
|
void drawDebugSensorRanges(QPainter& painter);
|
|
void drawBeams(QPainter& painter);
|
|
void drawOverlays(QPainter& painter);
|
|
void drawScreenSpace(QPainter& painter);
|
|
|
|
float tilePx() const;
|
|
float viewportWidthTiles() const;
|
|
QPointF worldToWidget(QVector2D worldPos) const;
|
|
QPointF tileToWidget(QPoint tile) const;
|
|
QPoint widgetToTile(QPoint widgetPt) const;
|
|
QRectF tileRect(QPoint tile) const;
|
|
QRect viewportRect() const;
|
|
|
|
float asteroidLeftEdge() const;
|
|
float enemyStationRightEdge() const;
|
|
void clampScroll();
|
|
|
|
bool isValidPlacement(BuildingType type, QPoint anchor, Rotation rot) const;
|
|
const BuildingDef* findBuildingDef(BuildingType type) const;
|
|
EntityId buildingAtTile(QPoint tile) const;
|
|
EntityId siteAtTile(QPoint tile) const;
|
|
|
|
void drawPortGlyph(QPainter& painter, QPoint bodyTile,
|
|
Rotation direction, const QColor& color);
|
|
|
|
void placeBlueprintAtTile(QPoint center);
|
|
|
|
std::optional<QVector2D> entityPosition(EntityId id) const;
|
|
void stepSpeed(int delta);
|
|
void placeAtTile(QPoint tile);
|
|
|
|
struct ActiveBeam
|
|
{
|
|
FireEvent event;
|
|
qint64 emittedWallMs;
|
|
QVector2D targetOffset;
|
|
};
|
|
|
|
struct ToastEntry
|
|
{
|
|
QString text;
|
|
qint64 createdWallMs;
|
|
};
|
|
|
|
static constexpr qint64 kBeamLifetimeMs = 300;
|
|
static constexpr qint64 kToastLifetimeMs = 4000;
|
|
static constexpr qint64 kToastFadeStartMs = 3500;
|
|
static constexpr float kScrollSpeedTilesPerSec = 10.0f;
|
|
|
|
Simulation* m_sim;
|
|
const GameConfig* m_config;
|
|
const VisualsConfig* m_visuals;
|
|
|
|
TickDriver m_tickDriver;
|
|
QElapsedTimer m_frameTimer;
|
|
qint64 m_wallMs;
|
|
std::mt19937 m_rng;
|
|
double m_gameSpeedMultiplier;
|
|
double m_prevNonZeroSpeed;
|
|
float m_scrollXTiles;
|
|
|
|
QTimer* m_renderTimer;
|
|
|
|
std::vector<ActiveBeam> m_activeBeams;
|
|
std::vector<ToastEntry> m_toasts;
|
|
|
|
std::optional<BuildingType> m_builderType;
|
|
Rotation m_ghostRotation;
|
|
QPoint m_ghostTile;
|
|
bool m_ghostValid;
|
|
std::set<QPoint, QPointCompare> m_beltDragTiles;
|
|
bool m_dragging;
|
|
|
|
std::optional<Blueprint> m_blueprintMode;
|
|
QPoint m_blueprintGhostTile;
|
|
|
|
bool m_demolishMode;
|
|
EntityId m_demolishHoverId;
|
|
bool m_debugDraw;
|
|
|
|
std::vector<EntityId> m_selectedIds;
|
|
bool m_boxSelecting;
|
|
QPoint m_boxStartTile;
|
|
QPoint m_boxCurrentTile;
|
|
|
|
bool m_scrollLeft;
|
|
bool m_scrollRight;
|
|
bool m_gameOverShown;
|
|
};
|