diff options
author | Philipp Sehmisch <crush@themanaworld.org> | 2009-01-06 14:33:20 +0100 |
---|---|---|
committer | Ira Rice <irarice@gmail.com> | 2009-01-06 10:48:20 -0700 |
commit | 394989d94081cbc462ba7d567fc88d0c4dff8134 (patch) | |
tree | 49c43dd2bcceb0cf204b13e7ed88132623910d2c /src/resources | |
parent | 3e40c30cdd3a7b3942131fc8b72b268aa0e98f77 (diff) | |
download | mana-394989d94081cbc462ba7d567fc88d0c4dff8134.tar.gz mana-394989d94081cbc462ba7d567fc88d0c4dff8134.tar.bz2 mana-394989d94081cbc462ba7d567fc88d0c4dff8134.tar.xz mana-394989d94081cbc462ba7d567fc88d0c4dff8134.zip |
Added support for animated tiles.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/mapreader.cpp | 88 |
1 files changed, 65 insertions, 23 deletions
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index c6b6d1cd..04019d32 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -300,7 +300,8 @@ void MapReader::readProperties(xmlNodePtr node, Properties *props) static void setTile(Map *map, MapLayer *layer, int x, int y, int gid) { const Tileset * const set = map->getTilesetWithGid(gid); - if (layer) { + if (layer) + { // Set regular tile on a layer Image * const img = set ? set->get(gid - set->getFirstGid()) : 0; layer->setTile(x, y, img); @@ -403,6 +404,12 @@ void MapReader::readLayer(xmlNodePtr node, Map *map) setTile(map, layer, x, y, gid); + TileAnimation* ani = map->getAnimationForGid(gid); + if (ani) + { + ani->addAffectedTile(layer, x + y * w); + } + x++; if (x == w) { x = 0; y++; @@ -450,6 +457,7 @@ Tileset *MapReader::readTileset(xmlNodePtr node, { int firstGid = XML::getProperty(node, "firstgid", 0); XML::Document* doc = NULL; + Tileset *set = NULL; if (xmlHasProp(node, BAD_CAST "source")) { @@ -466,37 +474,71 @@ Tileset *MapReader::readTileset(xmlNodePtr node, for_each_xml_child_node(childNode, node) { - if (!xmlStrEqual(childNode->name, BAD_CAST "image")) - continue; + if (xmlStrEqual(childNode->name, BAD_CAST "image")) + { + const std::string source = XML::getProperty(childNode, "source", ""); + + if (!source.empty()) + { + std::string sourceStr = source; + sourceStr.erase(0, 3); // Remove "../" - const std::string source = XML::getProperty(childNode, "source", ""); + ResourceManager *resman = ResourceManager::getInstance(); + Image* tilebmp = resman->getImage(sourceStr); - if (!source.empty()) + if (tilebmp) + { + set = new Tileset(tilebmp, tw, th, firstGid); + tilebmp->decRef(); + } + else { + logger->log("Warning: Failed to load tileset (%s)", + source.c_str()); + } + } + } + else if (xmlStrEqual(childNode->name, BAD_CAST "tile")) { - std::string sourceStr = source; - while (sourceStr.substr(0, 3) == "../") - sourceStr.erase(0, 3); // Remove "../" + for_each_xml_child_node(tileNode, childNode) + { + if (!xmlStrEqual(tileNode->name, BAD_CAST "properties")) continue; - ResourceManager *resman = ResourceManager::getInstance(); - Image* tilebmp = resman->getImage(sourceStr); + int tileGID = firstGid + XML::getProperty(childNode, "id", 0); - if (tilebmp) - { - Tileset *set = new Tileset(tilebmp, tw, th, firstGid); - tilebmp->decRef(); - delete doc; - return set; - } - else { - logger->log("Warning: Failed to load tileset (%s)", source.c_str()); + // read tile properties to a map for simpler handling + std::map<std::string, int> tileProperties; + for_each_xml_child_node(propertyNode, tileNode) + { + if (!xmlStrEqual(propertyNode->name, BAD_CAST "property")) continue; + std::string name = XML::getProperty(propertyNode, "name", ""); + int value = XML::getProperty(propertyNode, "value", 0); + tileProperties[name] = value; + logger->log("Tile Prop of %d \"%s\" = \"%d\"", tileGID, name.c_str(), value); + } + + // create animation + if (!set) continue; + + Animation *ani = new Animation(); + for (int i = 0; ;i++) + { + std::map<std::string, int>::iterator iFrame, iDelay; + iFrame = tileProperties.find("animation-frame" + toString(i)); + iDelay = tileProperties.find("animation-delay" + toString(i)); + if (iFrame != tileProperties.end() && iDelay != tileProperties.end()) + { + ani->addFrame(set->get(iFrame->second), iDelay->second, 0, 0); + } else { + break; + } + } + map->addAnimation(tileGID, new TileAnimation(ani)); + logger->log("Animation length: %d", ani->getLength()); } } - - // Only one image element expected - break; } delete doc; - return NULL; + return set; } |