]> git.localhorst.tv Git - l2e.git/commitdiff
split complex and simple animations in interpreter
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 30 Aug 2012 20:34:03 +0000 (22:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 30 Aug 2012 20:34:03 +0000 (22:34 +0200)
src/loader/Interpreter.cpp
src/loader/Interpreter.h

index 12dacb89cd8ab85fe92f3b6bf9f23e0df0a8180d..3eb680414736c41354909398dca9ec6149de260c 100644 (file)
@@ -34,7 +34,7 @@ using std::vector;
 namespace loader {
 
 Interpreter::~Interpreter() {
-       for (vector<Animation *>::const_iterator i(animations.begin()), end(animations.end()); i != end; ++i) {
+       for (vector<ComplexAnimation *>::const_iterator i(complexAnimations.begin()), end(complexAnimations.end()); i != end; ++i) {
                delete *i;
        }
        for (vector<Hero *>::const_iterator i(heroes.begin()), end(heroes.end()); i != end; ++i) {
@@ -46,24 +46,22 @@ Interpreter::~Interpreter() {
        for (vector<Monster *>::const_iterator i(monsters.begin()), end(monsters.end()); i != end; ++i) {
                delete *i;
        }
+       for (vector<SimpleAnimation *>::const_iterator i(simpleAnimations.begin()), end(simpleAnimations.end()); i != end; ++i) {
+               delete *i;
+       }
        for (vector<Sprite *>::const_iterator i(sprites.begin()), end(sprites.end()); i != end; ++i) {
                delete *i;
        }
 }
 
 
-bool Interpreter::ParsedDefinition::IsCompatible(DynamicType with) const {
-       return type == with
-                       || (with == ANIMATION
-                                       && (type == COMPLEX_ANIMATION || type == SIMPLE_ANIMATION));
-}
-
-
 Animation *Interpreter::GetAnimation(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.IsCompatible(ANIMATION)) {
-                       return animations[i->second.index];
+               if (i->second.type == COMPLEX_ANIMATION) {
+                       return complexAnimations[i->second.index];
+               } else if (i->second.type == SIMPLE_ANIMATION) {
+                       return simpleAnimations[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to Animation");
                }
@@ -75,7 +73,7 @@ Animation *Interpreter::GetAnimation(const std::string &name) {
 Hero *Interpreter::GetHero(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.IsCompatible(HERO)) {
+               if (i->second.type == HERO) {
                        return heroes[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero");
@@ -88,7 +86,7 @@ Hero *Interpreter::GetHero(const std::string &name) {
 Monster *Interpreter::GetMonster(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.IsCompatible(MONSTER)) {
+               if (i->second.type == MONSTER) {
                        return monsters[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster");
@@ -101,7 +99,7 @@ Monster *Interpreter::GetMonster(const std::string &name) {
 int Interpreter::GetNumber(const std::string &name) const {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.IsCompatible(NUMBER)) {
+               if (i->second.type == NUMBER) {
                        return numbers[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to Number");
@@ -114,7 +112,7 @@ int Interpreter::GetNumber(const std::string &name) const {
 Sprite *Interpreter::GetSprite(const std::string &name) {
        map<string, ParsedDefinition>::const_iterator i(parsedDefinitions.find(name));
        if (i != parsedDefinitions.end()) {
-               if (i->second.IsCompatible(SPRITE)) {
+               if (i->second.type == SPRITE) {
                        return sprites[i->second.index];
                } else {
                        throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite");
@@ -177,12 +175,12 @@ Animation *Interpreter::GetAnimation(const Value &v) {
                if (v.GetLiteral().GetTypeName() == "ComplexAnimation") {
                        ComplexAnimation *a(new ComplexAnimation);
                        ReadComplexAnimation(*a, *v.GetLiteral().GetProperties());
-                       animations.push_back(a);
+                       complexAnimations.push_back(a);
                        return a;
                } else {
                        SimpleAnimation *a(new SimpleAnimation);
                        ReadSimpleAnimation(*a, *v.GetLiteral().GetProperties());
-                       animations.push_back(a);
+                       simpleAnimations.push_back(a);
                        return a;
                }
        } else {
@@ -268,7 +266,13 @@ Vector<int> Interpreter::GetVector(const Value &v) {
 
 
 void Interpreter::ReadObject(const Definition &dfn) {
-       if (dfn.TypeName() == "Hero") {
+       if (dfn.TypeName() == "ComplexAnimation") {
+               ComplexAnimation *animation(new ComplexAnimation);
+               int index(complexAnimations.size());
+               complexAnimations.push_back(animation);
+               ReadComplexAnimation(*animation, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, COMPLEX_ANIMATION, index)));
+       } else if (dfn.TypeName() == "Hero") {
                Hero *hero(new Hero);
                int index(heroes.size());
                heroes.push_back(hero);
@@ -280,6 +284,12 @@ void Interpreter::ReadObject(const Definition &dfn) {
                monsters.push_back(monster);
                ReadMonster(*monster, *dfn.GetProperties());
                parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, index)));
+       } else if (dfn.TypeName() == "SimpleAnimation") {
+               SimpleAnimation *animation(new SimpleAnimation);
+               int index(simpleAnimations.size());
+               simpleAnimations.push_back(animation);
+               ReadSimpleAnimation(*animation, *dfn.GetProperties());
+               parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SIMPLE_ANIMATION, index)));
        } else if (dfn.TypeName() == "Sprite") {
                Sprite *sprite(new Sprite);
                int index(sprites.size());
index aea5fbe85712433c963cf5f1aeb8016f59fdbc0b..da0cb88de147c2725a6d846bf9782d76f69ca77f 100644 (file)
@@ -87,8 +87,7 @@ private:
 
 private:
        const ParsedSource &source;
-       enum DynamicType {
-               ANIMATION,
+       enum Type {
                COMPLEX_ANIMATION,
                HERO,
                MONSTER,
@@ -97,20 +96,20 @@ private:
                SPRITE,
        };
        struct ParsedDefinition {
-               ParsedDefinition(const Definition *dfn, DynamicType type, int index)
+               ParsedDefinition(const Definition *dfn, Type type, int index)
                : dfn(dfn), type(type), index(index) { }
-               bool IsCompatible(DynamicType with) const;
                const Definition *dfn;
-               DynamicType type;
+               Type type;
                int index;
        };
        std::map<std::string, ParsedDefinition> parsedDefinitions;
 
-       std::vector<graphics::Animation *> animations;
+       std::vector<graphics::ComplexAnimation *> complexAnimations;
        std::vector<battle::Hero *> heroes;
        std::vector<SDL_Surface *> images;
        std::vector<battle::Monster *> monsters;
        std::vector<int> numbers;
+       std::vector<graphics::SimpleAnimation *> simpleAnimations;
        std::vector<graphics::Sprite *> sprites;
 
 };