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) {
delete *i;
}
for (vector<SDL_Surface *>::const_iterator i(images.begin()), end(images.end()); i != end; ++i) {
SDL_FreeSurface(*i);
}
-}
-
-
-bool Interpreter::ParsedDefinition::IsCompatible(DynamicType with) const {
- return type == with
- || (with == ANIMATION
- && (type == COMPLEX_ANIMATION || type == SIMPLE_ANIMATION));
+ 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;
+ }
}
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");
}
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)) {
- return &heroes[i->second.index];
+ if (i->second.type == HERO) {
+ return heroes[i->second.index];
} else {
throw Error("cannot cast " + i->second.dfn->TypeName() + " to Hero");
}
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)) {
- return &monsters[i->second.index];
+ if (i->second.type == MONSTER) {
+ return monsters[i->second.index];
} else {
throw Error("cannot cast " + i->second.dfn->TypeName() + " to Monster");
}
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");
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)) {
- return &sprites[i->second.index];
+ if (i->second.type == SPRITE) {
+ return sprites[i->second.index];
} else {
throw Error("cannot cast " + i->second.dfn->TypeName() + " to Sprite");
}
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 {
void Interpreter::ReadObject(const Definition &dfn) {
- if (dfn.TypeName() == "Hero") {
- heroes.push_back(Hero());
- ReadHero(heroes.back(), *dfn.GetProperties());
- parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, heroes.size() - 1)));
+ 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);
+ ReadHero(*hero, *dfn.GetProperties());
+ parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, HERO, index)));
} else if (dfn.TypeName() == "Monster") {
- monsters.push_back(Monster());
- ReadMonster(monsters.back(), *dfn.GetProperties());
- parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, MONSTER, monsters.size() - 1)));
+ Monster *monster(new Monster);
+ int index(monsters.size());
+ 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") {
- sprites.push_back(Sprite());
- ReadSprite(sprites.back(), *dfn.GetProperties());
- parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, sprites.size() - 1)));
+ Sprite *sprite(new Sprite);
+ int index(sprites.size());
+ sprites.push_back(sprite);
+ ReadSprite(*sprite, *dfn.GetProperties());
+ parsedDefinitions.insert(make_pair(dfn.Identifier(), ParsedDefinition(&dfn, SPRITE, index)));
} else {
throw Error("unhandled object type: " + dfn.TypeName());
}
m.SetStats(stats);
} else if (i->first == "attackAnimation") {
m.SetAttackAnimation(GetAnimation(*i->second));
+ } else if (i->first == "meleeAnimation") {
+ m.SetMeleeAnimation(GetAnimation(*i->second));
} else {
throw Error("unknown Monster property: " + i->first);
}