From ec980b3c586fc64d02ad02f31497013908056d52 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Tue, 8 Jun 2010 21:55:16 -0600 Subject: Remove hard-coded frame counts from Being class Also removes the Monster/Player difference in tmwAthena's Being::logic and moves the particle code from Being::setAttack to Being::Logic for tmwAthena. Reviewed-by: Chuck Miller --- src/animatedsprite.cpp | 10 +++ src/animatedsprite.h | 4 ++ src/being.cpp | 140 +++++++++++++++++++++-------------------- src/being.h | 14 ----- src/compoundsprite.cpp | 50 +++++++++++++++ src/compoundsprite.h | 14 +++++ src/imagesprite.h | 20 +++--- src/net/tmwa/beinghandler.cpp | 10 +-- src/net/tmwa/playerhandler.cpp | 1 - src/sprite.h | 10 +++ 10 files changed, 174 insertions(+), 99 deletions(-) (limited to 'src') diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 3e4104a9..9ddc001c 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -197,6 +197,16 @@ bool AnimatedSprite::setDirection(SpriteDirection direction) return false; } +size_t AnimatedSprite::getCurrentFrame() const +{ + return mFrameIndex; +} + +size_t AnimatedSprite::getFrameCount() const +{ + return mAnimation->getLength(); +} + int AnimatedSprite::getWidth() const { if (mFrame) diff --git a/src/animatedsprite.h b/src/animatedsprite.h index aae116be..67e9c7cb 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -73,6 +73,10 @@ class AnimatedSprite : public Sprite int getNumberOfLayers() { return 1; } + size_t getCurrentFrame() const; + + size_t getFrameCount() const; + private: bool updateCurrentAnimation(unsigned int dt); diff --git a/src/being.cpp b/src/being.cpp index 0e09126d..6bef136b 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -72,6 +72,7 @@ #include #define HAIR_FILE "hair.xml" +#define PARTICLE_LOCATION "graphics/particles/" static const int DEFAULT_BEING_WIDTH = 32; static const int DEFAULT_BEING_HEIGHT = 32; @@ -81,7 +82,6 @@ int Being::mNumberOfHairstyles = 1; Being::Being(int id, Type type, int subtype, Map *map): ActorSprite(id), mInfo(BeingInfo::Unknown), - mFrame(0), mWalkTime(0), mEmotion(0), mEmotionTime(0), mSpeechTime(0), @@ -380,7 +380,7 @@ void Being::handleAttack(Being *victim, int damage, AttackType type) if (Net::getNetworkType() == ServerInfo::TMWATHENA) { - mFrame = 0; + reset(); mWalkTime = tick_time; } @@ -570,24 +570,27 @@ void Being::setAction(Action action, int attackType) currentAction = mInfo->getAttack(attackType)->action; reset(); - int rotation = 0; - //attack particle effect - std::string particleEffect = mInfo->getAttack(attackType) - ->particleEffect; - if (!particleEffect.empty() && Particle::enabled) + if (Net::getNetworkType() == ServerInfo::MANASERV) { - switch (mSpriteDirection) + int rotation = 0; + //attack particle effect + std::string particleEffect = mInfo->getAttack(attackType) + ->particleEffect; + if (!particleEffect.empty() && Particle::enabled) { - case DIRECTION_DOWN: rotation = 0; break; - case DIRECTION_LEFT: rotation = 90; break; - case DIRECTION_UP: rotation = 180; break; - case DIRECTION_RIGHT: rotation = 270; break; - default: break; + switch (mSpriteDirection) + { + case DIRECTION_DOWN: rotation = 0; break; + case DIRECTION_LEFT: rotation = 90; break; + case DIRECTION_UP: rotation = 180; break; + case DIRECTION_RIGHT: rotation = 270; break; + default: break; + } + Particle *p; + p = particleEngine->addEffect(particleEffect, 0, 0, + rotation); + controlParticle(p); } - Particle *p; - p = particleEngine->addEffect(particleEffect, 0, 0, - rotation); - controlParticle(p); } } @@ -772,68 +775,67 @@ void Being::logic() } else if (Net::getNetworkType() == ServerInfo::TMWATHENA) { - if (getType() == MONSTER && (mAction != STAND)) - { - mFrame = (int) ((get_elapsed_time(mWalkTime) * 4) / getWalkSpeed().x); + int frameCount = getFrameCount(); - if (mFrame >= 4 && mAction != DEAD) - nextTile(); - } - else if (getType() == PLAYER) + switch (mAction) { - switch (mAction) - { - case STAND: - case SIT: - case DEAD: - case HURT: - break; - - case WALK: - mFrame = (int) ((get_elapsed_time(mWalkTime) * 6) - / getWalkSpeed().x); - if (mFrame >= 6) - nextTile(); - break; - - case ATTACK: - int rotation = 0; - std::string particleEffect = ""; - int frames = 4; + case STAND: + case SIT: + case DEAD: + case HURT: + break; + + case WALK: + if ((int) ((get_elapsed_time(mWalkTime) * frameCount) + / getWalkSpeed().x) >= frameCount) + nextTile(); + break; + + case ATTACK: + int rotation = 0; + std::string particleEffect = ""; - if (mEquippedWeapon && - mEquippedWeapon->getAttackType() == ACTION_ATTACK_BOW) - { - frames = 5; - } + int curFrame = (get_elapsed_time(mWalkTime) * frameCount) + / mAttackSpeed; - mFrame = (get_elapsed_time(mWalkTime) * frames) / mAttackSpeed; + //attack particle effect + if (mEquippedWeapon) + { + particleEffect = mEquippedWeapon->getParticleEffect(); - //attack particle effect - if (mEquippedWeapon) - particleEffect = mEquippedWeapon->getParticleEffect(); + if (!particleEffect.empty() && + findSameSubstring(particleEffect, + PARTICLE_LOCATION).empty()) + particleEffect = PARTICLE_LOCATION + + particleEffect; + } + else + { + particleEffect = mInfo->getAttack(mAttackType) + ->particleEffect; + } - if (!particleEffect.empty() && Particle::enabled && mFrame == 1) + if (!particleEffect.empty() && Particle::enabled + && curFrame == 1) + { + switch (mDirection) { - switch (mDirection) - { - case DOWN: rotation = 0; break; - case LEFT: rotation = 90; break; - case UP: rotation = 180; break; - case RIGHT: rotation = 270; break; - default: break; - } - Particle *p; - p = particleEngine->addEffect("graphics/particles/" + - particleEffect, 0, 0, rotation); - controlParticle(p); + case DOWN: rotation = 0; break; + case LEFT: rotation = 90; break; + case UP: rotation = 180; break; + case RIGHT: rotation = 270; break; + default: break; } + Particle *p; + p = particleEngine->addEffect(particleEffect, 0, 0, + rotation); + controlParticle(p); + } - if (mFrame >= frames) - nextTile(); + if (curFrame >= frameCount) + nextTile(); - break; - } + break; } // Update pixel coordinates diff --git a/src/being.h b/src/being.h index 76b50dec..4e7016c1 100644 --- a/src/being.h +++ b/src/being.h @@ -397,16 +397,6 @@ class Being : public ActorSprite, public ConfigListener */ void setDirection(Uint8 direction); - /** - * Returns the being's current sprite frame number. - */ - int getCurrentFrame() const { return mFrame; } - - /** - * Set the being's current sprite frame number. - */ - void setFrame(int frame) { mFrame = frame; } - /** * Returns the direction the being is facing. */ @@ -451,7 +441,6 @@ class Being : public ActorSprite, public ConfigListener */ const Path &getPath() const { return mPath; } - /** * Set the Emoticon type and time displayed above * the being. @@ -521,9 +510,6 @@ class Being : public ActorSprite, public ConfigListener BeingInfo *mInfo; - /** The current sprite Frame number to be displayed */ - int mFrame; - /** Used to trigger the nextStep (walking on next Tile) * TODO: Used by eAthena only? */ diff --git a/src/compoundsprite.cpp b/src/compoundsprite.cpp index d6731e5d..d1c6cd57 100644 --- a/src/compoundsprite.cpp +++ b/src/compoundsprite.cpp @@ -187,6 +187,26 @@ int CompoundSprite::getNumberOfLayers() const } } +size_t CompoundSprite::getCurrentFrame() const +{ + SpriteConstIterator it, it_end; + for (it = begin(), it_end = end(); it != it_end; it++) + if (*it) + return (*it)->getCurrentFrame(); + + return 0; +} + +size_t CompoundSprite::getFrameCount() const +{ + SpriteConstIterator it, it_end; + for (it = begin(), it_end = end(); it != it_end; it++) + if (*it) + return (*it)->getFrameCount(); + + return 0; +} + void CompoundSprite::addSprite(Sprite* sprite) { push_back(sprite); @@ -236,6 +256,36 @@ void CompoundSprite::ensureSize(size_t layerCount) mNeedsRedraw = true; } +/** + * Returns the curent frame in the current animation of the given layer. + */ +size_t CompoundSprite::getCurrentFrame(size_t layer) +{ + if (layer >= size()) + return 0; + + Sprite *s = getSprite(layer); + if (s) + return s->getCurrentFrame(); + + return 0; +} + +/** + * Returns the frame count in the current animation of the given layer. + */ +size_t CompoundSprite::getFrameCount(size_t layer) +{ + if (layer >= size()) + return 0; + + Sprite *s = getSprite(layer); + if (s) + return s->getFrameCount(); + + return 0; +} + void CompoundSprite::redraw() const { // TODO OpenGL support diff --git a/src/compoundsprite.h b/src/compoundsprite.h index c714c34b..38c38453 100644 --- a/src/compoundsprite.h +++ b/src/compoundsprite.h @@ -58,6 +58,10 @@ public: int getNumberOfLayers() const; + size_t getCurrentFrame() const; + + size_t getFrameCount() const; + size_t size() const { return std::vector::size(); } @@ -74,6 +78,16 @@ public: void ensureSize(size_t layerCount); + /** + * Returns the curent frame in the current animation of the given layer. + */ + virtual size_t getCurrentFrame(size_t layer); + + /** + * Returns the frame count in the current animation of the given layer. + */ + virtual size_t getFrameCount(size_t layer); + private: typedef CompoundSprite::iterator SpriteIterator; typedef CompoundSprite::const_iterator SpriteConstIterator; diff --git a/src/imagesprite.h b/src/imagesprite.h index 20f9fbef..8a195b8c 100644 --- a/src/imagesprite.h +++ b/src/imagesprite.h @@ -34,24 +34,24 @@ public: ~ImageSprite(); - virtual bool reset() + bool reset() { return false; } - virtual bool play(SpriteAction action) + bool play(SpriteAction action) { return false; } - virtual bool update(int time) + bool update(int time) { return false; } - virtual bool draw(Graphics* graphics, int posX, int posY) const; + bool draw(Graphics* graphics, int posX, int posY) const; - virtual int getWidth() const + int getWidth() const { return mImage->getWidth(); } - virtual int getHeight() const + int getHeight() const { return mImage->getHeight(); } - virtual const Image* getImage() const + const Image* getImage() const { return mImage; } virtual bool setDirection(SpriteDirection direction) @@ -60,6 +60,12 @@ public: int getNumberOfLayers() { return 1; } + size_t getCurrentFrame() const + { return 0; } + + size_t getFrameCount() const + { return 1; } + private: Image *mImage; }; diff --git a/src/net/tmwa/beinghandler.cpp b/src/net/tmwa/beinghandler.cpp index 9eaca74c..d9caa719 100644 --- a/src/net/tmwa/beinghandler.cpp +++ b/src/net/tmwa/beinghandler.cpp @@ -143,10 +143,9 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) break; } - if (msg.getId() == 0x0078) + if (msg.getId() == SMSG_BEING_VISIBLE) { dstBeing->clearPath(); - dstBeing->setFrame(0); dstBeing->setWalkTime(tick_time); dstBeing->setAction(Being::STAND); } @@ -349,7 +348,6 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) case 0x02: // Sit if (srcBeing) { - srcBeing->setFrame(0); srcBeing->setAction(Being::SIT); } break; @@ -357,7 +355,6 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) case 0x03: // Stand up if (srcBeing) { - srcBeing->setFrame(0); srcBeing->setAction(Being::STAND); } break; @@ -619,7 +616,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) msg.readInt8(); // unknown dstBeing->setWalkTime(tick_time); - dstBeing->setFrame(0); + dstBeing->reset(); dstBeing->setStunMode(stunMode); dstBeing->setStatusEffectBlock(0, (statusEffects >> 16) & 0xffff); @@ -650,10 +647,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) y = msg.readInt16(); dstBeing->setTileCoords(x, y); if (dstBeing->getCurrentAction() == Being::WALK) - { - dstBeing->setFrame(0); dstBeing->setAction(Being::STAND); - } } } break; diff --git a/src/net/tmwa/playerhandler.cpp b/src/net/tmwa/playerhandler.cpp index 84fcdeee..26c7e922 100644 --- a/src/net/tmwa/playerhandler.cpp +++ b/src/net/tmwa/playerhandler.cpp @@ -216,7 +216,6 @@ void PlayerHandler::handleMessage(Net::MessageIn &msg) } player_node->setAction(Being::STAND); - player_node->setFrame(0); player_node->setTileCoords(x, y); logger->log("Adjust scrolling by %d:%d", (int) scrollOffsetX, diff --git a/src/sprite.h b/src/sprite.h index 2bcd754a..cce41f1d 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -93,6 +93,16 @@ class Sprite virtual float getAlpha() const { return mAlpha; } + /** + * Returns the current frame number for the sprite. + */ + virtual size_t getCurrentFrame() const = 0; + + /** + * Returns the frame count for the sprite. + */ + virtual size_t getFrameCount() const = 0; + protected: float mAlpha; /**< The alpha opacity used to draw */ }; -- cgit v1.2.3-70-g09d2