diff options
author | Philipp Sehmisch <crush@themanaworld.org> | 2009-01-06 14:33:20 +0100 |
---|---|---|
committer | Philipp Sehmisch <crush@themanaworld.org> | 2009-01-06 16:40:46 +0100 |
commit | 6b60673b0805fbab747924d1a0b6d9c66fc96f64 (patch) | |
tree | e40cc55a4d952bfb9bc89a41c3205c1d7bd90616 /src/resources/mapreader.cpp | |
parent | 090b4893a57a1b7e9c83f7db043843d35016fcff (diff) | |
download | mana-6b60673b0805fbab747924d1a0b6d9c66fc96f64.tar.gz mana-6b60673b0805fbab747924d1a0b6d9c66fc96f64.tar.bz2 mana-6b60673b0805fbab747924d1a0b6d9c66fc96f64.tar.xz mana-6b60673b0805fbab747924d1a0b6d9c66fc96f64.zip |
Added support for animated tiles.
Diffstat (limited to 'src/resources/mapreader.cpp')
-rw-r--r-- | src/resources/mapreader.cpp | 90 |
1 files changed, 67 insertions, 23 deletions
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 835e52b3..4e116889 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -304,7 +304,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); @@ -407,6 +408,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++; @@ -452,10 +459,12 @@ Tileset *MapReader::readTileset(xmlNodePtr node, const std::string &path, Map *map) { + Tileset *set = NULL; + if (xmlHasProp(node, BAD_CAST "source")) { logger->log("Warning: External tilesets not supported yet."); - return NULL; + return set; } const int firstGid = XML::getProperty(node, "firstgid", 0); @@ -464,34 +473,69 @@ 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", ""); - const std::string source = XML::getProperty(childNode, "source", ""); + if (!source.empty()) + { + std::string sourceStr = source; + sourceStr.erase(0, 3); // Remove "../" + + 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; - 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(); - 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; } - return NULL; + return set; } |