Implement unit action queueing and busy state validation
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <QString>
|
||||
#include <QVector3D>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <utility>
|
||||
#include "unitconfig.h"
|
||||
#include "terrain.h"
|
||||
@@ -16,6 +17,31 @@ enum class UnitState {
|
||||
Building
|
||||
};
|
||||
|
||||
enum class UnitActionType {
|
||||
None,
|
||||
Move,
|
||||
BuildImprovement,
|
||||
BuildCity,
|
||||
Attack
|
||||
};
|
||||
|
||||
struct UnitAction {
|
||||
UnitActionType type = UnitActionType::None;
|
||||
int targetX = -1;
|
||||
int targetY = -1;
|
||||
int targetUnitId = -1;
|
||||
QString extraData = ""; // e.g. improvementId ("farm")
|
||||
|
||||
UnitAction() = default;
|
||||
UnitAction(UnitActionType t) : type(t) {}
|
||||
UnitAction(UnitActionType t, int x, int y, const QString &data = "")
|
||||
: type(t), targetX(x), targetY(y), extraData(data) {}
|
||||
UnitAction(UnitActionType t, int targetId)
|
||||
: type(t), targetUnitId(targetId) {}
|
||||
|
||||
bool isValid() const { return type != UnitActionType::None; }
|
||||
};
|
||||
|
||||
class Unit {
|
||||
public:
|
||||
Unit(int uId, int pId, const UnitConfig &cfg, int startX, int startY, float groundZ);
|
||||
@@ -38,6 +64,19 @@ public:
|
||||
int targetTileX;
|
||||
int targetTileY;
|
||||
|
||||
// Action Queue
|
||||
std::deque<UnitAction> actionQueue;
|
||||
int maxQueueSize = 5;
|
||||
|
||||
bool queueAction(const UnitAction &action);
|
||||
void clearActionQueue();
|
||||
bool hasQueuedAction() const { return !actionQueue.empty(); }
|
||||
bool hasQueuedActionType(UnitActionType type) const;
|
||||
void removeQueuedActionsOfType(UnitActionType type);
|
||||
UnitAction popNextQueuedAction();
|
||||
const UnitAction* peekNextQueuedAction() const;
|
||||
QString getQueuedActionSummary() const;
|
||||
|
||||
// Attack cooldown / flash
|
||||
float attackFlashTimer;
|
||||
|
||||
@@ -54,6 +93,7 @@ public:
|
||||
void startBuilding(const QString &impId, float duration);
|
||||
void cancelBuilding();
|
||||
bool isBuilding() const { return state == UnitState::Building; }
|
||||
bool isBusy() const { return state == UnitState::Building; }
|
||||
|
||||
bool canAttack(const Unit &other) const;
|
||||
void performAttack(Unit &target);
|
||||
|
||||
Reference in New Issue
Block a user