make it possible to put background images in each layer
[supertux.git] / src / badguy_specs.cpp
1 /***************************************************************************
2                badguy_specs.cpp  -  badguys properties table
3                      -------------------
4     begin                : Oct, 11 2004
5     copyright            : (C) 2004 by Ricardo Cruz
6     email                : rick2@aeiou.pt
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <config.h>
19
20 #include <iostream>
21
22 #include "special/sprite_manager.h"
23 #include "resources.h"
24
25 #include "badguy_specs.h"
26
27 BadGuySpecsManager* badguyspecs_manager = 0;
28
29 BadGuySpecsManager::BadGuySpecsManager(const std::string& filename)
30 {
31   load_resfile(filename);
32 }
33
34 BadGuySpecsManager::~BadGuySpecsManager()
35 {
36   for(std::map<std::string, BadGuySpecs*>::iterator i =
37      badguys_specs.begin(); i != badguys_specs.end(); ++i)
38     delete i->second;
39 }
40
41 void
42 BadGuySpecsManager::load_resfile(const std::string& filename)
43 {
44   lisp_object_t* root_obj = lisp_read_from_file(filename);
45   if (!root_obj)
46     {
47       std::cout << "BadGuySpecsManager: Couldn't load: " << filename << std::endl;
48       return;
49     }
50
51   lisp_object_t* cur = root_obj;
52
53   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-badguys-specifications") != 0)
54     return;
55   cur = lisp_cdr(cur);
56
57   while(cur)
58     {
59       lisp_object_t* el = lisp_car(cur);
60
61       if (strcmp(lisp_symbol(lisp_car(el)), "badguy") == 0)
62         {
63           LispReader reader(lisp_cdr(lisp_car(cur)));
64           BadGuySpecs* badguy_specs = new BadGuySpecs(reader);
65
66           BadGuysSpecs::iterator i = badguys_specs.find(badguy_specs->get_name());
67           if (i == badguys_specs.end())
68             {
69               badguys_specs[badguy_specs->get_name()] = badguy_specs;
70             }
71           else
72             {
73               delete i->second;
74               i->second = badguy_specs;
75               std::cerr << "Warning: dulpicate entry: '" << badguy_specs->get_name() << "'" << std::endl;
76             }
77         }
78       else
79         {
80           std::cout << "BadGuySpecsManager: Unknown tag" << std::endl;
81         }
82
83       cur = lisp_cdr(cur);
84     }
85
86   lisp_free(root_obj);
87 }
88
89 BadGuySpecs*
90 BadGuySpecsManager::load(const std::string& name)
91 {
92   BadGuysSpecs::iterator i = badguys_specs.find(name);
93   if (i == badguys_specs.end())
94     {
95       std::cerr << "Warning: BadGuy specification '" << name << "' not found" << std::endl;
96       return 0;
97     }
98   return i->second;
99 }
100
101 BadGuySpecs::BadGuySpecs(LispReader& reader)
102 {
103   reset();
104
105   std::string str;
106   reader.read_string("kind", str);
107   kind = str;
108
109   str.clear();
110   reader.read_string("inherits", str);
111   if(!str.empty())
112     {
113     BadGuySpecs* bgspecs = badguyspecs_manager->load(str);
114     if(bgspecs)
115       {
116       sprite = bgspecs->sprite;
117       }
118     else
119       std::cerr << "Warning: inherited '" << str
120                 << "was not found.\nProbably, it was declared after"
121                    "this entry '" << kind << std::endl;
122     }
123
124   str.clear();
125   reader.read_string("sprite", str);
126   if(str.empty())
127     std::cerr << "Warning: No sprite has been set to badguy " << kind << std::endl;
128   else
129     sprite = sprite_manager->create(str);
130
131   if(!sprite) {
132     std::cerr << "Warning: Sprite '" << str << "' could not be loaded.\n";
133   }
134 }
135
136 BadGuySpecs::BadGuySpecs(std::string& kind_)
137 {
138   reset();
139   kind = kind_;
140 }
141
142 BadGuySpecs::~BadGuySpecs()
143 {
144   delete sprite;
145 }
146
147 void
148 BadGuySpecs::reset()
149 {
150   kind.clear();
151 }
152
153 std::string
154 BadGuySpecs::get_name()
155 {
156 return kind;
157 }
158
159 // EOF //