diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-11-26 14:20:50 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-11-26 14:20:50 +0000 |
commit | 7f819e46b584145e2cf2ea742ced9fab0ffc233e (patch) | |
tree | 9aa8ee99a4b4dd653917155282e82e6b9a64f718 /src | |
parent | cc86266f4b59420a155df8c1aa01b9ce20b6585d (diff) | |
download | mana-7f819e46b584145e2cf2ea742ced9fab0ffc233e.tar.gz mana-7f819e46b584145e2cf2ea742ced9fab0ffc233e.tar.bz2 mana-7f819e46b584145e2cf2ea742ced9fab0ffc233e.tar.xz mana-7f819e46b584145e2cf2ea742ced9fab0ffc233e.zip |
Made Being::mDirection protected, forcing the use of setDirection. Defaulted
variant argument to 0 since this is the most common situation. Some SpriteDef
refactoring, splitting up the loading into several methods, in preparation of
adding support for including other sprites.
Diffstat (limited to 'src')
-rw-r--r-- | src/animatedsprite.h | 2 | ||||
-rw-r--r-- | src/being.cpp | 3 | ||||
-rw-r--r-- | src/being.h | 7 | ||||
-rw-r--r-- | src/game.cpp | 16 | ||||
-rw-r--r-- | src/monster.cpp | 2 | ||||
-rw-r--r-- | src/net/beinghandler.cpp | 10 | ||||
-rw-r--r-- | src/npc.cpp | 3 | ||||
-rw-r--r-- | src/player.cpp | 26 | ||||
-rw-r--r-- | src/resources/resourcemanager.h | 2 | ||||
-rw-r--r-- | src/resources/spritedef.cpp | 282 | ||||
-rw-r--r-- | src/resources/spritedef.h | 28 |
11 files changed, 236 insertions, 145 deletions
diff --git a/src/animatedsprite.h b/src/animatedsprite.h index e5a435f9..dde570e2 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -51,7 +51,7 @@ class AnimatedSprite * @param filename the file of the sprite to animate * @param variant the sprite variant */ - AnimatedSprite(const std::string& filename, int variant); + AnimatedSprite(const std::string& filename, int variant = 0); /** * Destructor. diff --git a/src/being.cpp b/src/being.cpp index 37552df3..c47ce90f 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -47,7 +47,7 @@ PATH_NODE::PATH_NODE(Uint16 iX, Uint16 iY): Being::Being(Uint32 id, Uint16 job, Map *map): mJob(job), - mX(0), mY(0), mDirection(DOWN), + mX(0), mY(0), mAction(0), mWalkTime(0), mEmotion(0), mEmotionTime(0), @@ -56,6 +56,7 @@ Being::Being(Uint32 id, Uint16 job, Map *map): mId(id), mWeapon(0), mWalkSpeed(150), + mDirection(DOWN), mMap(NULL), mHairStyle(0), mHairColor(0), mSex(2), diff --git a/src/being.h b/src/being.h index 3c497b75..81f7bc07 100644 --- a/src/being.h +++ b/src/being.h @@ -100,7 +100,6 @@ class Being : public Sprite Uint16 mJob; /**< Job (player job, npc, monster, ) */ Uint16 mX, mY; /**< Tile coordinates */ - Uint8 mDirection; /**< Facing direction */ Uint8 mAction; /**< Action the being is performing */ Uint8 mFrame; Uint16 mWalkTime; @@ -294,6 +293,11 @@ class Being : public Sprite void setAction(Uint8 action); /** + * Returns the current direction. + */ + Uint8 getDirection() const { return mDirection; } + + /** * Sets the current direction. */ void setDirection(Uint8 direction); @@ -356,6 +360,7 @@ class Being : public Sprite Uint32 mId; /**< Unique sprite id */ Uint16 mWeapon; /**< Weapon picture id */ Uint16 mWalkSpeed; /**< Walking speed */ + Uint8 mDirection; /**< Facing direction */ Map *mMap; /**< Map on which this being resides */ std::string mName; /**< Name of character */ SpriteIterator mSpriteIterator; diff --git a/src/game.cpp b/src/game.cpp index 7da80069..6bc37365 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -518,13 +518,13 @@ void Game::handleInput() if (!item) { Uint16 x = player_node->mX; Uint16 y = player_node->mY; - if (player_node->mDirection & Being::UP) + if (player_node->getDirection() & Being::UP) y--; - if (player_node->mDirection & Being::DOWN) + if (player_node->getDirection() & Being::DOWN) y++; - if (player_node->mDirection & Being::LEFT) + if (player_node->getDirection() & Being::LEFT) x--; - if (player_node->mDirection & Being::RIGHT) + if (player_node->getDirection() & Being::RIGHT) x++; item = floorItemManager->findByCoordinates(x, y); @@ -692,13 +692,13 @@ void Game::handleInput() { Uint16 targetX = x, targetY = y; - if (player_node->mDirection & Being::UP) + if (player_node->getDirection() & Being::UP) targetY--; - if (player_node->mDirection & Being::DOWN) + if (player_node->getDirection() & Being::DOWN) targetY++; - if (player_node->mDirection & Being::LEFT) + if (player_node->getDirection() & Being::LEFT) targetX--; - if (player_node->mDirection & Being::RIGHT) + if (player_node->getDirection() & Being::RIGHT) targetX++; // Attack priorioty is: Monster, Player, auto target diff --git a/src/monster.cpp b/src/monster.cpp index ce073a04..7fffd34e 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -33,7 +33,7 @@ Monster::Monster(Uint32 id, Uint16 job, Map *map): Being(id, job, map) { mSprites[BASE_SPRITE] = new AnimatedSprite( - "graphics/sprites/monster" + toString(job - 1002) + ".xml", 0); + "graphics/sprites/monster" + toString(job - 1002) + ".xml"); } void diff --git a/src/net/beinghandler.cpp b/src/net/beinghandler.cpp index 85760112..4fcf2105 100644 --- a/src/net/beinghandler.cpp +++ b/src/net/beinghandler.cpp @@ -138,8 +138,9 @@ void BeingHandler::handleMessage(MessageIn *msg) } else { - msg->readCoordinates(dstBeing->mX, dstBeing->mY, - dstBeing->mDirection); + Uint8 dir; + msg->readCoordinates(dstBeing->mX, dstBeing->mY, dir); + dstBeing->setDirection(dir); } msg->readInt8(); // unknown @@ -342,8 +343,9 @@ void BeingHandler::handleMessage(MessageIn *msg) } else { - msg->readCoordinates(dstBeing->mX, dstBeing->mY, - dstBeing->mDirection); + Uint8 dir; + msg->readCoordinates(dstBeing->mX, dstBeing->mY, dir); + dstBeing->setDirection(dir); } msg->readInt8(); // unknown diff --git a/src/npc.cpp b/src/npc.cpp index 6fa5ac37..d2e3f93f 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -36,7 +36,8 @@ NPC *current_npc = 0; NPC::NPC(Uint32 id, Uint16 job, Map *map, Network *network): Being(id, job, map), mNetwork(network) { - mSprites[BASE_SPRITE] = new AnimatedSprite("graphics/sprites/npc.xml", job-100); + mSprites[BASE_SPRITE] = new AnimatedSprite("graphics/sprites/npc.xml", + job - 100); } Being::Type diff --git a/src/player.cpp b/src/player.cpp index d6bd42f1..cd6986b3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -94,12 +94,12 @@ Player::setSex(Uint8 sex) if (sex == 0) { mSprites[BASE_SPRITE] = new AnimatedSprite( - "graphics/sprites/player_male_base.xml", 0); + "graphics/sprites/player_male_base.xml"); } else { mSprites[BASE_SPRITE] = new AnimatedSprite( - "graphics/sprites/player_female_base.xml", 0); + "graphics/sprites/player_female_base.xml"); } //reload equipment @@ -109,8 +109,8 @@ Player::setSex(Uint8 sex) { delete mSprites[i]; mSprites[i] = new AnimatedSprite( - "graphics/sprites/" + EquipmentDB::get(mEquipmentSpriteIDs.at(i))->getSprite(sex), - 0); + "graphics/sprites/" + EquipmentDB::get( + mEquipmentSpriteIDs.at(i))->getSprite(sex)); } } } @@ -130,16 +130,20 @@ Player::setWeapon(Uint16 weapon) switch (weapon) { case 0: - mSprites[WEAPON_SPRITE] = new AnimatedSprite("graphics/sprites/weapon-fist.xml", 0); + mSprites[WEAPON_SPRITE] = + new AnimatedSprite("graphics/sprites/weapon-fist.xml"); break; case 1: - mSprites[WEAPON_SPRITE] = new AnimatedSprite("graphics/sprites/weapon-dagger.xml", 0); + mSprites[WEAPON_SPRITE] = + new AnimatedSprite("graphics/sprites/weapon-dagger.xml"); break; case 2: - mSprites[WEAPON_SPRITE] = new AnimatedSprite("graphics/sprites/weapon-bow.xml", 0); + mSprites[WEAPON_SPRITE] = + new AnimatedSprite("graphics/sprites/weapon-bow.xml"); break; case 3: - mSprites[WEAPON_SPRITE] = new AnimatedSprite("graphics/sprites/weapon-scythe.xml", 0); + mSprites[WEAPON_SPRITE] = + new AnimatedSprite("graphics/sprites/weapon-scythe.xml"); break; } } @@ -215,13 +219,11 @@ Player::setVisibleEquipment(Uint8 slot, int id) if (mSex == 0) { equipmentSprite = new AnimatedSprite( - "graphics/sprites/" + EquipmentDB::get(id)->getSprite(0), - 0); + "graphics/sprites/" + EquipmentDB::get(id)->getSprite(0)); } else { equipmentSprite = new AnimatedSprite( - "graphics/sprites/" + EquipmentDB::get(id)->getSprite(1), - 0); + "graphics/sprites/" + EquipmentDB::get(id)->getSprite(1)); } equipmentSprite->setDirection(getSpriteDirection()); diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 4367cd5e..d458f96e 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -146,7 +146,7 @@ class ResourceManager * variant. */ SpriteDef* - getSprite(const std::string &path, int variant); + getSprite(const std::string &path, int variant = 0); /** * Releases a resource, removing it from the set of loaded resources. diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index 21eae6e0..bd273b3b 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -85,151 +85,202 @@ SpriteDef::load(const std::string &animationFile, int variant) // Get the variant int variant_num = XML::getProperty(node, "variants", 0); - int variant_offset = XML::getProperty(node, "variant_offset", 0); + int variant_offset = 0; - if (variant_num > 0 && variant < variant_num ) { - variant_offset *= variant; - } else { - variant_offset = 0; + if (variant_num > 0 && variant < variant_num) + { + variant_offset = variant * XML::getProperty(node, "variant_offset", 0); } for (node = node->xmlChildrenNode; node != NULL; node = node->next) { if (xmlStrEqual(node->name, BAD_CAST "imageset")) { - int width = XML::getProperty(node, "width", 0); - int height = XML::getProperty(node, "height", 0); - std::string name = XML::getProperty(node, "name", ""); - std::string imageSrc = XML::getProperty(node, "src", ""); + loadImageSet(node); + } + else if (xmlStrEqual(node->name, BAD_CAST "action")) + { + loadAction(node, variant_offset); + } + else if (xmlStrEqual(node->name, BAD_CAST "include")) + { + includeSprite(node); + } + } - Spriteset *spriteset = - resman->getSpriteset(imageSrc, width, height); + xmlFreeDoc(doc); - if (!spriteset) { - logger->error("Couldn't load spriteset!"); - } + // Complete missing actions + substituteAction(ACTION_STAND, ACTION_DEFAULT); + 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); +} - mSpritesets[name] = spriteset; - } - // get action - else if (xmlStrEqual(node->name, BAD_CAST "action")) +void +SpriteDef::loadImageSet(xmlNodePtr node) +{ + int width = XML::getProperty(node, "width", 0); + int height = XML::getProperty(node, "height", 0); + std::string name = XML::getProperty(node, "name", ""); + std::string imageSrc = XML::getProperty(node, "src", ""); + + ResourceManager *resman = ResourceManager::getInstance(); + Spriteset *spriteset = resman->getSpriteset(imageSrc, width, height); + + if (!spriteset) + { + logger->error("Couldn't load imageset!"); + } + + mSpritesets[name] = spriteset; +} + +void +SpriteDef::loadAction(xmlNodePtr node, int variant_offset) +{ + const std::string actionName = XML::getProperty(node, "name", ""); + const std::string imagesetName = XML::getProperty(node, "imageset", ""); + + SpritesetIterator si = mSpritesets.find(imagesetName); + if (si == mSpritesets.end()) + { + logger->log("Warning: imageset \"%s\" not defined in %s", + imagesetName.c_str(), getIdPath().c_str()); + return; + } + Spriteset *imageset = si->second; + + SpriteAction actionType = makeSpriteAction(actionName); + if (actionType == ACTION_INVALID) + { + logger->log("Warning: Unknown action \"%s\" defined in %s", + actionName.c_str(), getIdPath().c_str()); + return; + } + Action *action = new Action(); + mActions[actionType] = action; + + // When first action set it as default direction + if (mActions.empty()) + { + mActions[ACTION_DEFAULT] = action; + } + + // Load animations + for (xmlNodePtr animationNode = node->xmlChildrenNode; + animationNode != NULL; + animationNode = animationNode->next) + { + if (xmlStrEqual(animationNode->name, BAD_CAST "animation")) { - std::string actionName = XML::getProperty(node, "name", ""); - std::string imagesetName = XML::getProperty(node, "imageset", ""); + loadAnimation(animationNode, action, imageset, variant_offset); + } + } +} - SpritesetIterator si = mSpritesets.find(imagesetName); - if (si == mSpritesets.end()) { - logger->log("Warning: imageset \"%s\" not defined in %s", - imagesetName.c_str(), - animationFile.c_str()); +void +SpriteDef::loadAnimation(xmlNodePtr animationNode, + Action *action, Spriteset *imageset, + int variant_offset) +{ + std::string directionName = + XML::getProperty(animationNode, "direction", ""); + SpriteDirection directionType = makeSpriteDirection(directionName); - // skip loading animations - continue; - } - Spriteset *imageset = si->second; + if (directionType == DIRECTION_INVALID) + { + logger->log("Warning: Unknown direction \"%s\" used in %s", + directionName.c_str(), getIdPath().c_str()); + return; + } + + Animation *animation = new Animation(); + action->setAnimation(directionType, animation); + + // Get animation phases + for (xmlNodePtr phaseNode = animationNode->xmlChildrenNode; + phaseNode != NULL; + phaseNode = phaseNode->next) + { + int delay = XML::getProperty(phaseNode, "delay", 0); + int offsetX = XML::getProperty(phaseNode, "offsetX", 0); + int offsetY = XML::getProperty(phaseNode, "offsetY", 0); + offsetY -= imageset->getHeight() - 32; + offsetX -= imageset->getWidth() / 2 - 16; - SpriteAction actionType = makeSpriteAction(actionName); - if (actionType == ACTION_INVALID) + if (xmlStrEqual(phaseNode->name, BAD_CAST "frame")) + { + int index = XML::getProperty(phaseNode, "index", -1); + + if (index < 0) { - logger->log("Warning: Unknown action \"%s\" defined in %s", - actionName.c_str(), - animationFile.c_str()); + logger->log("No valid value for 'index'"); continue; } - Action *action = new Action(); - mActions[actionType] = action; - // When first action set it as default direction - if (mActions.empty()) + Image *img = imageset->get(index + variant_offset); + + if (!img) { - mActions[ACTION_DEFAULT] = action; + logger->log("No image at index " + (index + variant_offset)); + continue; } + animation->addPhase(img, delay, offsetX, offsetY); + } + else if (xmlStrEqual(phaseNode->name, BAD_CAST "sequence")) + { + int start = XML::getProperty(phaseNode, "start", -1); + int end = XML::getProperty(phaseNode, "end", -1); - // get animations - for (xmlNodePtr animationNode = node->xmlChildrenNode; - animationNode != NULL; - animationNode = animationNode->next) + if (start < 0 || end < 0) { - // We're only interested in animations - if (!xmlStrEqual(animationNode->name, BAD_CAST "animation")) - continue; + logger->log("No valid value for 'start' or 'end'"); + continue; + } - std::string directionName = - XML::getProperty(animationNode, "direction", ""); - SpriteDirection directionType = - makeSpriteDirection(directionName); + while (end >= start) + { + Image *img = imageset->get(start + variant_offset); - if (directionType == DIRECTION_INVALID) + if (!img) { - logger->log("Warning: Unknown direction \"%s\" defined " - "for action %s in %s", - directionName.c_str(), - actionName.c_str(), - animationFile.c_str()); + logger->log("No image at index " + + (start + variant_offset)); continue; } - Animation *animation = new Animation(); - action->setAnimation(directionType, animation); - - // Get animation phases - for (xmlNodePtr phaseNode = animationNode->xmlChildrenNode; - phaseNode != NULL; - phaseNode = phaseNode->next) - { - int delay = XML::getProperty(phaseNode, "delay", 0); - - if (xmlStrEqual(phaseNode->name, BAD_CAST "frame")) - { - int index = XML::getProperty(phaseNode, "index", -1); - int offsetX = XML::getProperty(phaseNode, "offsetX", 0); - int offsetY = XML::getProperty(phaseNode, "offsetY", 0); - - offsetY -= imageset->getHeight() - 32; - offsetX -= imageset->getWidth() / 2 - 16; - Image *img = imageset->get(index + variant_offset); - animation->addPhase(img, delay, offsetX, offsetY); - } - else if (xmlStrEqual(phaseNode->name, BAD_CAST "sequence")) - { - int start = XML::getProperty(phaseNode, "start", 0); - int end = XML::getProperty(phaseNode, "end", 0); - int offsetY = -imageset->getHeight() + 32; - int offsetX = -imageset->getWidth() / 2 + 16; - while (end >= start) - { - Image *img = imageset->get(start + variant_offset); - animation->addPhase(img, delay, offsetX, offsetY); - start++; - } - } - else if (xmlStrEqual(phaseNode->name, BAD_CAST "end")) - { - animation->addTerminator(); - } - } // for phaseNode - } // for animationNode - } // if "<imageset>" else if "<action>" - } // for node + animation->addPhase(img, delay, offsetX, offsetY); + start++; + } + } + else if (xmlStrEqual(phaseNode->name, BAD_CAST "end")) + { + animation->addTerminator(); + } + } // for phaseNode +} - // Complete missing actions - substituteAction(ACTION_STAND, ACTION_DEFAULT); - 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); +void +SpriteDef::includeSprite(xmlNodePtr includeNode) +{ + std::string filename = XML::getProperty(includeNode, "file", ""); + ResourceManager *resman = ResourceManager::getInstance(); + SpriteDef *sprite = resman->getSprite("graphics/sprites/" + filename); - xmlFreeDoc(doc); + // TODO: Somehow implement actually including it + sprite->decRef(); } void @@ -246,7 +297,8 @@ SpriteDef::substituteAction(SpriteAction complete, SpriteAction with) SpriteDef::~SpriteDef() { - for (SpritesetIterator i = mSpritesets.begin(); i != mSpritesets.end(); ++i) + for (SpritesetIterator i = mSpritesets.begin(); + i != mSpritesets.end(); ++i) { i->second->decRef(); } diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 51c47a24..64414259 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -29,6 +29,8 @@ #include <map> #include <string> +#include <libxml/tree.h> + class Action; class Graphics; class Spriteset; @@ -96,6 +98,32 @@ class SpriteDef : public Resource load(const std::string &file, int variant); /** + * Loads an imageset element. + */ + void + loadImageSet(xmlNodePtr node); + + /** + * Loads an action element. + */ + void + loadAction(xmlNodePtr node, int variant_offset); + + /** + * Loads an animation element. + */ + void + loadAnimation(xmlNodePtr animationNode, + Action *action, Spriteset *imageset, + int variant_offset); + + /** + * Include another sprite into this one. + */ + void + includeSprite(xmlNodePtr includeNode); + + /** * When there are no animations defined for the action "complete", its * animations become a copy of those of the action "with". */ |