Implement unit action queueing and busy state validation
This commit is contained in:
@@ -35,12 +35,72 @@ float Unit::getTerrainHeight(float worldX, float worldY, Matrix2D<TerrainPoint>
|
||||
return (h / 255.0f) * 3.5f + 1.0f;
|
||||
}
|
||||
|
||||
bool Unit::queueAction(const UnitAction &action)
|
||||
{
|
||||
if ((int)actionQueue.size() >= maxQueueSize) {
|
||||
return false;
|
||||
}
|
||||
actionQueue.push_back(action);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Unit::clearActionQueue()
|
||||
{
|
||||
actionQueue.clear();
|
||||
}
|
||||
|
||||
bool Unit::hasQueuedActionType(UnitActionType type) const
|
||||
{
|
||||
for (const auto &act : actionQueue) {
|
||||
if (act.type == type) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Unit::removeQueuedActionsOfType(UnitActionType type)
|
||||
{
|
||||
for (auto it = actionQueue.begin(); it != actionQueue.end(); ) {
|
||||
if (it->type == type) {
|
||||
it = actionQueue.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UnitAction Unit::popNextQueuedAction()
|
||||
{
|
||||
if (actionQueue.empty()) return UnitAction();
|
||||
UnitAction act = actionQueue.front();
|
||||
actionQueue.pop_front();
|
||||
return act;
|
||||
}
|
||||
|
||||
const UnitAction* Unit::peekNextQueuedAction() const
|
||||
{
|
||||
if (actionQueue.empty()) return nullptr;
|
||||
return &actionQueue.front();
|
||||
}
|
||||
|
||||
QString Unit::getQueuedActionSummary() const
|
||||
{
|
||||
if (actionQueue.empty()) return "";
|
||||
switch (actionQueue.front().type) {
|
||||
case UnitActionType::Move:
|
||||
return "Move";
|
||||
case UnitActionType::BuildImprovement:
|
||||
return "Build Farm";
|
||||
case UnitActionType::BuildCity:
|
||||
return "Found City";
|
||||
case UnitActionType::Attack:
|
||||
return "Attack";
|
||||
default:
|
||||
return "Action";
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::setPath(const std::vector<std::pair<int, int>> &newPath)
|
||||
{
|
||||
if (state == UnitState::Building) {
|
||||
cancelBuilding();
|
||||
}
|
||||
|
||||
path = newPath;
|
||||
pathIndex = 0;
|
||||
moveProgress = 0.0f;
|
||||
@@ -60,6 +120,7 @@ void Unit::stopMovement()
|
||||
pathIndex = 0;
|
||||
moveProgress = 0.0f;
|
||||
state = UnitState::Idle;
|
||||
clearActionQueue();
|
||||
}
|
||||
|
||||
void Unit::startBuilding(const QString &impId, float duration)
|
||||
|
||||
Reference in New Issue
Block a user