update
This commit is contained in:
@@ -6,8 +6,10 @@
|
||||
Unit::Unit(int uId, int pId, const UnitConfig &cfg, int startX, int startY, float groundZ)
|
||||
: id(uId), playerId(pId), config(cfg), tileX(startX), tileY(startY),
|
||||
health((float)cfg.health), maxHealth((float)cfg.health),
|
||||
buildCharges(cfg.buildCharges),
|
||||
state(UnitState::Idle), pathIndex(0), moveProgress(0.0f),
|
||||
targetTileX(startX), targetTileY(startY), attackFlashTimer(0.0f)
|
||||
targetTileX(startX), targetTileY(startY), attackFlashTimer(0.0f),
|
||||
buildProgress(0.0f), buildDuration(0.0f), buildingImprovementId("")
|
||||
{
|
||||
QVector2D w = Pathfinder::hexToWorld(startX, startY);
|
||||
worldPos = QVector3D(w.x(), w.y(), groundZ);
|
||||
@@ -34,6 +36,10 @@ float Unit::getTerrainHeight(float worldX, float worldY, Matrix2D<TerrainPoint>
|
||||
|
||||
void Unit::setPath(const std::vector<std::pair<int, int>> &newPath)
|
||||
{
|
||||
if (state == UnitState::Building) {
|
||||
cancelBuilding();
|
||||
}
|
||||
|
||||
path = newPath;
|
||||
pathIndex = 0;
|
||||
moveProgress = 0.0f;
|
||||
@@ -55,6 +61,27 @@ void Unit::stopMovement()
|
||||
state = UnitState::Idle;
|
||||
}
|
||||
|
||||
void Unit::startBuilding(const QString &impId, float duration)
|
||||
{
|
||||
path.clear();
|
||||
pathIndex = 0;
|
||||
moveProgress = 0.0f;
|
||||
state = UnitState::Building;
|
||||
buildingImprovementId = impId;
|
||||
buildDuration = std::max(0.1f, duration);
|
||||
buildProgress = 0.0f;
|
||||
}
|
||||
|
||||
void Unit::cancelBuilding()
|
||||
{
|
||||
if (state == UnitState::Building) {
|
||||
state = UnitState::Idle;
|
||||
buildProgress = 0.0f;
|
||||
buildDuration = 0.0f;
|
||||
buildingImprovementId = "";
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::update(float deltaTime, float tickDuration, float simSpeedMultiplier,
|
||||
Matrix2D<TerrainPoint> *mapPoints, int numPoints05)
|
||||
{
|
||||
@@ -62,6 +89,13 @@ void Unit::update(float deltaTime, float tickDuration, float simSpeedMultiplier,
|
||||
attackFlashTimer = std::max(0.0f, attackFlashTimer - deltaTime);
|
||||
}
|
||||
|
||||
if (state == UnitState::Building) {
|
||||
if (buildDuration > 0.001f) {
|
||||
buildProgress += (deltaTime * simSpeedMultiplier) / buildDuration;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (state != UnitState::Moving || path.size() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user