Snowman enhancements: dead-script is passed to snowball, fireballs only kill body...
[supertux.git] / src / badguy / snowman.cpp
1 //  SuperTux
2 //  Copyright (C) 2010 Ingo Ruhnke <grumbel@gmx.de>
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 }
30
31 Snowman::Snowman(const Vector& pos, Direction d) :
32   WalkingBadguy(pos, d, "images/creatures/snowman/snowman.sprite", "walk-left", "walk-right")
33 {
34   walk_speed = 40;
35 }
36
37 void
38 Snowman::loose_head()
39 {
40   // replace with Snowball
41   Vector snowball_pos = get_pos();
42   // Hard-coded values from sprites
43   snowball_pos.x += 5;
44   snowball_pos.y += 1;
45
46   /* Create death animation for the (now headless) snowman. */
47   set_action (dir == LEFT ? "headless-left" : "headless-right", /* loops = */ -1);
48   set_pos (get_pos () + Vector (-4.0, 19.0)); /* difference in the sprite offsets */
49   physic.set_velocity_y(0);
50   physic.set_acceleration_y(0);
51   physic.enable_gravity(true);
52   set_state (STATE_FALLING);
53
54   /* Create a new snowball where the snowman's head was */
55   SnowBall* snowball = new SnowBall(snowball_pos, dir, dead_script);
56   Sector::current()->add_object(snowball);
57 }
58
59 HitResponse
60 Snowman::collision_bullet(Bullet& bullet, const CollisionHit& hit)
61 {
62   if(bullet.get_type() == FIRE_BONUS) {
63     // fire bullets destroy snowman's body
64     loose_head();
65
66     // FIXME: the sound used here should differ since there is still a threat
67     sound_manager->play("sounds/fall.wav", get_pos());
68     bullet.remove_me();
69
70     return ABORT_MOVE;
71   }
72   else {
73     // in all other cases, bullets ricochet
74     bullet.ricochet(*this, hit);
75     return FORCE_MOVE;
76   }
77 }
78
79 bool
80 Snowman::collision_squished(GameObject& object)
81 {
82   // bounce
83   Player* player = dynamic_cast<Player*>(&object);
84   if (player)
85     player->bounce(*this);
86
87   // FIXME: the squish sound isn't best here
88   sound_manager->play("sounds/squish.wav", get_pos());
89
90   loose_head();
91
92   return true;
93 }
94
95 /* EOF */