From 14b6f195e61f9a35bdc45371802f01fe9c504abe Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 25 Jun 2011 15:30:18 +0000 Subject: [PATCH] - for multiple animations, now can specify a 'preferred' animation based on the units hp using: --- source/glest_game/type_instances/unit.cpp | 4 +-- source/glest_game/types/skill_type.cpp | 31 ++++++++++++++++++++--- source/glest_game/types/skill_type.h | 15 ++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 418948e2..9196a7f6 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -689,7 +689,7 @@ Model *Unit::getCurrentModelPtr() { throw runtime_error(szBuf); } - return currSkill->getAnimation(animProgress,&lastModelIndexForCurrSkillType); + return currSkill->getAnimation(animProgress,this,&lastModelIndexForCurrSkillType); } const Model *Unit::getCurrentModel() { @@ -699,7 +699,7 @@ const Model *Unit::getCurrentModel() { throw runtime_error(szBuf); } - return currSkill->getAnimation(animProgress,&lastModelIndexForCurrSkillType); + return currSkill->getAnimation(animProgress,this,&lastModelIndexForCurrSkillType); } Vec3f Unit::getCurrVector() const{ diff --git a/source/glest_game/types/skill_type.cpp b/source/glest_game/types/skill_type.cpp index 2ea3a42b..58b86a1c 100644 --- a/source/glest_game/types/skill_type.cpp +++ b/source/glest_game/types/skill_type.cpp @@ -126,6 +126,13 @@ void SkillType::load(const XmlNode *sn, const string &dir, const TechTree *tt, animations.push_back(animation); //printf("**FOUND ANIMATION [%s]\n",path.c_str()); + + AnimationAttributes animationAttributeList; + if(animationList[i]->getAttribute("fromHp",false) != NULL && animationList[i]->getAttribute("toHp",false) != NULL) { + animationAttributeList.fromHp = animationList[i]->getAttribute("fromHp")->getIntValue(); + animationAttributeList.toHp = animationList[i]->getAttribute("toHp")->getIntValue(); + } + animationAttributes.push_back(animationAttributeList); } else { SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line %d] WARNING CANNOT LOAD MODEL [%s] for parentLoader [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str(),parentLoader.c_str()); @@ -191,7 +198,7 @@ void SkillType::load(const XmlNode *sn, const string &dir, const TechTree *tt, } -Model *SkillType::getAnimation(float animProgress, int *lastAnimationIndex) const { +Model *SkillType::getAnimation(float animProgress, const Unit *unit, int *lastAnimationIndex) const { int modelIndex = 0; if(animations.size() > 1) { //printf("animProgress = [%f] for skill [%s]\n",animProgress,name.c_str()); @@ -200,9 +207,25 @@ Model *SkillType::getAnimation(float animProgress, int *lastAnimationIndex) cons modelIndex = *lastAnimationIndex; } if(modelIndex < 0 || animProgress > 1.0f) { - //int modelIndex = random.randRange(0,animations.size()-1); - srand(time(NULL)); - modelIndex = rand() % animations.size(); + bool foundSpecificAnimation = false; + if(unit != NULL) { + for(unsigned int i = 0; i < animationAttributes.size(); ++i) { + const AnimationAttributes &attributes = animationAttributes[i]; + if(attributes.fromHp != 0 || attributes.toHp != 0) { + if(unit->getHp() >= attributes.fromHp && unit->getHp() <= attributes.toHp) { + modelIndex = i; + foundSpecificAnimation = true; + break; + } + } + } + } + + if(foundSpecificAnimation == false) { + //int modelIndex = random.randRange(0,animations.size()-1); + srand(time(NULL)); + modelIndex = rand() % animations.size(); + } } } if(lastAnimationIndex) { diff --git a/source/glest_game/types/skill_type.h b/source/glest_game/types/skill_type.h index 670fb22d..34f64de2 100644 --- a/source/glest_game/types/skill_type.h +++ b/source/glest_game/types/skill_type.h @@ -86,6 +86,17 @@ public: bool isAffected(const Unit *source, const Unit *dest) const; }; +class AnimationAttributes { +public: + AnimationAttributes() { + fromHp = 0; + toHp = 0; + } + + int fromHp; + int toHp; +}; + class SkillType { protected: @@ -96,6 +107,8 @@ protected: int speed; int animSpeed; vector animations; + vector animationAttributes; + SoundContainer sounds; float soundStartTime; RandomGen random; @@ -118,7 +131,7 @@ public: int getHpCost() const {return hpCost;} int getSpeed() const {return speed;} int getAnimSpeed() const {return animSpeed;} - Model *getAnimation(float animProgress=0, int *lastAnimationIndex=NULL) const; + Model *getAnimation(float animProgress=0, const Unit *unit=NULL, int *lastAnimationIndex=NULL) const; StaticSound *getSound() const {return sounds.getRandSound();} float getSoundStartTime() const {return soundStartTime;}