diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-17 16:34:43 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-21 15:47:08 +0100 |
commit | ffb5fe6236fd9c9e758631c2ea715943018af402 (patch) | |
tree | 475cf3409247dfeed7e91a05cfd51e957cf36de9 | |
parent | 6e0681392282cce5fd1009d88b4a248e7f545f4c (diff) | |
download | mana-ffb5fe6236fd9c9e758631c2ea715943018af402.tar.gz mana-ffb5fe6236fd9c9e758631c2ea715943018af402.tar.bz2 mana-ffb5fe6236fd9c9e758631c2ea715943018af402.tar.xz mana-ffb5fe6236fd9c9e758631c2ea715943018af402.zip |
Made ActorSprite aggregate instead of subclass CompoundSprite
Also, CompoundSprite itself no longer derives from Sprite.
This simplifies the class hierarchies and avoids the compiler warning
about Being::setDirection shadowing Sprite::setDirection.
-rw-r--r-- | src/actorsprite.cpp | 14 | ||||
-rw-r--r-- | src/actorsprite.h | 12 | ||||
-rw-r--r-- | src/being.cpp | 26 | ||||
-rw-r--r-- | src/compoundsprite.cpp | 15 | ||||
-rw-r--r-- | src/compoundsprite.h | 54 |
5 files changed, 49 insertions, 72 deletions
diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp index b98cad70..f2c9ac88 100644 --- a/src/actorsprite.cpp +++ b/src/actorsprite.cpp @@ -87,13 +87,13 @@ bool ActorSprite::draw(Graphics *graphics, int offsetX, int offsetY) const bool ActorSprite::drawSpriteAt(Graphics *graphics, int x, int y) const { - return CompoundSprite::draw(graphics, x, y); + return mSprites.draw(graphics, x, y); } void ActorSprite::logic() { // Update sprite animations - update(Time::deltaTimeMs()); + mSprites.update(Time::deltaTimeMs()); if (mUsedTargetCursor) mUsedTargetCursor->update(Time::deltaTimeMs()); @@ -131,20 +131,20 @@ void ActorSprite::setTargetType(TargetCursorType type) void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display, bool forceDisplay) { - clear(); + mSprites.clear(); for (const auto &sprite : display.sprites) { std::string file = paths.getStringValue("sprites") + sprite.sprite; - addSprite(AnimatedSprite::load(file, sprite.variant)); + mSprites.add(AnimatedSprite::load(file, sprite.variant)); } // Ensure that something is shown, if desired - if (size() == 0 && forceDisplay) + if (mSprites.size() == 0 && forceDisplay) { if (display.image.empty()) { - addSprite(AnimatedSprite::load(paths.getStringValue("sprites") + mSprites.add(AnimatedSprite::load(paths.getStringValue("sprites") + paths.getStringValue("spriteErrorFile"))); } else @@ -158,7 +158,7 @@ void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display, img = Theme::getImageFromTheme( paths.getStringValue("unknownItemFile")); - addSprite(new ImageSprite(img)); + mSprites.add(new ImageSprite(img)); } } diff --git a/src/actorsprite.h b/src/actorsprite.h index 35635752..c82a486a 100644 --- a/src/actorsprite.h +++ b/src/actorsprite.h @@ -28,7 +28,7 @@ class SimpleAnimation; class StatusEffect; -class ActorSprite : public CompoundSprite, public Actor +class ActorSprite : public Actor { public: enum Type @@ -108,11 +108,11 @@ public: */ void untarget() { mUsedTargetCursor = nullptr; } - void setAlpha(float alpha) override - { CompoundSprite::setAlpha(alpha); } + void setAlpha(float alpha) override { mSprites.setAlpha(alpha); } + float getAlpha() const override { return mSprites.getAlpha(); } - float getAlpha() const override - { return CompoundSprite::getAlpha(); } + int getWidth() const { return mSprites.getWidth(); } + int getHeight() const { return mSprites.getHeight(); } static void load(); static void unload(); @@ -124,6 +124,8 @@ protected: int mId; ParticleList mChildParticleEffects; + CompoundSprite mSprites; + private: /** Load the target cursors into memory */ static void initTargetCursor(); diff --git a/src/being.cpp b/src/being.cpp index 567850a5..7de30d9b 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -118,7 +118,7 @@ void Being::setType(Type type, int subtype) mShowName = true; break; case PLAYER: { - clear(); + mSprites.clear(); mChildParticleEffects.clear(); int id = -100 - subtype; @@ -139,7 +139,7 @@ void Being::setType(Type type, int subtype) break; } - doRedraw(); + mSprites.doRedraw(); updateName(); updateNamePosition(); @@ -676,12 +676,12 @@ void Being::setAction(Action action, int attackId) if (mEquippedWeapon) { currentAction = mEquippedWeapon->attackAction; - reset(); + mSprites.reset(); } else { currentAction = mInfo->getAttack(attackId).action; - reset(); + mSprites.reset(); // Attack particle effect if (Particle::enabled) @@ -720,7 +720,7 @@ void Being::setAction(Action action, int attackId) if (currentAction != SpriteAction::INVALID) { - play(currentAction); + mSprites.play(currentAction); mAction = action; } @@ -827,7 +827,7 @@ void Being::setDirection(uint8_t direction) mSpriteDirection = dir; updatePlayerSprites(); - CompoundSprite::setDirection(dir); + mSprites.setDirection(dir); } int Being::getCollisionRadius() const @@ -870,7 +870,7 @@ void Being::logic() // Remove it after 1.5 secs if the dead animation isn't long enough, // or simply play it until it's finished. if (!isAlive() && Net::getGameHandler()->removeDeadBeings() && getType() != PLAYER) - if (mActionTimer.elapsed() > std::max(getDuration(), 1500)) + if (mActionTimer.elapsed() > std::max(mSprites.getMaxDuration(), 1500)) actorSpriteManager->scheduleDelete(this); } @@ -1244,7 +1244,7 @@ void Being::updatePlayerSprites() // Set the new sprites bool newSpriteSet = false; - ensureSize(mSpriteStates.size()); + mSprites.ensureSize(mSpriteStates.size()); for (size_t i = 0; i < mSpriteStates.size(); i++) { @@ -1256,7 +1256,7 @@ void Being::updatePlayerSprites() if (spriteState.visibleId == 0) { - CompoundSprite::setSprite(i, nullptr); + mSprites.set(i, nullptr); } else { @@ -1278,7 +1278,7 @@ void Being::updatePlayerSprites() equipmentSprite->setDirection(getSpriteDirection()); } - CompoundSprite::setSprite(i, equipmentSprite); + mSprites.set(i, equipmentSprite); } } @@ -1303,7 +1303,7 @@ void Being::setSprite(unsigned slot, int id, const std::string &color, if (spriteState.color != color && spriteState.visibleId) { spriteState.visibleId = 0; - CompoundSprite::setSprite(slot, nullptr); + mSprites.set(slot, nullptr); } spriteState.id = id; @@ -1343,7 +1343,7 @@ void Being::setSpriteColor(unsigned slot, const std::string &color) bool Being::drawnWhenBehind() const { // For now, just draw actors with only one layer when obscured - return CompoundSprite::getNumberOfLayers() == 1; + return mSprites.getNumberOfLayers() == 1; } void Being::setGender(Gender gender) @@ -1358,7 +1358,7 @@ void Being::setGender(Gender gender) auto &spriteState = mSpriteStates[i]; if (spriteState.visibleId) { - CompoundSprite::setSprite(i, nullptr); + mSprites.set(i, nullptr); spriteState.visibleId = 0; } } diff --git a/src/compoundsprite.cpp b/src/compoundsprite.cpp index cdf6cecd..0ca55098 100644 --- a/src/compoundsprite.cpp +++ b/src/compoundsprite.cpp @@ -29,13 +29,9 @@ #include <SDL.h> -CompoundSprite::CompoundSprite() = default; - CompoundSprite::~CompoundSprite() { delete_all(mSprites); - mSprites.clear(); - delete mImage; delete mAlphaImage; } @@ -110,11 +106,6 @@ bool CompoundSprite::draw(Graphics *graphics, int posX, int posY) const return false; } -const Image *CompoundSprite::getImage() const -{ - return mImage; -} - bool CompoundSprite::setDirection(SpriteDirection direction) { bool ret = false; @@ -135,13 +126,13 @@ int CompoundSprite::getNumberOfLayers() const return size(); } -void CompoundSprite::addSprite(Sprite *sprite) +void CompoundSprite::add(Sprite *sprite) { mSprites.push_back(sprite); mNeedsRedraw = true; } -void CompoundSprite::setSprite(int layer, Sprite *sprite) +void CompoundSprite::set(int layer, Sprite *sprite) { // Skip if it won't change anything if (mSprites.at(layer) == sprite) @@ -178,7 +169,7 @@ void CompoundSprite::doRedraw() redraw(); } -int CompoundSprite::getDuration() const +int CompoundSprite::getMaxDuration() const { int duration = 0; for (auto sprite : mSprites) diff --git a/src/compoundsprite.h b/src/compoundsprite.h index f32e4e6c..cea0b11f 100644 --- a/src/compoundsprite.h +++ b/src/compoundsprite.h @@ -24,61 +24,44 @@ #include <vector> -class Image; - -class CompoundSprite : public Sprite +class CompoundSprite { public: - CompoundSprite(); - - ~CompoundSprite() override; - - bool reset() final; - - bool play(const std::string &action) final; - - bool update(int time) final; + CompoundSprite() = default; + ~CompoundSprite(); - bool draw(Graphics *graphics, int posX, int posY) const override; + bool reset(); + bool play(const std::string &action); + bool update(int time); + bool draw(Graphics *graphics, int posX, int posY) const; /** * Gets the width in pixels of the first sprite in the list. */ - int getWidth() const override - { return mWidth; } + int getWidth() const { return mWidth; } /** * Gets the height in pixels of the first sprite in the list. */ - int getHeight() const override - { return mHeight; } + int getHeight() const { return mHeight; } - int getOffsetX() const final - { return mOffsetX; } + int getOffsetX() const { return mOffsetX; } + int getOffsetY() const { return mOffsetY; } - int getOffsetY() const final - { return mOffsetY; } + float getAlpha() const { return mAlpha; } + void setAlpha(float alpha) { mAlpha = alpha; } - const Image *getImage() const final; - - bool setDirection(SpriteDirection direction) final; + bool setDirection(SpriteDirection direction); int getNumberOfLayers() const; - int getDuration() const final; - - size_t size() const - { return mSprites.size(); } - - void addSprite(Sprite *sprite); + int getMaxDuration() const; - void setSprite(int layer, Sprite *sprite); - - Sprite *getSprite(int layer) const - { return mSprites.at(layer); } + size_t size() const { return mSprites.size(); } + void add(Sprite *sprite); + void set(int layer, Sprite *sprite); void clear(); - void ensureSize(size_t layerCount); void doRedraw(); @@ -94,5 +77,6 @@ private: mutable bool mNeedsRedraw = false; + float mAlpha = 1.0f; std::vector<Sprite*> mSprites; }; |