summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJared Adams <jaxad0127@gmail.com>2010-05-09 14:31:43 -0600
committerJared Adams <jaxad0127@gmail.com>2010-05-09 15:09:56 -0600
commitf269d5a68e6a22676c49961c7529b9c00dc90649 (patch)
tree3ccce636203883c0009146099df9d4854cab2171 /src
parent9baedc27191c82bbf1fedee2a7e738bc5b267c0e (diff)
downloadmana-client-f269d5a68e6a22676c49961c7529b9c00dc90649.tar.gz
mana-client-f269d5a68e6a22676c49961c7529b9c00dc90649.tar.bz2
mana-client-f269d5a68e6a22676c49961c7529b9c00dc90649.tar.xz
mana-client-f269d5a68e6a22676c49961c7529b9c00dc90649.zip
Move target cursor management into ActorSprite
This simplifies handling of target cursors, centralizing their logic into a single class, instead of two. Also make them more flexible and move the image files outside of the theme so servers can can control them and give them better names. Reviewed-by: Thorbjørn Lindeijer
Diffstat (limited to 'src')
-rw-r--r--src/actorsprite.cpp121
-rw-r--r--src/actorsprite.h34
-rw-r--r--src/client.cpp23
-rw-r--r--src/game.cpp2
-rw-r--r--src/localplayer.cpp58
-rw-r--r--src/localplayer.h16
6 files changed, 170 insertions, 84 deletions
diff --git a/src/actorsprite.cpp b/src/actorsprite.cpp
index ad703bc2..da81873f 100644
--- a/src/actorsprite.cpp
+++ b/src/actorsprite.cpp
@@ -34,10 +34,16 @@
#include "net/net.h"
#include "resources/image.h"
+#include "resources/imageset.h"
#include "resources/resourcemanager.h"
+#include <cassert>
+
#define EFFECTS_FILE "effects.xml"
+ImageSet *ActorSprite::targetCursorImages[2][NUM_TC];
+SimpleAnimation *ActorSprite::targetCursor[2][NUM_TC];
+
ActorSprite::ActorSprite(int id):
mId(id),
mStunMode(0),
@@ -83,9 +89,6 @@ bool ActorSprite::drawSpriteAt(Graphics *graphics, int x, int y) const
void ActorSprite::logic()
{
// Update sprite animations
- if (mUsedTargetCursor)
- mUsedTargetCursor->update(tick_time * MILLISECONDS_IN_A_TICK);
-
update(tick_time * MILLISECONDS_IN_A_TICK);
// Restart status/particle effects, if needed
@@ -105,6 +108,18 @@ void ActorSprite::logic()
mChildParticleEffects.moveTo(mPos.x, mPos.y);
}
+void ActorSprite::actorLogic()
+{
+ // Update sprite animations
+ for (int size = TC_SMALL; size < NUM_TC; size++)
+ {
+ for (int type = TCT_NORMAL; type < NUM_TCT; type++)
+ {
+ targetCursor[type][size]->update(tick_time * MILLISECONDS_IN_A_TICK);
+ }
+ }
+}
+
void ActorSprite::setMap(Map* map)
{
Actor::setMap(map);
@@ -119,10 +134,12 @@ void ActorSprite::controlParticle(Particle *particle)
mChildParticleEffects.addLocally(particle);
}
-void ActorSprite::setTargetAnimation(SimpleAnimation *animation)
+void ActorSprite::setTargetType(TargetCursorType type)
{
- mUsedTargetCursor = animation;
- mUsedTargetCursor->reset();
+ if (type == TCT_NONE)
+ untarget();
+ else
+ mUsedTargetCursor = targetCursor[type][getTargetCursorSize()];
}
struct EffectDescription {
@@ -324,3 +341,95 @@ void ActorSprite::setupSpriteDisplay(const SpriteDisplay &display,
mMustResetParticles = true;
}
+
+void ActorSprite::load()
+{
+ initTargetCursor();
+}
+
+void ActorSprite::unload()
+{
+ cleanupTargetCursors();
+}
+
+static const char *cursorType(int type)
+{
+ switch (type)
+ {
+ case ActorSprite::TCT_IN_RANGE:
+ return "in-range";
+ case ActorSprite::TCT_NORMAL:
+ return "normal";
+ default:
+ assert(false);
+ }
+}
+
+static const char *cursorSize(int size)
+{
+ switch (size)
+ {
+ case ActorSprite::TC_LARGE:
+ return "l";
+ case ActorSprite::TC_MEDIUM:
+ return "m";
+ case ActorSprite::TC_SMALL:
+ return "s";
+ default:
+ assert(false);
+ }
+}
+
+void ActorSprite::initTargetCursor()
+{
+ static std::string targetCursor = "graphics/target-cursor-%s-%s.png";
+ static int targetWidths[NUM_TC] = {44, 62, 82};
+ static int targetHeights[NUM_TC] = {35, 44, 60};
+
+ // Load target cursors
+ for (int size = TC_SMALL; size < NUM_TC; size++)
+ {
+ for (int type = TCT_NORMAL; type < NUM_TCT; type++)
+ {
+ loadTargetCursor(strprintf(targetCursor.c_str(), cursorType(type),
+ cursorSize(size)), targetWidths[size],
+ targetHeights[size], type, size);
+ }
+ }
+}
+
+void ActorSprite::cleanupTargetCursors()
+{
+ for (int size = TC_SMALL; size < NUM_TC; size++)
+ {
+ for (int type = TCT_NORMAL; type < NUM_TCT; type++)
+ {
+ delete targetCursor[type][size];
+ targetCursorImages[type][size]->decRef();
+ }
+ }
+}
+
+void ActorSprite::loadTargetCursor(const std::string &filename,
+ int width, int height, int type, int size)
+{
+ assert(size > -1);
+ assert(size < 3);
+
+ ResourceManager *resman = ResourceManager::getInstance();
+ ImageSet *currentImageSet = resman->getImageSet(filename, width, height);
+
+ Animation *anim = new Animation;
+
+ for (unsigned int i = 0; i < currentImageSet->size(); ++i)
+ {
+ anim->addFrame(currentImageSet->get(i), 750,
+ (16 - (currentImageSet->getWidth() / 2)),
+ (16 - (currentImageSet->getHeight() / 2)));
+ }
+
+ SimpleAnimation *currentCursor = new SimpleAnimation(anim);
+
+ targetCursorImages[type][size] = currentImageSet;
+ targetCursor[type][size] = currentCursor;
+}
diff --git a/src/actorsprite.h b/src/actorsprite.h
index b5c402eb..a56be125 100644
--- a/src/actorsprite.h
+++ b/src/actorsprite.h
@@ -53,6 +53,14 @@ public:
NUM_TC
};
+ enum TargetCursorType
+ {
+ TCT_NONE = -1,
+ TCT_NORMAL = 0,
+ TCT_IN_RANGE,
+ NUM_TCT
+ };
+
ActorSprite(int id);
~ActorSprite();
@@ -73,6 +81,8 @@ public:
virtual void logic();
+ static void actorLogic();
+
void setMap(Map* map);
/**
@@ -95,7 +105,7 @@ public:
/**
* Sets the target animation for this actor.
*/
- void setTargetAnimation(SimpleAnimation *animation);
+ void setTargetType(TargetCursorType type);
/**
* Untargets the actor.
@@ -140,6 +150,10 @@ public:
virtual float getAlpha() const
{ return CompoundSprite::getAlpha(); }
+ static void load();
+
+ static void unload();
+
protected:
/**
* Trigger visual effect, with components
@@ -185,6 +199,24 @@ private:
/** Reset particle status effects on next redraw? */
bool mMustResetParticles;
+ /** Load the target cursors into memory */
+ static void initTargetCursor();
+
+ /** Remove the target cursors from memory */
+ static void cleanupTargetCursors();
+
+ /**
+ * Helper function for loading target cursors
+ */
+ static void loadTargetCursor(const std::string &filename,
+ int width, int height, int type, int size);
+
+ /** Images of the target cursor. */
+ static ImageSet *targetCursorImages[NUM_TCT][NUM_TC];
+
+ /** Animated target cursors. */
+ static SimpleAnimation *targetCursor[NUM_TCT][NUM_TC];
+
/** Target cursor being used */
SimpleAnimation *mUsedTargetCursor;
};
diff --git a/src/client.cpp b/src/client.cpp
index afc1ae1d..137210b1 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -446,14 +446,6 @@ Client::~Client()
// Shutdown sound
sound.close();
- // Unload XML databases
- ColorDB::unload();
- EmoteDB::unload();
- ItemDB::unload();
- MonsterDB::unload();
- NPCDB::unload();
- StatusEffect::unload();
-
ResourceManager::deleteInstance();
SDL_FreeSurface(mIcon);
@@ -594,6 +586,19 @@ int Client::exec()
{
delete game;
game = 0;
+
+ if (mState != STATE_CHANGE_MAP)
+ {
+ // Unload XML databases
+ ColorDB::unload();
+ EmoteDB::unload();
+ ItemDB::unload();
+ MonsterDB::unload();
+ NPCDB::unload();
+ StatusEffect::unload();
+
+ ActorSprite::unload();
+ }
}
mOldState = mState;
@@ -748,6 +753,8 @@ int Client::exec()
StatusEffect::load();
Units::loadUnits();
+ ActorSprite::load();
+
mDesktop->reloadWallpaper();
mState = STATE_GET_CHARACTERS;
diff --git a/src/game.cpp b/src/game.cpp
index 74997b90..b75d3cdf 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -21,6 +21,7 @@
#include "game.h"
+#include "actorsprite.h"
#include "beingmanager.h"
#include "channelmanager.h"
#include "client.h"
@@ -334,6 +335,7 @@ void Game::logic()
handleInput();
// Handle all necessary game logic
+ ActorSprite::actorLogic();
beingManager->logic();
floorItemManager->logic();
particleEngine->update();
diff --git a/src/localplayer.cpp b/src/localplayer.cpp
index 19ddec51..9796b2ac 100644
--- a/src/localplayer.cpp
+++ b/src/localplayer.cpp
@@ -116,8 +116,6 @@ LocalPlayer::LocalPlayer(int id, int subtype):
mTextColor = &Theme::getThemeColor(Theme::PLAYER);
mNameColor = &userPalette->getColor(UserPalette::SELF);
- initTargetCursor();
-
config.addListener("showownname", this);
setShowName(config.getValue("showownname", 1));
}
@@ -127,14 +125,6 @@ LocalPlayer::~LocalPlayer()
delete mInventory;
config.removeListener("showownname", this);
-
- for (int i = Being::TC_SMALL; i < Being::NUM_TC; i++)
- {
- delete mTargetCursor[0][i];
- delete mTargetCursor[1][i];
- mTargetCursorImages[0][i]->decRef();
- mTargetCursorImages[1][i]->decRef();
- }
}
void LocalPlayer::logic()
@@ -200,8 +190,7 @@ void LocalPlayer::logic()
if (mTarget->getType() == Being::NPC)
{
// NPCs are always in range
- mTarget->setTargetAnimation(
- mTargetCursor[0][mTarget->getTargetCursorSize()]);
+ mTarget->setTargetType(TCT_IN_RANGE);
}
else
{
@@ -217,10 +206,10 @@ void LocalPlayer::logic()
abs(mTarget->getTileY() - getTileY());
const int attackRange = getAttackRange();
- const int inRange = rangeX > attackRange || rangeY > attackRange
- ? 1 : 0;
- mTarget->setTargetAnimation(
- mTargetCursor[inRange][mTarget->getTargetCursorSize()]);
+ const TargetCursorType targetType = rangeX > attackRange ||
+ rangeY > attackRange ?
+ TCT_NORMAL : TCT_IN_RANGE;
+ mTarget->setTargetType(targetType);
if (!mTarget->isAlive())
stopAttack();
@@ -1374,43 +1363,6 @@ void LocalPlayer::handleStatusEffect(StatusEffect *effect, int effectId)
}
}
-void LocalPlayer::initTargetCursor()
-{
- // Load target cursors
- loadTargetCursor("target-cursor-blue-s.png", 44, 35, false, TC_SMALL);
- loadTargetCursor("target-cursor-red-s.png", 44, 35, true, TC_SMALL);
- loadTargetCursor("target-cursor-blue-m.png", 62, 44, false, TC_MEDIUM);
- loadTargetCursor("target-cursor-red-m.png", 62, 44, true, TC_MEDIUM);
- loadTargetCursor("target-cursor-blue-l.png", 82, 60, false, TC_LARGE);
- loadTargetCursor("target-cursor-red-l.png", 82, 60, true, TC_LARGE);
-}
-
-void LocalPlayer::loadTargetCursor(const std::string &filename,
- int width, int height,
- bool outRange, TargetCursorSize size)
-{
- assert(size > -1);
- assert(size < 3);
-
- ImageSet *currentImageSet = Theme::getImageSetFromTheme(filename,
- width, height);
- Animation *anim = new Animation;
-
- for (unsigned int i = 0; i < currentImageSet->size(); ++i)
- {
- anim->addFrame(currentImageSet->get(i), 75,
- (16 - (currentImageSet->getWidth() / 2)),
- (16 - (currentImageSet->getHeight() / 2)));
- }
-
- SimpleAnimation *currentCursor = new SimpleAnimation(anim);
-
- const int index = outRange ? 1 : 0;
-
- mTargetCursorImages[index][size] = currentImageSet;
- mTargetCursor[index][size] = currentCursor;
-}
-
void LocalPlayer::addMessageToQueue(const std::string &message, int color)
{
mMessages.push_back(MessagePair(message, color));
diff --git a/src/localplayer.h b/src/localplayer.h
index 2c06dfb5..38212a60 100644
--- a/src/localplayer.h
+++ b/src/localplayer.h
@@ -479,22 +479,6 @@ class LocalPlayer : public Player
int mLocalWalkTime; /**< Timestamp used to control keyboard walk
messages flooding */
- /** Load the target cursors into memory */
- void initTargetCursor();
-
- /**
- * Helper function for loading target cursors
- */
- void loadTargetCursor(const std::string &filename,
- int width, int height,
- bool outRange, Being::TargetCursorSize size);
-
- /** Images of the target cursor. */
- ImageSet *mTargetCursorImages[2][NUM_TC];
-
- /** Animated target cursors. */
- SimpleAnimation *mTargetCursor[2][NUM_TC];
-
typedef std::pair<std::string, int> MessagePair;
/** Queued exp messages*/
std::list<MessagePair> mMessages;