game_object_factory.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SuperTux
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/game_object_factory.hpp"
  17. #include "audio/sound_source.hpp"
  18. #include "badguy/angrystone.hpp"
  19. #include "badguy/bouncing_snowball.hpp"
  20. #include "badguy/captainsnowball.hpp"
  21. #include "badguy/corrupted_granito.hpp"
  22. #include "badguy/corrupted_granito_big.hpp"
  23. #include "badguy/crusher.hpp"
  24. #include "badguy/crystallo.hpp"
  25. #include "badguy/dart.hpp"
  26. #include "badguy/darttrap.hpp"
  27. #include "badguy/dispenser.hpp"
  28. #include "badguy/dive_mine.hpp"
  29. #include "badguy/fish_chasing.hpp"
  30. #include "badguy/fish_harmless.hpp"
  31. #include "badguy/fish_jumping.hpp"
  32. #include "badguy/fish_swimming.hpp"
  33. #include "badguy/flame.hpp"
  34. #include "badguy/flyingsnowball.hpp"
  35. #include "badguy/ghosttree.hpp"
  36. #include "badguy/ghoul.hpp"
  37. #include "badguy/goldbomb.hpp"
  38. #include "badguy/granito.hpp"
  39. #include "badguy/granito_big.hpp"
  40. #include "badguy/granito_giant.hpp"
  41. #include "badguy/haywire.hpp"
  42. #include "badguy/igel.hpp"
  43. #include "badguy/jumpy.hpp"
  44. #include "badguy/kamikazesnowball.hpp"
  45. #include "badguy/kugelblitz.hpp"
  46. #include "badguy/livefire.hpp"
  47. #include "badguy/mole.hpp"
  48. #include "badguy/mole_rock.hpp"
  49. #include "badguy/mrbomb.hpp"
  50. #include "badguy/mrtree.hpp"
  51. #include "badguy/owl.hpp"
  52. #include "badguy/plant.hpp"
  53. #include "badguy/rcrystallo.hpp"
  54. #include "badguy/root.hpp"
  55. #include "badguy/root_sapling.hpp"
  56. #include "badguy/short_fuse.hpp"
  57. #include "badguy/skydive.hpp"
  58. #include "badguy/smartball.hpp"
  59. #include "badguy/smartblock.hpp"
  60. #include "badguy/snail.hpp"
  61. #include "badguy/snowball.hpp"
  62. #include "badguy/snowman.hpp"
  63. #include "badguy/spidermite.hpp"
  64. #include "badguy/scrystallo.hpp"
  65. #include "badguy/spiky.hpp"
  66. #include "badguy/sspiky.hpp"
  67. #include "badguy/stalactite.hpp"
  68. #include "badguy/stumpy.hpp"
  69. #include "badguy/toad.hpp"
  70. #include "badguy/totem.hpp"
  71. #include "badguy/viciousivy.hpp"
  72. #include "badguy/walking_candle.hpp"
  73. #include "badguy/walkingleaf.hpp"
  74. #include "badguy/willowisp.hpp"
  75. #include "badguy/yeti.hpp"
  76. #include "badguy/yeti_stalactite.hpp"
  77. #include "badguy/zeekling.hpp"
  78. #include "math/vector.hpp"
  79. #include "object/ambient_light.hpp"
  80. #include "object/ambient_sound.hpp"
  81. #include "object/background.hpp"
  82. #include "object/bicycle_platform.hpp"
  83. #include "object/bonus_block.hpp"
  84. #include "object/brick.hpp"
  85. #include "object/bumper.hpp"
  86. #include "object/camera.hpp"
  87. #include "object/candle.hpp"
  88. #include "object/circleplatform.hpp"
  89. #include "object/cloud_particle_system.hpp"
  90. #include "object/custom_particle_system.hpp"
  91. #include "object/custom_particle_system_file.hpp"
  92. #include "object/coin.hpp"
  93. #include "object/conveyor_belt.hpp"
  94. #include "object/decal.hpp"
  95. #include "object/explosion.hpp"
  96. #include "object/fallblock.hpp"
  97. #include "object/firefly.hpp"
  98. #include "object/ghost_particle_system.hpp"
  99. #include "object/gradient.hpp"
  100. #include "object/hurting_platform.hpp"
  101. #include "object/infoblock.hpp"
  102. #include "object/invisible_block.hpp"
  103. #include "object/invisible_wall.hpp"
  104. #include "object/ispy.hpp"
  105. #include "object/key.hpp"
  106. #include "object/lantern.hpp"
  107. #include "object/level_time.hpp"
  108. #include "object/lit_object.hpp"
  109. #include "object/magicblock.hpp"
  110. #include "object/path.hpp"
  111. #include "object/path_gameobject.hpp"
  112. #include "object/particle_zone.hpp"
  113. #include "object/platform.hpp"
  114. #include "object/pneumatic_platform.hpp"
  115. #include "object/powerup.hpp"
  116. #include "object/pushbutton.hpp"
  117. #include "object/rain_particle_system.hpp"
  118. #include "object/rublight.hpp"
  119. #include "object/rusty_trampoline.hpp"
  120. #include "object/scripted_object.hpp"
  121. #include "object/shard.hpp"
  122. #include "object/snow_particle_system.hpp"
  123. #include "object/sound_object.hpp"
  124. #include "object/spawnpoint.hpp"
  125. #include "object/spotlight.hpp"
  126. #include "object/textscroller.hpp"
  127. #include "object/thunderstorm.hpp"
  128. #include "object/tilemap.hpp"
  129. #include "object/torch.hpp"
  130. #include "object/trampoline.hpp"
  131. #include "object/unstable_tile.hpp"
  132. #include "object/weak_block.hpp"
  133. #include "object/wind.hpp"
  134. #include "supertux/level.hpp"
  135. #include "supertux/tile_manager.hpp"
  136. #include "trigger/climbable.hpp"
  137. #include "trigger/door.hpp"
  138. #include "trigger/scripttrigger.hpp"
  139. #include "trigger/secretarea_trigger.hpp"
  140. #include "trigger/sequence_trigger.hpp"
  141. #include "trigger/switch.hpp"
  142. #include "trigger/text_area.hpp"
  143. #include "util/reader_document.hpp"
  144. #include "util/reader_mapping.hpp"
  145. #include "worldmap/level_tile.hpp"
  146. #include "worldmap/spawn_point.hpp"
  147. #include "worldmap/special_tile.hpp"
  148. #include "worldmap/sprite_change.hpp"
  149. #include "worldmap/teleporter.hpp"
  150. GameObjectFactory&
  151. GameObjectFactory::instance()
  152. {
  153. static GameObjectFactory instance_;
  154. return instance_;
  155. }
  156. GameObjectFactory::GameObjectFactory()
  157. {
  158. init_factories();
  159. }
  160. void
  161. GameObjectFactory::init_factories()
  162. {
  163. // Badguys.
  164. m_adding_badguys = true;
  165. add_factory<AngryStone>("angrystone");
  166. add_factory<BouncingSnowball>("bouncingsnowball", OBJ_PARAM_DISPENSABLE);
  167. add_factory<CaptainSnowball>("captainsnowball", OBJ_PARAM_DISPENSABLE);
  168. add_type_factory<CorruptedGranito>("skullyhop", CorruptedGranito::SKULLYHOP); // backward compatibility
  169. add_factory<CorruptedGranito>("corrupted_granito", OBJ_PARAM_DISPENSABLE);
  170. add_factory<CorruptedGranitoBig>("corrupted_granito_big", OBJ_PARAM_DISPENSABLE);
  171. add_factory<Crusher>("icecrusher"); // backward compatibility
  172. add_factory<Crusher>("crusher");
  173. add_factory<Crystallo>("crystallo", OBJ_PARAM_DISPENSABLE);
  174. add_factory<Dart>("dart", OBJ_PARAM_DISPENSABLE);
  175. add_factory<DartTrap>("darttrap");
  176. add_factory<Dispenser>("dispenser", OBJ_PARAM_DISPENSABLE);
  177. add_factory<DiveMine>("dive-mine", OBJ_PARAM_DISPENSABLE);
  178. add_factory<FishChasing>("fish-chasing", OBJ_PARAM_DISPENSABLE);
  179. add_factory<FishHarmless>("fish-harmless", OBJ_PARAM_DISPENSABLE);
  180. add_factory<FishJumping>("fish"); // backward compatibility
  181. add_factory<FishJumping>("fish-jumping", OBJ_PARAM_DISPENSABLE);
  182. add_factory<FishSwimming>("fish-swimming", OBJ_PARAM_DISPENSABLE);
  183. add_factory<Flame>("flame", OBJ_PARAM_DISPENSABLE);
  184. add_factory<FlyingSnowBall>("flyingsnowball", OBJ_PARAM_DISPENSABLE);
  185. add_type_factory<Flame>("ghostflame", Flame::GHOST); // Backward compatibility.
  186. add_factory<GhostTree>("ghosttree");
  187. add_factory<Ghoul>("ghoul", OBJ_PARAM_DISPENSABLE);
  188. add_factory<GoldBomb>("goldbomb", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  189. add_factory<Granito>("granito", OBJ_PARAM_DISPENSABLE);
  190. add_factory<GranitoBig>("granito_big", OBJ_PARAM_DISPENSABLE);
  191. add_factory<GranitoGiant>("granito_giant", OBJ_PARAM_DISPENSABLE);
  192. add_factory<Haywire>("haywire", OBJ_PARAM_DISPENSABLE);
  193. add_type_factory<Flame>("iceflame", Flame::ICE); // Backward compatibility.
  194. add_factory<Igel>("igel", OBJ_PARAM_DISPENSABLE);
  195. add_factory<Ispy>("ispy");
  196. add_factory<Jumpy>("jumpy", OBJ_PARAM_DISPENSABLE);
  197. add_factory<KamikazeSnowball>("kamikazesnowball", OBJ_PARAM_DISPENSABLE);
  198. add_factory<Kugelblitz>("kugelblitz", OBJ_PARAM_DISPENSABLE);
  199. add_factory<LeafShot>("leafshot", OBJ_PARAM_DISPENSABLE);
  200. add_factory<LiveFire>("livefire", OBJ_PARAM_DISPENSABLE);
  201. add_factory<LiveFireAsleep>("livefire_asleep", OBJ_PARAM_DISPENSABLE);
  202. add_factory<LiveFireDormant>("livefire_dormant", OBJ_PARAM_DISPENSABLE);
  203. add_factory<Mole>("mole");
  204. add_factory<MoleRock>("mole_rock", OBJ_PARAM_DISPENSABLE);
  205. add_factory<MrBomb>("mrbomb", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  206. add_factory<MrIceBlock>("mriceblock", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  207. add_factory<MrTree>("mrtree", OBJ_PARAM_DISPENSABLE);
  208. add_factory<Owl>("owl", OBJ_PARAM_DISPENSABLE);
  209. add_factory<Plant>("plant", OBJ_PARAM_DISPENSABLE);
  210. add_factory<RCrystallo>("rcrystallo", OBJ_PARAM_DISPENSABLE);
  211. add_factory<Root>("root");
  212. add_factory<RootSapling>("root_sapling");
  213. add_factory<SCrystallo>("scrystallo", OBJ_PARAM_DISPENSABLE);
  214. add_factory<ShortFuse>("short_fuse", OBJ_PARAM_DISPENSABLE);
  215. add_factory<SSpiky>("sspiky", OBJ_PARAM_DISPENSABLE);
  216. add_factory<SkyDive>("skydive", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  217. add_factory<SmartBall>("smartball", OBJ_PARAM_DISPENSABLE);
  218. add_factory<SmartBlock>("smartblock", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  219. add_factory<Snail>("snail", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  220. add_factory<SnowBall>("snowball", OBJ_PARAM_DISPENSABLE);
  221. add_factory<Snowman>("snowman", OBJ_PARAM_DISPENSABLE);
  222. add_factory<SpiderMite>("spidermite", OBJ_PARAM_DISPENSABLE);
  223. add_factory<Spiky>("spiky", OBJ_PARAM_DISPENSABLE);
  224. add_factory<Stalactite>("stalactite", OBJ_PARAM_DISPENSABLE);
  225. add_factory<Stumpy>("stumpy", OBJ_PARAM_DISPENSABLE);
  226. add_factory<Toad>("toad", OBJ_PARAM_DISPENSABLE);
  227. add_factory<Totem>("totem", OBJ_PARAM_DISPENSABLE);
  228. add_factory<ViciousIvy>("poisonivy"); // Backward compatibilty.
  229. add_factory<ViciousIvy>("viciousivy", OBJ_PARAM_DISPENSABLE);
  230. add_factory<WalkingCandle>("walking_candle", OBJ_PARAM_DISPENSABLE);
  231. add_factory<WalkingLeaf>("walkingleaf", OBJ_PARAM_DISPENSABLE);
  232. add_factory<WillOWisp>("willowisp", OBJ_PARAM_DISPENSABLE);
  233. add_factory<Yeti>("yeti");
  234. add_factory<YetiStalactite>("yeti_stalactite");
  235. add_factory<Zeekling>("zeekling", OBJ_PARAM_DISPENSABLE);
  236. m_adding_badguys = false;
  237. // Other objects.
  238. add_factory<AmbientLight>("ambient-light");
  239. add_factory<AmbientSound>("ambient_sound"); // Backward compatibilty.
  240. add_factory<AmbientSound>("ambient-sound");
  241. add_factory<Background>("background", OBJ_PARAM_WORLDMAP);
  242. add_factory<PathGameObject>("path");
  243. add_factory<BicyclePlatform>("bicycle-platform");
  244. add_factory<BonusBlock>("bonusblock", OBJ_PARAM_DISPENSABLE);
  245. add_factory<Brick>("brick", OBJ_PARAM_DISPENSABLE);
  246. add_factory<Bumper>("bumper");
  247. add_factory<Camera>("camera");
  248. add_factory<Candle>("candle");
  249. add_factory<CirclePlatform>("circleplatform");
  250. add_factory<CloudParticleSystem>("particles-clouds");
  251. add_factory<ConveyorBelt>("conveyor-belt");
  252. add_factory<CustomParticleSystem>("particles-custom");
  253. add_factory<CustomParticleSystemFile>("particles-custom-file");
  254. add_factory<Coin>("coin", OBJ_PARAM_DISPENSABLE);
  255. add_factory<Decal>("decal", OBJ_PARAM_WORLDMAP);
  256. add_factory<Explosion>("explosion", OBJ_PARAM_DISPENSABLE);
  257. add_factory<FallBlock>("fallblock", OBJ_PARAM_DISPENSABLE);
  258. add_factory<Firefly>("firefly");
  259. add_factory<GhostParticleSystem>("particles-ghosts");
  260. add_factory<Gradient>("gradient");
  261. add_factory<HeavyBrick>("heavy-brick", OBJ_PARAM_DISPENSABLE);
  262. add_factory<HeavyCoin>("heavycoin");
  263. add_factory<HurtingPlatform>("hurting_platform");
  264. add_factory<InfoBlock>("infoblock", OBJ_PARAM_DISPENSABLE);
  265. add_factory<InvisibleBlock>("invisible_block", OBJ_PARAM_DISPENSABLE);
  266. add_factory<InvisibleWall>("invisible_wall");
  267. add_factory<Key>("key");
  268. add_factory<Lantern>("lantern", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  269. add_factory<LevelTime>("leveltime");
  270. add_factory<LitObject>("lit-object");
  271. add_factory<MagicBlock>("magicblock");
  272. add_factory<ParticleZone>("particle-zone");
  273. add_factory<Platform>("platform");
  274. add_factory<PneumaticPlatform>("pneumatic-platform");
  275. add_factory<PowerUp>("powerup", OBJ_PARAM_DISPENSABLE);
  276. add_factory<PushButton>("pushbutton");
  277. add_factory<RainParticleSystem>("particles-rain");
  278. add_factory<Rock>("rock", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  279. add_factory<RubLight>("rublight", OBJ_PARAM_DISPENSABLE);
  280. add_factory<ScriptedObject>("scriptedobject");
  281. add_factory<Shard>("shard", OBJ_PARAM_DISPENSABLE);
  282. add_type_factory<UnstableTile>("skull_tile", UnstableTile::DELAYED); // Backward compatibility.
  283. add_factory<SoundObject>("sound-object");
  284. add_factory<SnowParticleSystem>("particles-snow");
  285. add_factory<Spotlight>("spotlight");
  286. add_factory<TextScroller>("textscroller");
  287. add_factory<Thunderstorm>("thunderstorm");
  288. add_factory<Torch>("torch");
  289. add_factory<Trampoline>("trampoline", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  290. add_factory<RustyTrampoline>("rustytrampoline", OBJ_PARAM_PORTABLE | OBJ_PARAM_DISPENSABLE);
  291. add_factory<UnstableTile>("unstable_tile");
  292. add_factory<WeakBlock>("weak_block", OBJ_PARAM_DISPENSABLE);
  293. add_factory<Wind>("wind");
  294. add_factory<TextArea>("text-area");
  295. // Triggers.
  296. add_factory<Climbable>("climbable");
  297. add_factory<Door>("door");
  298. add_factory<ScriptTrigger>("scripttrigger");
  299. add_factory<SecretAreaTrigger>("secretarea");
  300. add_factory<SequenceTrigger>("sequencetrigger");
  301. add_factory<Switch>("switch");
  302. // Editor stuff.
  303. add_factory<SpawnPointMarker>("spawnpoint");
  304. // Worldmap objects.
  305. add_factory<worldmap::LevelTile>("level", OBJ_PARAM_WORLDMAP);
  306. add_factory<worldmap::SpecialTile>("special-tile", OBJ_PARAM_WORLDMAP);
  307. add_factory<worldmap::SpriteChange>("sprite-change", OBJ_PARAM_WORLDMAP);
  308. add_factory<worldmap::Teleporter>("teleporter", OBJ_PARAM_WORLDMAP);
  309. add_factory<worldmap::SpawnPointObject>("worldmap-spawnpoint");
  310. add_factory("tilemap", {
  311. [](const ReaderMapping& reader) {
  312. auto tileset = TileManager::current()->get_tileset(Level::current()->get_tileset());
  313. return std::make_unique<TileMap>(tileset, reader);
  314. },
  315. TileMap::display_name
  316. });
  317. }
  318. std::unique_ptr<GameObject>
  319. GameObjectFactory::create(const std::string& name, const std::string& data) const
  320. {
  321. std::stringstream lisptext;
  322. lisptext << "(" << name << "\n" << data << ")";
  323. auto doc = ReaderDocument::from_stream(lisptext);
  324. return create(name, doc.get_root().get_mapping());
  325. }
  326. std::unique_ptr<GameObject>
  327. GameObjectFactory::create(const std::string& name, const Vector& pos, const Direction& dir, const std::string& data) const
  328. {
  329. std::stringstream lisptext;
  330. lisptext << "(" << name << "\n"
  331. << " (x " << pos.x << ")"
  332. << " (y " << pos.y << ")" << data;
  333. if (dir != Direction::AUTO) {
  334. lisptext << " (direction \"" << dir << "\"))";
  335. } else {
  336. lisptext << ")";
  337. }
  338. auto doc = ReaderDocument::from_stream(lisptext);
  339. return create(name, doc.get_root().get_mapping());
  340. }
  341. /* EOF */