* Add Airship (regular above-ground theme) and Battle (castle/boss theme) music court...
[supertux.git] / src / badguy / dispenser.cpp
index 81a1fa6..a82136a 100644 (file)
 #include <config.h>
 
 #include "dispenser.hpp"
+
 #include "object/bullet.hpp"
 #include "random_generator.hpp"
+#include "lisp/writer.hpp"
+#include "object_factory.hpp"
+#include "audio/sound_manager.hpp"
+#include "sector.hpp"
+#include "object/player.hpp"
+#include "log.hpp"
+
+#include <stdexcept>
 
 Dispenser::Dispenser(const lisp::Lisp& reader)
-       : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
+        : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
 {
   set_colgroup_active(COLGROUP_MOVING_STATIC);
   sound_manager->preload("sounds/squish.wav");
   reader.get("cycle", cycle);
-  reader.get_vector("badguy", badguys);
+  reader.get("badguy", badguys);
   random = false; // default
   reader.get("random", random);
   type = "dropper"; //default
@@ -42,7 +51,7 @@ Dispenser::Dispenser(const lisp::Lisp& reader)
   if (badguys.size() <= 0)
     throw std::runtime_error("No badguys in dispenser.");
 
-  if (type == "rocket_launcher") {
+  if (type == "rocketlauncher") {
     sprite->set_action(dir == LEFT ? "working-left" : "working-right");
     set_colgroup_active(COLGROUP_MOVING); //if this were COLGROUP_MOVING_STATIC MrRocket would explode on launch.
 
@@ -64,12 +73,12 @@ Dispenser::write(lisp::Writer& writer)
 {
   writer.start_list("dispenser");
 
-  writer.write_float("x", start_position.x);
-  writer.write_float("y", start_position.y);
-  writer.write_float("cycle", cycle);
-  writer.write_bool("random", random);
-  writer.write_string("type", type);
-  writer.write_string_vector("badguy", badguys);
+  writer.write("x", start_position.x);
+  writer.write("y", start_position.y);
+  writer.write("cycle", cycle);
+  writer.write("random", random);
+  writer.write("type", type);
+  writer.write("badguy", badguys);
 
   writer.end_list("dispenser");
 }
@@ -102,8 +111,8 @@ bool
 Dispenser::collision_squished(GameObject& object)
 {
   //Cannon launching MrRocket can be broken by jumping on it
-  //other dispencers are not that fragile.
-  if (broken || type != "rocket_launcher") {
+  //other dispensers are not that fragile.
+  if (broken || type != "rocketlauncher") {
     return false;
   }
 
@@ -198,17 +207,32 @@ Dispenser::launch_badguy()
     }
 
     std::string badguy = badguys[next_badguy];
+
+    if(badguy == "random") {
+      log_warning << "random is outdated; use a list of badguys to select from." << std::endl;
+      return;
+    }
+
     GameObject* badguy_object = NULL;
 
-    if (type == "dropper")
-      badguy_object = create_badguy_object(badguy, Vector(get_pos().x, get_pos().y+32), launchdir);
-    else if (type == "cannon")
-      badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
-    else if (type == "rocket_launcher")
-      badguy_object = create_badguy_object(badguy, Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y), launchdir);
+    try {
+      Vector spawnpoint;
+
+      if (type == "dropper")
+        spawnpoint = Vector(get_pos().x, get_pos().y+32);
+      else if (type == "cannon")
+        spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
+      else if (type == "rocketlauncher")
+        spawnpoint = Vector(get_pos().x + (launchdir == LEFT ? -32 : 32), get_pos().y);
 
-    if (badguy_object)
-      Sector::current()->add_object(badguy_object);
+      badguy_object = create_object(badguy, Vector(get_pos().x, get_pos().y+32), launchdir);
+
+      if (badguy_object)
+        Sector::current()->add_object(badguy_object);
+    } catch(std::exception& e) {
+      log_warning << "Error dispensing badguy: " << e.what() << std::endl;
+      return;
+    }
   }
 }