Set countMe flag to false for headless snomen
[supertux.git] / src / badguy / snowman.cpp
1 //  SuperTux
2 //  Copyright (C) 2010 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
17 #include "badguy/snowman.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/snowball.hpp"
21 #include "object/bullet.hpp"
22 #include "object/player.hpp"
23 #include "supertux/sector.hpp"
24
25 Snowman::Snowman(const Reader& reader) :
26   WalkingBadguy(reader, "images/creatures/snowman/snowman.sprite", "walk-left", "walk-right")
27 {
28   walk_speed = 40;
29   SoundManager::current()->preload("sounds/pop.ogg");
30 }
31
32 void
33 Snowman::loose_head()
34 {
35   // replace with Snowball
36   Vector snowball_pos = get_pos();
37   // Hard-coded values from sprites
38   snowball_pos.x += 5;
39   snowball_pos.y += 1;
40
41   /* Create death animation for the (now headless) snowman. */
42   set_action (dir == LEFT ? "headless-left" : "headless-right", /* loops = */ -1);
43   set_pos (get_pos () + Vector (-4.0, 19.0)); /* difference in the sprite offsets */
44   physic.set_velocity_y(0);
45   physic.set_acceleration_y(0);
46   physic.enable_gravity(true);
47   set_state (STATE_FALLING);
48   countMe = false;
49
50   /* Create a new snowball where the snowman's head was */
51   auto snowball = std::make_shared<SnowBall>(snowball_pos, dir, dead_script);
52   Sector::current()->add_object(snowball);
53 }
54
55 HitResponse
56 Snowman::collision_bullet(Bullet& bullet, const CollisionHit& hit)
57 {
58   if(bullet.get_type() == FIRE_BONUS) {
59     // fire bullets destroy snowman's body
60     loose_head();
61
62     SoundManager::current()->play("sounds/pop.ogg", get_pos()); // this could be a different sound
63     bullet.remove_me();
64
65     return ABORT_MOVE;
66   }
67   else {
68     // in all other cases, bullets ricochet
69     bullet.ricochet(*this, hit);
70     return FORCE_MOVE;
71   }
72 }
73
74 bool
75 Snowman::collision_squished(GameObject& object)
76 {
77   // bounce
78   Player* player = dynamic_cast<Player*>(&object);
79   if (player)
80     player->bounce(*this);
81
82   SoundManager::current()->play("sounds/pop.ogg", get_pos());
83
84   loose_head();
85
86   return true;
87 }
88
89 /* EOF */