bugfix for smoother animation sequences and fix progress bound animations

This commit is contained in:
Mark Vejvoda 2013-06-03 04:28:53 +00:00
parent 01e7c28992
commit 335eb3ac4a
2 changed files with 43 additions and 26 deletions

View File

@ -388,6 +388,9 @@ void UnitAttackBoostEffectOriginator::saveGame(XmlNode *rootNode) {
// class Unit // class Unit
// ===================================================== // =====================================================
const float Unit::ANIMATION_SPEED_MULTIPLIER = 100000.f;
const float Unit::PROGRESS_SPEED_MULTIPLIER = 100.f;
const int Unit::speedDivider= 100; const int Unit::speedDivider= 100;
const int Unit::maxDeadCount= 1000; //time in until the corpse disapears - should be about 40 seconds const int Unit::maxDeadCount= 1000; //time in until the corpse disapears - should be about 40 seconds
const int Unit::invalidId= -1; const int Unit::invalidId= -1;
@ -1369,7 +1372,7 @@ Vec3f Unit::getCurrVectorFlat() const{
} }
float Unit::getProgressAsFloat() const { float Unit::getProgressAsFloat() const {
float result = (static_cast<float>(progress) / 100.f); float result = (static_cast<float>(progress) / PROGRESS_SPEED_MULTIPLIER);
result = truncateDecimal<float>(result); result = truncateDecimal<float>(result);
return result; return result;
} }
@ -1885,9 +1888,9 @@ const CommandType *Unit::computeCommandType(const Vec2i &pos, const Unit *target
} }
int Unit::getUpdateProgress() { int Unit::getUpdateProgress() {
if(progress > 100) { if(progress > PROGRESS_SPEED_MULTIPLIER) {
char szBuf[8096]=""; char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > 100, progress = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,progress); snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: progress > %f, progress = [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,PROGRESS_SPEED_MULTIPLIER,progress);
throw megaglest_runtime_error(szBuf); throw megaglest_runtime_error(szBuf);
} }
@ -1908,7 +1911,7 @@ int Unit::getUpdateProgress() {
//speed modifier //speed modifier
int diagonalFactor = getDiagonalFactor(); int diagonalFactor = getDiagonalFactor();
int heightFactor = getHeightFactor(); int heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER);
//update progresses //update progresses
const Game *game = Renderer::getInstance().getGame(); const Game *game = Renderer::getInstance().getGame();
@ -1931,7 +1934,7 @@ bool Unit::needToUpdate() {
bool return_value = false; bool return_value = false;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
int newProgress = getUpdateProgress(); int newProgress = getUpdateProgress();
if(newProgress >= 100) { if(newProgress >= PROGRESS_SPEED_MULTIPLIER) {
return_value = true; return_value = true;
} }
} }
@ -1940,7 +1943,7 @@ bool Unit::needToUpdate() {
int Unit::getDiagonalFactor() { int Unit::getDiagonalFactor() {
//speed modifier //speed modifier
int diagonalFactor= 100; int diagonalFactor= PROGRESS_SPEED_MULTIPLIER;
if(currSkill->getClass() == scMove) { if(currSkill->getClass() == scMove) {
//if moving in diagonal move slower //if moving in diagonal move slower
Vec2i dest= pos - lastPos; Vec2i dest= pos - lastPos;
@ -1951,8 +1954,8 @@ int Unit::getDiagonalFactor() {
return diagonalFactor; return diagonalFactor;
} }
int Unit::getHeightFactor() { int Unit::getHeightFactor(float speedMultiplier) {
int heightFactor = 100; int heightFactor = speedMultiplier;
if(currSkill->getClass() == scMove) { if(currSkill->getClass() == scMove) {
//if moving to an higher cell move slower else move faster //if moving to an higher cell move slower else move faster
Cell *unitCell = map->getCell(pos); Cell *unitCell = map->getCell(pos);
@ -1965,16 +1968,16 @@ int Unit::getHeightFactor() {
throw megaglest_runtime_error("targetCell == NULL"); throw megaglest_runtime_error("targetCell == NULL");
} }
int heightDiff= (truncateDecimal<float>(unitCell->getHeight() * 100.f,2) - int heightDiff= (truncateDecimal<float>(unitCell->getHeight() * speedMultiplier,2) -
truncateDecimal<float>(targetCell->getHeight() * 100.f,2)); truncateDecimal<float>(targetCell->getHeight() * speedMultiplier,2));
heightFactor= clamp(100 + heightDiff / 500, 20, 500); heightFactor= clamp(speedMultiplier + heightDiff / (5.f * speedMultiplier), 0.2f * speedMultiplier, 5.f * speedMultiplier);
} }
return heightFactor; return heightFactor;
} }
int Unit::getSpeedDenominator(int updateFPS) { int Unit::getSpeedDenominator(int updateFPS) {
int speedDenominator = (speedDivider * updateFPS) * 100; int speedDenominator = (speedDivider * updateFPS) * PROGRESS_SPEED_MULTIPLIER;
return speedDenominator; return speedDenominator;
} }
int Unit::getUpdatedProgress(int currentProgress, int updateFPS, int speed, int Unit::getUpdatedProgress(int currentProgress, int updateFPS, int speed,
@ -2221,7 +2224,7 @@ void Unit::updateAttackBoostProgress(const Game* game) {
} }
bool Unit::update() { bool Unit::update() {
assert(progress <= 100); assert(progress <= PROGRESS_SPEED_MULTIPLIER);
//highlight //highlight
if(highlight > 0.f) { if(highlight > 0.f) {
@ -2244,7 +2247,7 @@ bool Unit::update() {
//speed modifier //speed modifier
int diagonalFactor = getDiagonalFactor(); int diagonalFactor = getDiagonalFactor();
int heightFactor = getHeightFactor(); int heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER);
//update progresses //update progresses
this->lastAnimProgress= this->animProgress; this->lastAnimProgress= this->animProgress;
@ -2270,17 +2273,29 @@ bool Unit::update() {
if(currSkill->getClass() == scMorph) { if(currSkill->getClass() == scMorph) {
targetProgress = this->getProgressRatio(); targetProgress = this->getProgressRatio();
} }
if(getAnimProgressAsFloat() < targetProgress) {
float diff = targetProgress - getAnimProgressAsFloat(); float targetProgressIntValue = targetProgress * ANIMATION_SPEED_MULTIPLIER;
int progressIncrease = this->animProgress + static_cast<int>(diff * 100.f) / (GameConstants::updateFps); if(this->animProgress < targetProgressIntValue) {
float diff = targetProgressIntValue - this->animProgress;
float progressIncrease = static_cast<float>(this->animProgress) + diff / static_cast<float>(GameConstants::updateFps);
// Ensure we increment at least a value of 1 of the action will be stuck infinitely // Ensure we increment at least a value of 1 of the action will be stuck infinitely
if(diff > 0 && GameConstants::updateFps > 0 && progressIncrease == 0) { if(diff > 0.f && GameConstants::updateFps > 0 && progressIncrease == 0.f) {
progressIncrease = 1; progressIncrease = 1.f;
} }
//if(currSkill->getClass() == scBeBuilt) {
// printf("targetProgress: %.10f this->animProgress: %d diff: %.10f GameConstants::updateFps: %d progressIncrease: %.10f\n",targetProgress,this->animProgress,diff,GameConstants::updateFps,progressIncrease);
//}
this->animProgress = progressIncrease; this->animProgress = progressIncrease;
//if(currSkill->getClass() == scBeBuilt) {
// printf("Unit build progress: %d anim: %d\n",progress,this->animProgress);
//}
} }
} }
else { else {
int heightFactor = getHeightFactor(ANIMATION_SPEED_MULTIPLIER);
int speedDenominator = speedDivider * int speedDenominator = speedDivider *
game->getWorld()->getUpdateFps(this->getFactionIndex()); game->getWorld()->getUpdateFps(this->getFactionIndex());
int progressIncrease = (currSkill->getAnimSpeed() * heightFactor) / speedDenominator; int progressIncrease = (currSkill->getAnimSpeed() * heightFactor) / speedDenominator;
@ -2371,9 +2386,9 @@ bool Unit::update() {
} }
//checks //checks
if(this->animProgress > 100) { if(this->animProgress > ANIMATION_SPEED_MULTIPLIER) {
bool canCycle = currSkill->CanCycleNextRandomAnimation(&animationRandomCycleCount); bool canCycle = currSkill->CanCycleNextRandomAnimation(&animationRandomCycleCount);
this->animProgress = currSkill->getClass() == scDie ? 100 : 0; this->animProgress = currSkill->getClass() == scDie ? ANIMATION_SPEED_MULTIPLIER : 0;
if(canCycle == true) { if(canCycle == true) {
this->lastModelIndexForCurrSkillType = -1; this->lastModelIndexForCurrSkillType = -1;
} }
@ -2381,14 +2396,14 @@ bool Unit::update() {
bool return_value = false; bool return_value = false;
//checks //checks
if(progress >= 100) { if(progress >= PROGRESS_SPEED_MULTIPLIER) {
lastRotation= targetRotation; lastRotation= targetRotation;
if(currSkill->getClass() != scDie) { if(currSkill->getClass() != scDie) {
progress = 0; progress = 0;
return_value = true; return_value = true;
} }
else { else {
progress= 100; progress= PROGRESS_SPEED_MULTIPLIER;
deadCount++; deadCount++;
if(deadCount >= maxDeadCount) { if(deadCount >= maxDeadCount) {
toBeUndertaken= true; toBeUndertaken= true;

View File

@ -334,6 +334,8 @@ public:
static const int speedDivider; static const int speedDivider;
static const int maxDeadCount; static const int maxDeadCount;
static const int invalidId; static const int invalidId;
static const float ANIMATION_SPEED_MULTIPLIER;
static const float PROGRESS_SPEED_MULTIPLIER;
#ifdef LEAK_CHECK_UNITS #ifdef LEAK_CHECK_UNITS
static std::map<UnitPathInterface *,int> mapMemoryList2; static std::map<UnitPathInterface *,int> mapMemoryList2;
@ -506,8 +508,8 @@ public:
//inline int getLastAnimProgress() const {return lastAnimProgress;} //inline int getLastAnimProgress() const {return lastAnimProgress;}
//inline int getAnimProgress() const {return animProgress;} //inline int getAnimProgress() const {return animProgress;}
inline float getLastAnimProgressAsFloat() const {return static_cast<float>(lastAnimProgress) / 100.f;} inline float getLastAnimProgressAsFloat() const {return static_cast<float>(lastAnimProgress) / ANIMATION_SPEED_MULTIPLIER;}
inline float getAnimProgressAsFloat() const {return static_cast<float>(animProgress) / 100.f;} inline float getAnimProgressAsFloat() const {return static_cast<float>(animProgress) / ANIMATION_SPEED_MULTIPLIER;}
inline float getHightlight() const {return highlight;} inline float getHightlight() const {return highlight;}
inline int getProgress2() const {return progress2;} inline int getProgress2() const {return progress2;}
@ -734,7 +736,7 @@ public:
float getProgressAsFloat() const; float getProgressAsFloat() const;
int getUpdateProgress(); int getUpdateProgress();
int getDiagonalFactor(); int getDiagonalFactor();
int getHeightFactor(); int getHeightFactor(float speedMultiplier=PROGRESS_SPEED_MULTIPLIER);
int getSpeedDenominator(int updateFPS); int getSpeedDenominator(int updateFPS);
bool isChangedActiveCommand() const { return changedActiveCommand; } bool isChangedActiveCommand() const { return changedActiveCommand; }