]> git.localhorst.tv Git - l2e.git/blob - src/loader/Interpreter.h
8824a14ecfb2e36eb428519c9268d328d5a3230f
[l2e.git] / src / loader / Interpreter.h
1 /*
2  * Interpreter.h
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_INTERPRETER_H_
9 #define LOADER_INTERPRETER_H_
10
11 #include "../geometry/Vector.h"
12 #include "../graphics/ComplexAnimation.h"
13
14 #include <map>
15 #include <stdexcept>
16 #include <string>
17 #include <vector>
18 #include <SDL.h>
19
20 namespace battle {
21         class Hero;
22         class Monster;
23         class Stats;
24 }
25
26 namespace graphics {
27         class Animation;
28         class SimpleAnimation;
29         class Sprite;
30 }
31
32 namespace loader {
33
34 class Definition;
35 class ParsedSource;
36 class PropertyList;
37 class Value;
38
39 class Interpreter {
40
41 public:
42         class Error: public std::runtime_error {
43         public:
44                 Error(const std::string &msg) : std::runtime_error("interpreter error: " + msg) { }
45         };
46
47 public:
48         Interpreter(const ParsedSource &source) : source(source) { }
49         ~Interpreter();
50 private:
51         Interpreter(const Interpreter &);
52         Interpreter &operator =(const Interpreter &);
53
54 public:
55         void ReadSource();
56
57 public:
58         graphics::Animation *GetAnimation(const std::string &name);
59         bool GetBoolean(const std::string &name) const;
60         battle::Hero *GetHero(const std::string &name);
61         battle::Monster *GetMonster(const std::string &name);
62         int GetNumber(const std::string &name) const;
63         graphics::Sprite *GetSprite(const std::string &name);
64         const char *GetString(const std::string &name) const;
65         geometry::Vector<int> GetVector(const std::string &name) const;
66
67 public:
68         const std::vector<bool> &Booleans() const { return booleans; }
69         const std::vector<graphics::ComplexAnimation *> &ComplexAnimations() const { return complexAnimations; }
70         const std::vector<battle::Hero *> &Heroes() const { return heroes; }
71         const std::vector<SDL_Surface *> &Images() const { return images; }
72         const std::vector<battle::Monster *> &Monsters() const { return monsters; }
73         const std::vector<int> &Numbers() const { return numbers; }
74         const std::vector<graphics::SimpleAnimation *> &SimpleAnimations() const { return simpleAnimations; }
75         const std::vector<graphics::Sprite *> &Sprites() const { return sprites; }
76         const std::vector<const char *> &Strings() const { return strings; }
77         const std::vector<geometry::Vector<int> > &Vectors() const { return vectors; }
78
79 private:
80         void ReadDefinition(const Definition &);
81         void ReadLiteral(const Definition &);
82         void ReadObject(const Definition &);
83
84         graphics::Animation *GetAnimation(const Value &);
85         bool GetBoolean(const Value &);
86         SDL_Surface *GetImage(const Value &);
87         int GetNumber(const Value &);
88         const PropertyList *GetPropertyList(const Value &);
89         const std::vector<PropertyList *> &GetPropertyListArray(const Value &);
90         graphics::Sprite *GetSprite(const Value &);
91         const char *GetString(const Value &);
92         const std::vector<Value *> &GetValueArray(const Value &);
93         geometry::Vector<int> GetVector(const Value &);
94
95         void ReadComplexAnimation(graphics::ComplexAnimation &, const PropertyList &);
96         void ReadComplexAnimationFrame(graphics::ComplexAnimation::FrameProp &, const PropertyList &);
97         void ReadHero(battle::Hero &, const PropertyList &);
98         void ReadMonster(battle::Monster &, const PropertyList &);
99         void ReadSimpleAnimation(graphics::SimpleAnimation &, const PropertyList &);
100         void ReadSprite(graphics::Sprite &, const PropertyList &);
101         void ReadStats(battle::Stats &, const PropertyList &);
102
103 private:
104         const ParsedSource &source;
105         enum Type {
106                 BOOLEAN,
107                 COMPLEX_ANIMATION,
108                 HERO,
109                 IMAGE,
110                 MONSTER,
111                 NUMBER,
112                 PROPERTY_LIST_ARRAY,
113                 SIMPLE_ANIMATION,
114                 SPRITE,
115                 STRING,
116                 VECTOR,
117                 VALUE_ARRAY,
118         };
119         struct ParsedDefinition {
120                 ParsedDefinition(const Definition *dfn, Type type, int index)
121                 : dfn(dfn), type(type), index(index) { }
122                 const Definition *dfn;
123                 Type type;
124                 int index;
125         };
126         std::map<std::string, ParsedDefinition> parsedDefinitions;
127
128         std::vector<bool> booleans;
129         std::vector<graphics::ComplexAnimation *> complexAnimations;
130         std::vector<battle::Hero *> heroes;
131         std::vector<SDL_Surface *> images;
132         std::vector<battle::Monster *> monsters;
133         std::vector<int> numbers;
134         std::vector<PropertyList *> propertyLists;
135         std::vector<std::vector<PropertyList *> > propertyListArrays;
136         std::vector<graphics::SimpleAnimation *> simpleAnimations;
137         std::vector<graphics::Sprite *> sprites;
138         std::vector<const char *> strings;
139         std::vector<std::vector<Value *> > valueArrays;
140         std::vector<geometry::Vector<int> > vectors;
141
142 };
143
144 }
145
146 #endif /* LOADER_INTERPRETER_H_ */