diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/animatedsprite.cpp | 53 | ||||
-rw-r--r-- | src/animatedsprite.h | 54 | ||||
-rw-r--r-- | src/animation.h | 28 | ||||
-rw-r--r-- | src/being.cpp | 124 | ||||
-rw-r--r-- | src/being.h | 7 | ||||
-rw-r--r-- | src/net/network.cpp | 4 | ||||
-rw-r--r-- | src/player.cpp | 45 |
7 files changed, 191 insertions, 124 deletions
diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 0e93ad97..3ea400ac 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -228,6 +228,19 @@ AnimatedSprite::~AnimatedSprite() } void +AnimatedSprite::reset() +{ + // Reset all defined actions (because of aliases, some will be resetted + // multiple times) + for (ActionIterator i = mActions.begin(); i != mActions.end(); ++i) + { + //TODO: If resetting everything is really a nice way of fixing the + // synchronization issues, finish implementing this. + //i->second->reset(); + } +} + +void AnimatedSprite::play(SpriteAction action, int time) { ActionIterator i = mActions.find(action); @@ -306,66 +319,62 @@ AnimatedSprite::makeSpriteAction(const std::string& action) return ACTION_STAND; } else if (action == "walk") { - return ACTION_WALK; + return ACTION_WALK; } else if (action == "run") { - return ACTION_RUN; + return ACTION_RUN; } else if (action == "attack") { - return ACTION_ATTACK; + return ACTION_ATTACK; } else if (action == "attack_swing") { - return ACTION_ATTACK_SWING; + return ACTION_ATTACK_SWING; } else if (action == "attack_stab") { - return ACTION_ATTACK_STAB; + return ACTION_ATTACK_STAB; } else if (action == "attack_bow") { - return ACTION_ATTACK_BOW; + return ACTION_ATTACK_BOW; } else if (action == "attack_throw") { - return ACTION_ATTACK_THROW; + return ACTION_ATTACK_THROW; } else if (action == "cast_magic") { - return ACTION_CAST_MAGIC; + return ACTION_CAST_MAGIC; } else if (action == "use_item") { - return ACTION_USE_ITEM; + return ACTION_USE_ITEM; } else if (action == "sit") { - return ACTION_SIT; + return ACTION_SIT; } else if (action == "sleep") { - return ACTION_SLEEP; + return ACTION_SLEEP; } else if (action == "hurt") { - return ACTION_HURT; + return ACTION_HURT; } else if (action == "dead") { - return ACTION_DEAD; + return ACTION_DEAD; } - else - { + else { return ACTION_DEFAULT; } } - - SpriteDirection AnimatedSprite::makeSpriteDirection(const std::string& direction) { if (direction == "up") { - return DIRECTION_UP; + return DIRECTION_UP; } else if (direction == "left") { - return DIRECTION_LEFT; + return DIRECTION_LEFT; } else if (direction == "right") { - return DIRECTION_RIGHT; + return DIRECTION_RIGHT; } - else - { + else { return DIRECTION_DOWN; } } diff --git a/src/animatedsprite.h b/src/animatedsprite.h index 16b30077..89394d6c 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -78,6 +78,13 @@ class AnimatedSprite ~AnimatedSprite(); /** + * Resets the animated sprite. This is used to synchronize several + * animated sprites. + */ + void + reset(); + + /** * Plays an action using the current direction that will have a * duration of the specified time, 0 means default. */ @@ -118,27 +125,14 @@ class AnimatedSprite mDirection = direction; } - protected: + private: /** * When there are no animations defined for the action "complete", its * animations become a copy of those of the action "with". */ void - substituteAction(SpriteAction complete, - SpriteAction with); - - typedef std::map<std::string, Spriteset*> Spritesets; - typedef Spritesets::iterator SpritesetIterator; - Spritesets mSpritesets; - typedef std::map<SpriteAction, Action*> Actions; - typedef Actions::iterator ActionIterator; - Actions mActions; - Action *mAction; - SpriteDirection mDirection; - int mLastTime; - float mSpeed; + substituteAction(SpriteAction complete, SpriteAction with); - private: /** * Gets an integer property from an xmlNodePtr. * @@ -146,25 +140,39 @@ class AnimatedSprite * TODO: shared in a static utility class. */ static int - getProperty(xmlNodePtr node, const char* name, int def); + getProperty(xmlNodePtr node, const char *name, int def); /** * Gets a string property from an xmlNodePtr. */ static std::string - getProperty(xmlNodePtr node, const char* name, const std::string& def); + getProperty(xmlNodePtr node, const char *name, const std::string &def); /** - * converts a string into a SpriteAction enum + * Converts a string into a SpriteAction enum. */ - SpriteAction - makeSpriteAction(const std::string& action); + static SpriteAction + makeSpriteAction(const std::string &action); /** - * converts a string into a SpriteDirection enum + * Converts a string into a SpriteDirection enum. */ - SpriteDirection - makeSpriteDirection(const std::string& direction); + static SpriteDirection + makeSpriteDirection(const std::string &direction); + + + typedef std::map<std::string, Spriteset*> Spritesets; + typedef Spritesets::iterator SpritesetIterator; + + typedef std::map<SpriteAction, Action*> Actions; + typedef Actions::iterator ActionIterator; + + Spritesets mSpritesets; + Actions mActions; + Action *mAction; + SpriteDirection mDirection; + int mLastTime; + float mSpeed; }; #endif diff --git a/src/animation.h b/src/animation.h index f2c40194..c5b277b7 100644 --- a/src/animation.h +++ b/src/animation.h @@ -55,16 +55,32 @@ class Animation */ Animation(); - void addPhase(int image, unsigned int delay, int offsetX, int offsetY); + void + addPhase(int image, unsigned int delay, int offsetX, int offsetY); - void update(unsigned int time); + void + update(unsigned int time); - int getCurrentPhase() const; + int + getCurrentPhase() const; - int getOffsetX() const { return (*iCurrentPhase).offsetX; }; - int getOffsetY() const { return (*iCurrentPhase).offsetY; }; + /** + * Returns the x offset of the current frame. + */ + int + getOffsetX() const { return (*iCurrentPhase).offsetX; }; - int getLength(); + /** + * Returns the y offset of the current frame. + */ + int + getOffsetY() const { return (*iCurrentPhase).offsetY; }; + + /** + * Returns the length of this animation. + */ + int + getLength(); protected: std::list<AnimationPhase> mAnimationPhases; diff --git a/src/being.cpp b/src/being.cpp index 13bcb44a..4d106641 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -167,64 +167,63 @@ Being::setMap(Map *map) void Being::setAction(Uint8 action) { - //if (action != mAction) + SpriteAction currentAction = ACTION_STAND; + switch (action) { - SpriteAction currentAction = ACTION_STAND; - switch (action) - { - case WALK: - currentAction = ACTION_WALK; - break; - case SIT: - currentAction = ACTION_SIT; - break; - case ATTACK: - if (getType() == MONSTER) - { - currentAction = ACTION_DEAD; - }else{ - switch (getWeapon()) - { - case 2: - currentAction = ACTION_ATTACK_BOW; - break; - case 1: - currentAction = ACTION_ATTACK_STAB; - break; - case 0: - currentAction = ACTION_ATTACK; - break; - } - }; - break; - case MONSTER_ATTACK: - currentAction = ACTION_ATTACK; - break; - case DEAD: + case WALK: + currentAction = ACTION_WALK; + break; + case SIT: + currentAction = ACTION_SIT; + break; + case ATTACK: + if (getType() == MONSTER) + { currentAction = ACTION_DEAD; - break; - default: - currentAction = ACTION_STAND; - break; - } + } + else { + switch (getWeapon()) + { + case 2: + currentAction = ACTION_ATTACK_BOW; + break; + case 1: + currentAction = ACTION_ATTACK_STAB; + break; + case 0: + currentAction = ACTION_ATTACK; + break; + } + }; + break; + case MONSTER_ATTACK: + currentAction = ACTION_ATTACK; + break; + case DEAD: + currentAction = ACTION_DEAD; + break; + default: + currentAction = ACTION_STAND; + break; + } - for (int i = 0; i < VECTOREND_SPRITE; i++) + for (int i = 0; i < VECTOREND_SPRITE; i++) + { + if (mSprites[i] != NULL) { - if (mSprites[i] != NULL) + if (currentAction == ACTION_ATTACK || + currentAction == ACTION_ATTACK_STAB || + currentAction == ACTION_ATTACK_BOW) { - if (currentAction == ACTION_ATTACK || - currentAction == ACTION_ATTACK_STAB || - currentAction == ACTION_ATTACK_BOW) - { - mSprites[i]->play(currentAction, mAttackSpeed); - } - else - { - mSprites[i]->play(currentAction); - } + mSprites[i]->play(currentAction, mAttackSpeed); + } + else + { + mSprites[i]->play(currentAction); } } } + mAction = action; } @@ -232,29 +231,36 @@ void Being::setDirection(Uint8 direction) { mDirection = direction; + SpriteDirection dir = getSpriteDirection(); + + for (int i = 0; i < VECTOREND_SPRITE; i++) + { + if (mSprites[i] != NULL) mSprites[i]->setDirection(dir); + } +} + +SpriteDirection +Being::getSpriteDirection() const +{ SpriteDirection dir; - if (direction & UP) + if (mDirection & UP) { dir = DIRECTION_UP; } - else if (direction & RIGHT) + else if (mDirection & RIGHT) { dir = DIRECTION_RIGHT; } - else if (direction & DOWN) + else if (mDirection & DOWN) { dir = DIRECTION_DOWN; } - else - { + else { dir = DIRECTION_LEFT; } - for (int i = 0; i < VECTOREND_SPRITE; i++) - { - if (mSprites[i] != NULL) mSprites[i]->setDirection(dir); - } + return dir; } void diff --git a/src/being.h b/src/being.h index 58a3e82d..0735efe9 100644 --- a/src/being.h +++ b/src/being.h @@ -31,6 +31,7 @@ #include "sprite.h" #include "map.h" +#include "animatedsprite.h" #define NR_HAIR_STYLES 7 #define NR_HAIR_COLORS 10 @@ -347,6 +348,12 @@ class Being : public Sprite */ int getOffset(char pos, char neg) const; + /** + * Returns the sprite direction of this being. + */ + SpriteDirection + getSpriteDirection() const; + Uint32 mId; /**< Unique sprite id */ Uint16 mWeapon; /**< Weapon picture id */ Uint16 mWalkSpeed; /**< Walking speed */ diff --git a/src/net/network.cpp b/src/net/network.cpp index bd0e2444..757a533e 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -112,8 +112,8 @@ Network::~Network() SDL_DestroyMutex(mMutex); - delete mInBuffer; - delete mOutBuffer; + delete[] mInBuffer; + delete[] mOutBuffer; } bool Network::connect(const std::string &address, short port) diff --git a/src/player.cpp b/src/player.cpp index 096520cb..2e119e2d 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -90,11 +90,13 @@ Player::setSex(Uint8 sex) delete mSprites[BASE_SPRITE]; if (sex == 0) { - mSprites[BASE_SPRITE] = new AnimatedSprite("graphics/sprites/player_male_base.xml", 0); + mSprites[BASE_SPRITE] = new AnimatedSprite( + "graphics/sprites/player_male_base.xml", 0); } else { - mSprites[BASE_SPRITE] = new AnimatedSprite("graphics/sprites/player_female_base.xml", 0); + mSprites[BASE_SPRITE] = new AnimatedSprite( + "graphics/sprites/player_female_base.xml", 0); } } Being::setSex(sex); @@ -109,10 +111,17 @@ Player::setHairColor(Uint16 color) { delete mSprites[HAIR_SPRITE]; } - mSprites[HAIR_SPRITE] = new AnimatedSprite("graphics/sprites/hairstyle"+toString(mHairStyle)+".xml", color - 1); + + AnimatedSprite *newHairSprite = new AnimatedSprite( + "graphics/sprites/hairstyle" + toString(mHairStyle) + ".xml", + color - 1); + newHairSprite->setDirection(getSpriteDirection()); + + mSprites[HAIR_SPRITE] = newHairSprite; + + setAction(mAction); } - setDirection(mDirection); - setAction(mAction); + Being::setHairColor(color); }; @@ -125,10 +134,17 @@ Player::setHairStyle(Uint16 style) { delete mSprites[HAIR_SPRITE]; } - mSprites[HAIR_SPRITE] = new AnimatedSprite("graphics/sprites/hairstyle"+toString(style)+".xml", mHairColor - 1); + + AnimatedSprite *newHairSprite = new AnimatedSprite( + "graphics/sprites/hairstyle" + toString(style) + ".xml", + mHairColor - 1); + newHairSprite->setDirection(getSpriteDirection()); + + mSprites[HAIR_SPRITE] = newHairSprite; + + setAction(mAction); } - setDirection(mDirection); - setAction(mAction); + Being::setHairStyle(style); }; @@ -151,16 +167,21 @@ Player::setVisibleEquipment(Uint8 slot, Uint8 id) // id = 0 means unequip if (mSprites[position]) { delete mSprites[position]; - mSprites[position] = 0; + mSprites[position] = NULL; } if (id) { char stringId[4]; sprintf(stringId, "%03i", id); - mSprites[position] = new AnimatedSprite( + + AnimatedSprite *equipmentSprite = new AnimatedSprite( "graphics/sprites/item" + toString(stringId) + ".xml", 0); + equipmentSprite->setDirection(getSpriteDirection()); + + mSprites[position] = equipmentSprite; + + setAction(mAction); } - setDirection(mDirection); - setAction(mAction); + Being::setVisibleEquipment(slot, id); } |