Haywire: Implemented a first version of the "crazy bomb".
[supertux.git] / src / supertux / object_factory.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include <sstream>
19 #include <stdexcept>
20
21 #include "lisp/parser.hpp"
22 #include "math/vector.hpp"
23 #include "util/reader.hpp"
24 #include "supertux/object_factory.hpp"
25
26 #include "badguy/angrystone.hpp"
27 #include "badguy/badguy.hpp"
28 #include "badguy/bomb.hpp"
29 #include "badguy/bouncing_snowball.hpp"
30 #include "badguy/captainsnowball.hpp"
31 #include "badguy/crystallo.hpp"
32 #include "badguy/dart.hpp"
33 #include "badguy/darttrap.hpp"
34 #include "badguy/dispenser.hpp"
35 #include "badguy/fish.hpp"
36 #include "badguy/flame.hpp"
37 #include "badguy/flyingsnowball.hpp"
38 #include "badguy/ghosttree.hpp"
39 #include "badguy/haywire.hpp"
40 #include "badguy/igel.hpp"
41 #include "badguy/jumpy.hpp"
42 #include "badguy/kamikazesnowball.hpp"
43 #include "badguy/kugelblitz.hpp"
44 #include "badguy/mole.hpp"
45 #include "badguy/mole_rock.hpp"
46 #include "badguy/mrbomb.hpp"
47 #include "badguy/mriceblock.hpp"
48 #include "badguy/mrtree.hpp"
49 #include "badguy/plant.hpp"
50 #include "badguy/poisonivy.hpp"
51 #include "badguy/root.hpp"
52 #include "badguy/skullyhop.hpp"
53 #include "badguy/smartball.hpp"
54 #include "badguy/snail.hpp"
55 #include "badguy/snowball.hpp"
56 #include "badguy/snowman.hpp"
57 #include "badguy/spidermite.hpp"
58 #include "badguy/spiky.hpp"
59 #include "badguy/sspiky.hpp"
60 #include "badguy/stalactite.hpp"
61 #include "badguy/stumpy.hpp"
62 #include "badguy/toad.hpp"
63 #include "badguy/totem.hpp"
64 #include "badguy/treewillowisp.hpp"
65 #include "badguy/walking_badguy.hpp"
66 #include "badguy/walkingleaf.hpp"
67 #include "badguy/willowisp.hpp"
68 #include "badguy/yeti.hpp"
69 #include "badguy/yeti_stalactite.hpp"
70 #include "badguy/zeekling.hpp"
71
72 #include "object/ambient_sound.hpp"
73 #include "object/anchor_point.hpp"
74 #include "object/background.hpp"
75 #include "object/bicycle_platform.hpp"
76 #include "object/block.hpp"
77 #include "object/bonus_block.hpp"
78 #include "object/bouncy_coin.hpp"
79 #include "object/brick.hpp"
80 #include "object/broken_brick.hpp"
81 #include "object/bullet.hpp"
82 #include "object/camera.hpp"
83 #include "object/candle.hpp"
84 #include "object/cloud_particle_system.hpp"
85 #include "object/coin.hpp"
86 #include "object/comet_particle_system.hpp"
87 #include "object/decal.hpp"
88 #include "object/display_effect.hpp"
89 #include "object/electrifier.hpp"
90 #include "object/endsequence_fireworks.hpp"
91 #include "object/endsequence.hpp"
92 #include "object/endsequence_walkleft.hpp"
93 #include "object/endsequence_walkright.hpp"
94 #include "object/explosion.hpp"
95 #include "object/falling_coin.hpp"
96 #include "object/firefly.hpp"
97 #include "object/fireworks.hpp"
98 #include "object/floating_image.hpp"
99 #include "object/floating_text.hpp"
100 #include "object/flower.hpp"
101 #include "object/ghost_particle_system.hpp"
102 #include "object/gradient.hpp"
103 #include "object/growup.hpp"
104 #include "object/hurting_platform.hpp"
105 #include "object/icecrusher.hpp"
106 #include "object/infoblock.hpp"
107 #include "object/invisible_block.hpp"
108 #include "object/invisible_wall.hpp"
109 #include "object/ispy.hpp"
110 #include "object/lantern.hpp"
111 #include "object/level_time.hpp"
112 #include "object/light.hpp"
113 #include "object/magicblock.hpp"
114 #include "object/moving_sprite.hpp"
115 #include "object/oneup.hpp"
116 #include "object/particles.hpp"
117 #include "object/particlesystem.hpp"
118 #include "object/particlesystem_interactive.hpp"
119 #include "object/path.hpp"
120 #include "object/path_walker.hpp"
121 #include "object/platform.hpp"
122 #include "object/player.hpp"
123 #include "object/pneumatic_platform.hpp"
124 #include "object/portable.hpp"
125 #include "object/powerup.hpp"
126 #include "object/pulsing_light.hpp"
127 #include "object/pushbutton.hpp"
128 #include "object/rain_particle_system.hpp"
129 #include "object/rainsplash.hpp"
130 #include "object/rock.hpp"
131 #include "object/scripted_object.hpp"
132 #include "object/skull_tile.hpp"
133 #include "object/smoke_cloud.hpp"
134 #include "object/snow_particle_system.hpp"
135 #include "object/specialriser.hpp"
136 #include "object/spotlight.hpp"
137 #include "object/sprite_particle.hpp"
138 #include "object/star.hpp"
139 #include "object/text_object.hpp"
140 #include "object/thunderstorm.hpp"
141 #include "object/tilemap.hpp"
142 #include "object/trampoline.hpp"
143 #include "object/unstable_tile.hpp"
144 #include "object/weak_block.hpp"
145 #include "object/wind.hpp"
146
147 #include "trigger/climbable.hpp"
148 #include "trigger/door.hpp"
149 #include "trigger/scripttrigger.hpp"
150 #include "trigger/secretarea_trigger.hpp"
151 #include "trigger/sequence_trigger.hpp"
152 #include "trigger/switch.hpp"
153
154 ObjectFactory&
155 ObjectFactory::instance()
156 {
157   static ObjectFactory instance_;
158   return instance_;
159 }
160
161 ObjectFactory::ObjectFactory() :
162   factories()
163 {
164   init_factories();
165 }
166
167 ObjectFactory::~ObjectFactory()
168 {
169 }
170
171 void
172 ObjectFactory::init_factories()
173 {
174   // badguys
175   add_factory<AngryStone>("angrystone");
176   add_factory<BouncingSnowball>("bouncingsnowball");
177   add_factory<CaptainSnowball>("captainsnowball");
178   add_factory<Crystallo>("crystallo");
179   add_factory<Dart>("dart");
180   add_factory<DartTrap>("darttrap");
181   add_factory<Dispenser>("dispenser");
182   add_factory<Fish>("fish");
183   add_factory<Flame>("flame");
184   add_factory<FlyingSnowBall>("flyingsnowball");
185   add_factory<GhostTree>("ghosttree");
186   add_factory<Haywire>("haywire");
187   add_factory<Igel>("igel");
188   add_factory<Jumpy>("jumpy");
189   add_factory<KamikazeSnowball>("kamikazesnowball");
190   add_factory<Kugelblitz>("kugelblitz");
191   add_factory<Mole>("mole");
192   add_factory<MoleRock>("mole_rock");
193   add_factory<MrBomb>("mrbomb");
194   add_factory<MrIceBlock>("mriceblock");
195   add_factory<MrTree>("mrtree");
196   add_factory<Plant>("plant");
197   add_factory<PoisonIvy>("poisonivy");
198   add_factory<SSpiky>("sspiky");
199   add_factory<SkullyHop>("skullyhop");
200   add_factory<SmartBall>("smartball");
201   add_factory<Snail>("snail");
202   add_factory<SnowBall>("snowball");
203   add_factory<Snowman>("snowman");
204   add_factory<SpiderMite>("spidermite");
205   add_factory<Spiky>("spiky");
206   add_factory<Stalactite>("stalactite");
207   add_factory<Stumpy>("stumpy");
208   add_factory<Toad>("toad");
209   add_factory<Totem>("totem");
210   add_factory<WalkingLeaf>("walkingleaf");
211   add_factory<WillOWisp>("willowisp");
212   add_factory<Yeti>("yeti");
213   add_factory<YetiStalactite>("yeti_stalactite");
214   add_factory<Zeekling>("zeekling");
215
216   // other objects
217   add_factory<AmbientSound>("ambient_sound");
218   add_factory<Background>("background");
219   add_factory<BicyclePlatform>("bicycle-platform");
220   add_factory<BonusBlock>("bonusblock");
221   add_factory<Candle>("candle");
222   add_factory<Coin>("coin");
223   add_factory<Decal>("decal");
224   add_factory<Explosion>("explosion");
225   add_factory<Firefly>("firefly");
226   add_factory<Gradient>("gradient");
227   add_factory<HurtingPlatform>("hurting_platform");
228   add_factory<IceCrusher>("icecrusher");
229   add_factory<InfoBlock>("infoblock");
230   add_factory<InvisibleWall>("invisible_wall");
231   add_factory<Ispy>("ispy");
232   add_factory<Lantern>("lantern");
233   add_factory<LevelTime>("leveltime");
234   add_factory<MagicBlock>("magicblock");
235   add_factory<Platform>("platform");
236   add_factory<PneumaticPlatform>("pneumatic-platform");
237   add_factory<PowerUp>("powerup");
238   add_factory<PushButton>("pushbutton");
239   add_factory<Rock>("rock");
240   add_factory<ScriptedObject>("scriptedobject");
241   add_factory<SkullTile>("skull_tile");
242   add_factory<Spotlight>("spotlight");
243   add_factory<Thunderstorm>("thunderstorm");
244   add_factory<TileMap>("tilemap");
245   add_factory<Trampoline>("trampoline");
246   add_factory<UnstableTile>("unstable_tile");
247   add_factory<WeakBlock>("weak_block");
248   add_factory<Wind>("wind");
249
250   // trigger
251   add_factory<Climbable>("climbable");
252   add_factory<Door>("door");
253   add_factory<ScriptTrigger>("scripttrigger");
254   add_factory<SecretAreaTrigger>("secretarea");
255   add_factory<SequenceTrigger>("sequencetrigger");
256   add_factory<Switch>("switch");
257 }
258
259 GameObject*
260 ObjectFactory::create(const std::string& name, const Reader& reader)
261 {
262   Factories::iterator i = factories.find(name);
263
264   if (i == factories.end()) 
265   {
266     std::stringstream msg;
267     msg << "No factory for object '" << name << "' found.";
268     throw std::runtime_error(msg.str());
269   }
270   else
271   {
272     return i->second->create(reader);
273   }
274 }
275
276 GameObject*
277 ObjectFactory::create(const std::string& name, const Vector& pos, const Direction dir)
278 {
279   std::stringstream lisptext;
280   lisptext << "((x " << pos.x << ")"
281            << " (y " << pos.y << ")";
282   if(dir != AUTO)
283     lisptext << " (direction " << dir << "))";
284
285   lisp::Parser parser;
286   const lisp::Lisp* lisp = parser.parse(lisptext, "create_object");
287   
288   GameObject* object = create(name, *(lisp->get_car()));
289   return object;
290 }
291
292 /* EOF */