Set countMe flag to false for headless snomen
[supertux.git] / src / badguy / snowman.cpp
index 4a3cc2c..6170ae1 100644 (file)
@@ -1,5 +1,5 @@
 //  SuperTux
-//  Copyright (C) 2010 Ingo Ruhnke <grumbel@gmx.de>
+//  Copyright (C) 2010 Ingo Ruhnke <grumbel@gmail.com>
 //
 //  This program is free software: you can redistribute it and/or modify
 //  it under the terms of the GNU General Public License as published by
@@ -16,7 +16,9 @@
 
 #include "badguy/snowman.hpp"
 
+#include "audio/sound_manager.hpp"
 #include "badguy/snowball.hpp"
+#include "object/bullet.hpp"
 #include "object/player.hpp"
 #include "supertux/sector.hpp"
 
@@ -24,16 +26,11 @@ Snowman::Snowman(const Reader& reader) :
   WalkingBadguy(reader, "images/creatures/snowman/snowman.sprite", "walk-left", "walk-right")
 {
   walk_speed = 40;
+  SoundManager::current()->preload("sounds/pop.ogg");
 }
 
-Snowman::Snowman(const Vector& pos, Direction d) :
-  WalkingBadguy(pos, d, "images/creatures/snowman/snowman.sprite", "walk-left", "walk-right")
-{
-  walk_speed = 40;
-}
-
-bool
-Snowman::collision_squished(GameObject& object)
+void
+Snowman::loose_head()
 {
   // replace with Snowball
   Vector snowball_pos = get_pos();
@@ -41,11 +38,6 @@ Snowman::collision_squished(GameObject& object)
   snowball_pos.x += 5;
   snowball_pos.y += 1;
 
-  // bounce
-  Player* player = dynamic_cast<Player*>(&object);
-  if (player)
-    player->bounce(*this);
-
   /* Create death animation for the (now headless) snowman. */
   set_action (dir == LEFT ? "headless-left" : "headless-right", /* loops = */ -1);
   set_pos (get_pos () + Vector (-4.0, 19.0)); /* difference in the sprite offsets */
@@ -53,11 +45,43 @@ Snowman::collision_squished(GameObject& object)
   physic.set_acceleration_y(0);
   physic.enable_gravity(true);
   set_state (STATE_FALLING);
+  countMe = false;
 
   /* Create a new snowball where the snowman's head was */
-  /* TODO: Pass on our "dead_script" to the snowball. */
-  SnowBall* snowball = new SnowBall(snowball_pos, dir);
+  auto snowball = std::make_shared<SnowBall>(snowball_pos, dir, dead_script);
   Sector::current()->add_object(snowball);
+}
+
+HitResponse
+Snowman::collision_bullet(Bullet& bullet, const CollisionHit& hit)
+{
+  if(bullet.get_type() == FIRE_BONUS) {
+    // fire bullets destroy snowman's body
+    loose_head();
+
+    SoundManager::current()->play("sounds/pop.ogg", get_pos()); // this could be a different sound
+    bullet.remove_me();
+
+    return ABORT_MOVE;
+  }
+  else {
+    // in all other cases, bullets ricochet
+    bullet.ricochet(*this, hit);
+    return FORCE_MOVE;
+  }
+}
+
+bool
+Snowman::collision_squished(GameObject& object)
+{
+  // bounce
+  Player* player = dynamic_cast<Player*>(&object);
+  if (player)
+    player->bounce(*this);
+
+  SoundManager::current()->play("sounds/pop.ogg", get_pos());
+
+  loose_head();
 
   return true;
 }