ebc98697c2c0f650f1a138283babf9e6ddfeb16c
[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/bombfish.hpp"
30 #include "badguy/bouncing_snowball.hpp"
31 #include "badguy/captainsnowball.hpp"
32 #include "badguy/crystallo.hpp"
33 #include "badguy/dart.hpp"
34 #include "badguy/darttrap.hpp"
35 #include "badguy/dispenser.hpp"
36 #include "badguy/fish.hpp"
37 #include "badguy/flame.hpp"
38 #include "badguy/flyingsnowball.hpp"
39 #include "badguy/ghosttree.hpp"
40 #include "badguy/haywire.hpp"
41 #include "badguy/igel.hpp"
42 #include "badguy/jumpy.hpp"
43 #include "badguy/kamikazesnowball.hpp"
44 #include "badguy/kugelblitz.hpp"
45 #include "badguy/mole.hpp"
46 #include "badguy/mole_rock.hpp"
47 #include "badguy/mrbomb.hpp"
48 #include "badguy/mriceblock.hpp"
49 #include "badguy/mrtree.hpp"
50 #include "badguy/owl.hpp"
51 #include "badguy/plant.hpp"
52 #include "badguy/poisonivy.hpp"
53 #include "badguy/root.hpp"
54 #include "badguy/short_fuse.hpp"
55 #include "badguy/skullyhop.hpp"
56 #include "badguy/smartball.hpp"
57 #include "badguy/snail.hpp"
58 #include "badguy/snowball.hpp"
59 #include "badguy/snowman.hpp"
60 #include "badguy/spidermite.hpp"
61 #include "badguy/spiky.hpp"
62 #include "badguy/sspiky.hpp"
63 #include "badguy/stalactite.hpp"
64 #include "badguy/stumpy.hpp"
65 #include "badguy/toad.hpp"
66 #include "badguy/totem.hpp"
67 #include "badguy/treewillowisp.hpp"
68 #include "badguy/walking_badguy.hpp"
69 #include "badguy/walkingleaf.hpp"
70 #include "badguy/willowisp.hpp"
71 #include "badguy/yeti.hpp"
72 #include "badguy/yeti_stalactite.hpp"
73 #include "badguy/zeekling.hpp"
74
75 #include "object/ambient_sound.hpp"
76 #include "object/anchor_point.hpp"
77 #include "object/background.hpp"
78 #include "object/bicycle_platform.hpp"
79 #include "object/block.hpp"
80 #include "object/bonus_block.hpp"
81 #include "object/bouncy_coin.hpp"
82 #include "object/brick.hpp"
83 #include "object/broken_brick.hpp"
84 #include "object/bullet.hpp"
85 #include "object/camera.hpp"
86 #include "object/candle.hpp"
87 #include "object/cloud_particle_system.hpp"
88 #include "object/coin.hpp"
89 #include "object/comet_particle_system.hpp"
90 #include "object/decal.hpp"
91 #include "object/display_effect.hpp"
92 #include "object/electrifier.hpp"
93 #include "object/endsequence_fireworks.hpp"
94 #include "object/endsequence.hpp"
95 #include "object/endsequence_walkleft.hpp"
96 #include "object/endsequence_walkright.hpp"
97 #include "object/explosion.hpp"
98 #include "object/falling_coin.hpp"
99 #include "object/firefly.hpp"
100 #include "object/fireworks.hpp"
101 #include "object/floating_image.hpp"
102 #include "object/floating_text.hpp"
103 #include "object/flower.hpp"
104 #include "object/ghost_particle_system.hpp"
105 #include "object/gradient.hpp"
106 #include "object/growup.hpp"
107 #include "object/hurting_platform.hpp"
108 #include "object/icecrusher.hpp"
109 #include "object/infoblock.hpp"
110 #include "object/invisible_block.hpp"
111 #include "object/invisible_wall.hpp"
112 #include "object/ispy.hpp"
113 #include "object/lantern.hpp"
114 #include "object/level_time.hpp"
115 #include "object/light.hpp"
116 #include "object/magicblock.hpp"
117 #include "object/moving_sprite.hpp"
118 #include "object/oneup.hpp"
119 #include "object/particles.hpp"
120 #include "object/particlesystem.hpp"
121 #include "object/particlesystem_interactive.hpp"
122 #include "object/path.hpp"
123 #include "object/path_walker.hpp"
124 #include "object/platform.hpp"
125 #include "object/player.hpp"
126 #include "object/pneumatic_platform.hpp"
127 #include "object/portable.hpp"
128 #include "object/powerup.hpp"
129 #include "object/pulsing_light.hpp"
130 #include "object/pushbutton.hpp"
131 #include "object/rain_particle_system.hpp"
132 #include "object/rainsplash.hpp"
133 #include "object/rock.hpp"
134 #include "object/scripted_object.hpp"
135 #include "object/skull_tile.hpp"
136 #include "object/smoke_cloud.hpp"
137 #include "object/snow_particle_system.hpp"
138 #include "object/specialriser.hpp"
139 #include "object/spotlight.hpp"
140 #include "object/sprite_particle.hpp"
141 #include "object/star.hpp"
142 #include "object/text_object.hpp"
143 #include "object/thunderstorm.hpp"
144 #include "object/tilemap.hpp"
145 #include "object/trampoline.hpp"
146 #include "object/unstable_tile.hpp"
147 #include "object/weak_block.hpp"
148 #include "object/wind.hpp"
149
150 #include "trigger/climbable.hpp"
151 #include "trigger/door.hpp"
152 #include "trigger/scripttrigger.hpp"
153 #include "trigger/secretarea_trigger.hpp"
154 #include "trigger/sequence_trigger.hpp"
155 #include "trigger/switch.hpp"
156
157 ObjectFactory&
158 ObjectFactory::instance()
159 {
160   static ObjectFactory instance_;
161   return instance_;
162 }
163
164 ObjectFactory::ObjectFactory() :
165   factories()
166 {
167   init_factories();
168 }
169
170 ObjectFactory::~ObjectFactory()
171 {
172 }
173
174 void
175 ObjectFactory::init_factories()
176 {
177   // badguys
178   add_factory<AngryStone>("angrystone");
179   add_factory<BombFish>("bombfish");
180   add_factory<BouncingSnowball>("bouncingsnowball");
181   add_factory<CaptainSnowball>("captainsnowball");
182   add_factory<Crystallo>("crystallo");
183   add_factory<Dart>("dart");
184   add_factory<DartTrap>("darttrap");
185   add_factory<Dispenser>("dispenser");
186   add_factory<Fish>("fish");
187   add_factory<Flame>("flame");
188   add_factory<FlyingSnowBall>("flyingsnowball");
189   add_factory<GhostTree>("ghosttree");
190   add_factory<Haywire>("haywire");
191   add_factory<Igel>("igel");
192   add_factory<Jumpy>("jumpy");
193   add_factory<KamikazeSnowball>("kamikazesnowball");
194   add_factory<Kugelblitz>("kugelblitz");
195   add_factory<Mole>("mole");
196   add_factory<MoleRock>("mole_rock");
197   add_factory<MrBomb>("mrbomb");
198   add_factory<MrIceBlock>("mriceblock");
199   add_factory<MrTree>("mrtree");
200   add_factory<Owl>("owl");
201   add_factory<Plant>("plant");
202   add_factory<PoisonIvy>("poisonivy");
203   add_factory<ShortFuse>("short_fuse");
204   add_factory<SSpiky>("sspiky");
205   add_factory<SkullyHop>("skullyhop");
206   add_factory<SmartBall>("smartball");
207   add_factory<Snail>("snail");
208   add_factory<SnowBall>("snowball");
209   add_factory<Snowman>("snowman");
210   add_factory<SpiderMite>("spidermite");
211   add_factory<Spiky>("spiky");
212   add_factory<Stalactite>("stalactite");
213   add_factory<Stumpy>("stumpy");
214   add_factory<Toad>("toad");
215   add_factory<Totem>("totem");
216   add_factory<WalkingLeaf>("walkingleaf");
217   add_factory<WillOWisp>("willowisp");
218   add_factory<Yeti>("yeti");
219   add_factory<YetiStalactite>("yeti_stalactite");
220   add_factory<Zeekling>("zeekling");
221
222   // other objects
223   add_factory<AmbientSound>("ambient_sound");
224   add_factory<Background>("background");
225   add_factory<BicyclePlatform>("bicycle-platform");
226   add_factory<BonusBlock>("bonusblock");
227   add_factory<Candle>("candle");
228   add_factory<Coin>("coin");
229   add_factory<Decal>("decal");
230   add_factory<Explosion>("explosion");
231   add_factory<Firefly>("firefly");
232   add_factory<Gradient>("gradient");
233   add_factory<HurtingPlatform>("hurting_platform");
234   add_factory<IceCrusher>("icecrusher");
235   add_factory<InfoBlock>("infoblock");
236   add_factory<InvisibleWall>("invisible_wall");
237   add_factory<Ispy>("ispy");
238   add_factory<Lantern>("lantern");
239   add_factory<LevelTime>("leveltime");
240   add_factory<MagicBlock>("magicblock");
241   add_factory<Platform>("platform");
242   add_factory<PneumaticPlatform>("pneumatic-platform");
243   add_factory<PowerUp>("powerup");
244   add_factory<PushButton>("pushbutton");
245   add_factory<Rock>("rock");
246   add_factory<ScriptedObject>("scriptedobject");
247   add_factory<SkullTile>("skull_tile");
248   add_factory<Spotlight>("spotlight");
249   add_factory<Thunderstorm>("thunderstorm");
250   add_factory<TileMap>("tilemap");
251   add_factory<Trampoline>("trampoline");
252   add_factory<UnstableTile>("unstable_tile");
253   add_factory<WeakBlock>("weak_block");
254   add_factory<Wind>("wind");
255
256   // trigger
257   add_factory<Climbable>("climbable");
258   add_factory<Door>("door");
259   add_factory<ScriptTrigger>("scripttrigger");
260   add_factory<SecretAreaTrigger>("secretarea");
261   add_factory<SequenceTrigger>("sequencetrigger");
262   add_factory<Switch>("switch");
263 }
264
265 GameObject*
266 ObjectFactory::create(const std::string& name, const Reader& reader)
267 {
268   Factories::iterator i = factories.find(name);
269
270   if (i == factories.end()) 
271   {
272     std::stringstream msg;
273     msg << "No factory for object '" << name << "' found.";
274     throw std::runtime_error(msg.str());
275   }
276   else
277   {
278     return i->second->create(reader);
279   }
280 }
281
282 GameObject*
283 ObjectFactory::create(const std::string& name, const Vector& pos, const Direction dir)
284 {
285   std::stringstream lisptext;
286   lisptext << "((x " << pos.x << ")"
287            << " (y " << pos.y << ")";
288   if(dir != AUTO)
289     lisptext << " (direction " << dir << "))";
290
291   lisp::Parser parser;
292   const lisp::Lisp* lisp = parser.parse(lisptext, "create_object");
293   
294   GameObject* object = create(name, *(lisp->get_car()));
295   return object;
296 }
297
298 /* EOF */