From 930eeae09a3a7dc31c047d199c77ef9e284d37ba Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 27 Mar 2012 23:17:55 +0300 Subject: Extend sequence animation tag. Allow set in attribute value any frame numbers. Examples what doing same: Other examples: p mean pause. --- src/resources/spritedef.cpp | 118 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 20 deletions(-) (limited to 'src/resources/spritedef.cpp') diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index a4ebc6f7e..95e05de93 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -304,13 +304,7 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, { const int start = XML::getProperty(frameNode, "start", -1); const int end = XML::getProperty(frameNode, "end", -1); - - if (start < 0 || end < 0) - { - logger->log1("No valid value for 'start' or 'end'"); - continue; - } - + const std::string value = XML::getProperty(frameNode, "value", ""); int repeat = XML::getProperty(frameNode, "repeat", 1); if (repeat < 1) @@ -319,25 +313,47 @@ void SpriteDef::loadAnimation(XmlNodePtr animationNode, continue; } - while (repeat > 0) + if (value.empty()) { - int pos = start; - while (end >= pos) + if (addSequence(start, end, delay, offsetX, offsetY, + variant_offset, repeat, rand, imageSet, animation)) { - Image *img = imageSet->get(pos + variant_offset); + continue; + } - if (!img) + } + else + { + std::vector vals; + splitToStringVector(vals, value, ','); + std::vector::const_iterator it = vals.begin(); + std::vector::const_iterator it_end = vals.end(); + for (; it != it_end; ++ it) + { + std::string str = *it; + size_t idx = str.find("-"); + if (str == "p") { - logger->log("No image at index %d", - pos + variant_offset); - pos ++; - continue; + animation->addPause(delay, rand); + } + else if (idx != std::string::npos) + { + int v1 = atoi(str.substr(0, idx).c_str()); + int v2 = atoi(str.substr(idx + 1).c_str()); + addSequence(v1, v2, delay, offsetX, offsetY, + variant_offset, repeat, rand, imageSet, animation); + } + else + { + Image *img = imageSet->get(atoi( + str.c_str()) + variant_offset); + if (img) + { + animation->addFrame(img, delay, + offsetX, offsetY, rand); + } } - - animation->addFrame(img, delay, offsetX, offsetY, rand); - pos ++; } - repeat --; } } else if (xmlNameEqual(frameNode, "pause")) @@ -461,3 +477,65 @@ void SpriteDef::addAction(unsigned hp, std::string name, Action *action) (*mActions[hp])[name] = action; } + +bool SpriteDef::addSequence(int start, int end, int delay, + int offsetX, int offsetY, int variant_offset, + int repeat, int rand, ImageSet *imageSet, + Animation *animation) +{ + if (start < 0 || end < 0) + { + logger->log1("No valid value for 'start' or 'end'"); + return true; + } + + if (start <= end) + { + while (repeat > 0) + { + int pos = start; + while (end >= pos) + { + Image *img = imageSet->get(pos + variant_offset); + + if (!img) + { + logger->log("No image at index %d", + pos + variant_offset); + pos ++; + continue; + } + + animation->addFrame(img, delay, + offsetX, offsetY, rand); + pos ++; + } + repeat --; + } + } + else + { + while (repeat > 0) + { + int pos = start; + while (end <= pos) + { + Image *img = imageSet->get(pos + variant_offset); + + if (!img) + { + logger->log("No image at index %d", + pos + variant_offset); + pos ++; + continue; + } + + animation->addFrame(img, delay, + offsetX, offsetY, rand); + pos --; + } + repeat --; + } + } + return false; +} -- cgit v1.2.3-60-g2f50