From 8714de88fe9715b5a4014f16d15bc55f8860cdcf Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Tue, 1 Aug 2006 19:05:34 +0000 Subject: animations and directions are now passed and stored as enums and no longer as strings. --- ChangeLog | 6 +++ src/animatedsprite.cpp | 113 ++++++++++++++++++++++++++++++++++++++++--------- src/animatedsprite.h | 51 +++++++++++++++++++--- src/animation.cpp | 8 ++-- src/animation.h | 7 ++- src/being.cpp | 36 ++++++++-------- 6 files changed, 168 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index c5d32c33..1cfd7911 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2006-08-01 Philipp Sehmisch + + * src/animation.h, src/animation.cpp, src/animatedsprite.h, + src/animatedsprite.cpp, src/being.cpp: animations and directions + are now passed and stored as enums and no longer as strings. + 2006-07-30 Björn Steinbrink * src/animatedsprite.h, src/animatedsprite.cpp: Unified the play diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 9c82bf7a..0e93ad97 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -32,7 +32,7 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): mAction(NULL), - mDirection("down"), + mDirection(DIRECTION_DOWN), mLastTime(0), mSpeed(1.0f) { @@ -107,7 +107,7 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): Action *action = new Action(); action->setSpriteset(mSpritesets[imageset]); - mActions[name] = action; + mActions[makeSpriteAction(name)] = action; // get animations for (xmlNodePtr animationNode = node->xmlChildrenNode; @@ -153,29 +153,29 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): } } } // for phaseNode - action->setAnimation(direction, animation); + action->setAnimation(makeSpriteDirection(direction), animation); } // if "" } // for animationNode } // if "" else if "" } // for node // Complete missing actions - substituteAction("walk", "stand"); - substituteAction("walk", "run"); - substituteAction("attack", "stand"); - substituteAction("attack_swing", "attack"); - substituteAction("attack_stab", "attack_swing"); - substituteAction("attack_bow", "attack_stab"); - substituteAction("attack_throw", "attack_swing"); - substituteAction("cast_magic", "attack_swing"); - substituteAction("use_item", "cast_magic"); - substituteAction("sit", "stand"); - substituteAction("sleeping", "sit"); - substituteAction("hurt", "stand"); - substituteAction("dead", "hurt"); + substituteAction(ACTION_WALK, ACTION_STAND); + substituteAction(ACTION_WALK, ACTION_RUN); + substituteAction(ACTION_ATTACK, ACTION_STAND); + substituteAction(ACTION_ATTACK_SWING, ACTION_ATTACK); + substituteAction(ACTION_ATTACK_STAB, ACTION_ATTACK_SWING); + substituteAction(ACTION_ATTACK_BOW, ACTION_ATTACK_STAB); + substituteAction(ACTION_ATTACK_THROW, ACTION_ATTACK_SWING); + substituteAction(ACTION_CAST_MAGIC, ACTION_ATTACK_SWING); + substituteAction(ACTION_USE_ITEM, ACTION_CAST_MAGIC); + substituteAction(ACTION_SIT, ACTION_STAND); + substituteAction(ACTION_SLEEP, ACTION_SIT); + substituteAction(ACTION_HURT, ACTION_STAND); + substituteAction(ACTION_DEAD, ACTION_HURT); // Play the stand animation by default - play("stand"); + play(ACTION_STAND); xmlFreeDoc(doc); } @@ -209,8 +209,8 @@ AnimatedSprite::getProperty(xmlNodePtr node, const char* name, } void -AnimatedSprite::substituteAction(const std::string& complete, - const std::string& with) +AnimatedSprite::substituteAction(SpriteAction complete, + SpriteAction with) { if (mActions.find(complete) == mActions.end()) { @@ -228,13 +228,13 @@ AnimatedSprite::~AnimatedSprite() } void -AnimatedSprite::play(const std::string& action, int time) +AnimatedSprite::play(SpriteAction action, int time) { ActionIterator i = mActions.find(action); if (i == mActions.end()) { - logger->log("Warning: no action \"%s\" defined!", action.c_str()); + logger->log("Warning: no action \"%s\" defined!", action); mAction = NULL; return; } @@ -298,3 +298,74 @@ AnimatedSprite::getHeight() const { return mAction ? mAction->getSpriteset()->getHeight() : 0; } + +SpriteAction +AnimatedSprite::makeSpriteAction(const std::string& action) +{ + if (action == "stand") { + return ACTION_STAND; + } + else if (action == "walk") { + return ACTION_WALK; + } + else if (action == "run") { + return ACTION_RUN; + } + else if (action == "attack") { + return ACTION_ATTACK; + } + else if (action == "attack_swing") { + return ACTION_ATTACK_SWING; + } + else if (action == "attack_stab") { + return ACTION_ATTACK_STAB; + } + else if (action == "attack_bow") { + return ACTION_ATTACK_BOW; + } + else if (action == "attack_throw") { + return ACTION_ATTACK_THROW; + } + else if (action == "cast_magic") { + return ACTION_CAST_MAGIC; + } + else if (action == "use_item") { + return ACTION_USE_ITEM; + } + else if (action == "sit") { + return ACTION_SIT; + } + else if (action == "sleep") { + return ACTION_SLEEP; + } + else if (action == "hurt") { + return ACTION_HURT; + } + else if (action == "dead") { + return ACTION_DEAD; + } + else + { + return ACTION_DEFAULT; + } +} + + + +SpriteDirection +AnimatedSprite::makeSpriteDirection(const std::string& direction) +{ + if (direction == "up") { + return DIRECTION_UP; + } + else if (direction == "left") { + return DIRECTION_LEFT; + } + else if (direction == "right") { + return DIRECTION_RIGHT; + } + else + { + return DIRECTION_DOWN; + } +} diff --git a/src/animatedsprite.h b/src/animatedsprite.h index 4ef1d9f7..16b30077 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -34,6 +34,33 @@ class Action; class Graphics; class Spriteset; +enum SpriteAction +{ + ACTION_DEFAULT = 0, + ACTION_STAND, + ACTION_WALK, + ACTION_RUN, + ACTION_ATTACK, + ACTION_ATTACK_SWING, + ACTION_ATTACK_STAB, + ACTION_ATTACK_BOW, + ACTION_ATTACK_THROW, + ACTION_CAST_MAGIC, + ACTION_USE_ITEM, + ACTION_SIT, + ACTION_SLEEP, + ACTION_HURT, + ACTION_DEAD +}; + +enum SpriteDirection +{ + DIRECTION_DOWN = 0, + DIRECTION_UP, + DIRECTION_LEFT, + DIRECTION_RIGHT +}; + /** * Defines a class to load an animation. */ @@ -55,7 +82,7 @@ class AnimatedSprite * duration of the specified time, 0 means default. */ void - play(const std::string& action, int time = 0); + play(SpriteAction action, int time = 0); /** * Inform the animation of the passed time so that it can output the @@ -86,7 +113,7 @@ class AnimatedSprite * Sets the direction. */ void - setDirection(const std::string& direction) + setDirection(SpriteDirection direction) { mDirection = direction; } @@ -97,17 +124,17 @@ class AnimatedSprite * animations become a copy of those of the action "with". */ void - substituteAction(const std::string& complete, - const std::string& with); + substituteAction(SpriteAction complete, + SpriteAction with); typedef std::map Spritesets; typedef Spritesets::iterator SpritesetIterator; Spritesets mSpritesets; - typedef std::map Actions; + typedef std::map Actions; typedef Actions::iterator ActionIterator; Actions mActions; Action *mAction; - std::string mDirection; + SpriteDirection mDirection; int mLastTime; float mSpeed; @@ -126,6 +153,18 @@ class AnimatedSprite */ static std::string getProperty(xmlNodePtr node, const char* name, const std::string& def); + + /** + * converts a string into a SpriteAction enum + */ + SpriteAction + makeSpriteAction(const std::string& action); + + /** + * converts a string into a SpriteDirection enum + */ + SpriteDirection + makeSpriteDirection(const std::string& direction); }; #endif diff --git a/src/animation.cpp b/src/animation.cpp index cd716d78..1af8c616 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -104,26 +104,26 @@ Action::~Action() } Animation* -Action::getAnimation(const std::string& direction) const +Action::getAnimation(int direction) const { Animations::const_iterator i = mAnimations.find(direction); // When the direction isn't defined, try the default if (i == mAnimations.end()) { - i = mAnimations.find("default"); + i = mAnimations.find(0); } return (i == mAnimations.end()) ? NULL : i->second; } void -Action::setAnimation(const std::string& direction, Animation *animation) +Action::setAnimation(int direction, Animation *animation) { // Set first direction as default direction if (mAnimations.empty()) { - mAnimations["default"] = animation; + mAnimations[0] = animation; } mAnimations[direction] = animation; diff --git a/src/animation.h b/src/animation.h index fe227637..f2c40194 100644 --- a/src/animation.h +++ b/src/animation.h @@ -26,7 +26,6 @@ #include #include -#include #include @@ -102,14 +101,14 @@ class Action getSpriteset() const { return mSpriteset; } void - setAnimation(const std::string& direction, Animation *animation); + setAnimation(int direction, Animation *animation); Animation* - getAnimation(const std::string& direction) const; + getAnimation(int direction) const; protected: Spriteset *mSpriteset; - typedef std::map Animations; + typedef std::map Animations; typedef Animations::iterator AnimationIterator; Animations mAnimations; }; diff --git a/src/being.cpp b/src/being.cpp index 0295defb..13bcb44a 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -169,42 +169,42 @@ Being::setAction(Uint8 action) { //if (action != mAction) { - std::string currentAction = "stand"; + SpriteAction currentAction = ACTION_STAND; switch (action) { case WALK: - currentAction = "walk"; + currentAction = ACTION_WALK; break; case SIT: - currentAction = "sit"; + currentAction = ACTION_SIT; break; case ATTACK: if (getType() == MONSTER) { - currentAction = "dead"; + currentAction = ACTION_DEAD; }else{ switch (getWeapon()) { case 2: - currentAction = "attack_bow"; + currentAction = ACTION_ATTACK_BOW; break; case 1: - currentAction = "attack_stab"; + currentAction = ACTION_ATTACK_STAB; break; case 0: - currentAction = "attack"; + currentAction = ACTION_ATTACK; break; } }; break; case MONSTER_ATTACK: - currentAction = "attack"; + currentAction = ACTION_ATTACK; break; case DEAD: - currentAction = "dead"; + currentAction = ACTION_DEAD; break; default: - currentAction = "stand"; + currentAction = ACTION_STAND; break; } @@ -212,9 +212,9 @@ Being::setAction(Uint8 action) { if (mSprites[i] != NULL) { - if (currentAction == "attack" || - currentAction == "attack_stab" || - currentAction == "attack_bow") + if (currentAction == ACTION_ATTACK || + currentAction == ACTION_ATTACK_STAB || + currentAction == ACTION_ATTACK_BOW) { mSprites[i]->play(currentAction, mAttackSpeed); } @@ -232,23 +232,23 @@ void Being::setDirection(Uint8 direction) { mDirection = direction; - std::string dir; + SpriteDirection dir; if (direction & UP) { - dir = "up"; + dir = DIRECTION_UP; } else if (direction & RIGHT) { - dir = "right"; + dir = DIRECTION_RIGHT; } else if (direction & DOWN) { - dir = "down"; + dir = DIRECTION_DOWN; } else { - dir = "left"; + dir = DIRECTION_LEFT; } for (int i = 0; i < VECTOREND_SPRITE; i++) -- cgit v1.2.3-70-g09d2