From e0541b52c4321a8535516590de0df40ec0d98436 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 19 Jan 2013 14:56:24 -0800 Subject: [PATCH] Use a list to store and get state information --- apps/openmw/mwmechanics/character.cpp | 67 ++++++++++++++++----------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/apps/openmw/mwmechanics/character.cpp b/apps/openmw/mwmechanics/character.cpp index 5127b65641..a494008236 100644 --- a/apps/openmw/mwmechanics/character.cpp +++ b/apps/openmw/mwmechanics/character.cpp @@ -19,6 +19,8 @@ #include "character.hpp" +#include + #include "../mwrender/animation.hpp" #include "../mwbase/environment.hpp" @@ -29,6 +31,35 @@ namespace MWMechanics { +static const struct { + CharacterState state; + const char groupname[32]; + Ogre::Vector3 accumulate; +} sStateList[] = { + { CharState_SpecialIdle, "idle", Ogre::Vector3(1.0f, 1.0f, 0.0f) }, + { CharState_Idle, "idle", Ogre::Vector3::ZERO }, + + { CharState_WalkForward, "walkforward", Ogre::Vector3(0.0f, 1.0f, 0.0f) }, + { CharState_WalkBack, "walkback", Ogre::Vector3(0.0f, 1.0f, 0.0f) }, + + { CharState_Dead, "death1", Ogre::Vector3(1.0f, 1.0f, 0.0f) }, +}; +static const size_t sStateListSize = sizeof(sStateList)/sizeof(sStateList[0]); + +static void getStateInfo(CharacterState state, std::string *group, Ogre::Vector3 *accum) +{ + for(size_t i = 0;i < sStateListSize;i++) + { + if(sStateList[i].state == state) + { + *group = sStateList[i].groupname; + *accum = sStateList[i].accumulate; + return; + } + } + throw std::runtime_error("Failed to find character state "+Ogre::StringConverter::toString(state)); +} + CharacterController::CharacterController(const MWWorld::Ptr &ptr, MWRender::Animation *anim, CharacterState state, bool loop) : mPtr(ptr), mAnimation(anim), mDirection(Ogre::Vector3::ZERO), mState(state), mSkipAnim(false), mLoop(loop) { @@ -41,7 +72,11 @@ CharacterController::CharacterController(const MWWorld::Ptr &ptr, MWRender::Anim } mAnimation->setController(this); - setState(mState, loop); + + Ogre::Vector3 accum; + getStateInfo(mState, &mCurrentGroup, &accum); + mAnimation->setAccumulation(accum); + mAnimation->play(mCurrentGroup, "stop"); } CharacterController::CharacterController(const CharacterController &rhs) @@ -186,33 +221,11 @@ void CharacterController::setState(CharacterState state, bool loop) if(mAnimNames.size() == 0) return; mAnimQueue.clear(); - switch(mState) - { - case CharState_SpecialIdle: - break; - case CharState_Idle: - mCurrentGroup = "idle"; - mAnimation->setAccumulation(Ogre::Vector3::ZERO); - mAnimation->play(mCurrentGroup, "start"); - break; - case CharState_WalkForward: - mCurrentGroup = "walkforward"; - mAnimation->setAccumulation(Ogre::Vector3(0.0f, 1.0f, 0.0f)); - mAnimation->play(mCurrentGroup, "start"); - break; - case CharState_WalkBack: - mCurrentGroup = "walkback"; - mAnimation->setAccumulation(Ogre::Vector3(0.0f, 1.0f, 0.0f)); - mAnimation->play(mCurrentGroup, "start"); - break; - - case CharState_Dead: - mCurrentGroup = "death1"; - mAnimation->setAccumulation(Ogre::Vector3(1.0f, 1.0f, 0.0f)); - mAnimation->play(mCurrentGroup, "start"); - break; - } + Ogre::Vector3 accum; + getStateInfo(mState, &mCurrentGroup, &accum); + mAnimation->setAccumulation(accum); + mAnimation->play(mCurrentGroup, "start"); } }