1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.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/mrbomb.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "audio/sound_source.hpp"
21#include "badguy/bomb.hpp"
22#include "object/explosion.hpp"
23#include "object/player.hpp"
24#include "sprite/sprite.hpp"
25#include "sprite/sprite_manager.hpp"
26#include "supertux/sector.hpp"
27#include "util/reader_mapping.hpp"
28
29MrBomb::MrBomb(const ReaderMapping& reader) :
30 WalkingBadguy(reader, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right")
31{
32 walk_speed = 80;
33 max_drop_height = 16;
34
35 //Prevent stutter when Tux jumps on Mr Bomb
36 SoundManager::current()->preload("sounds/explosion.wav");
37
38 //Check if we need another sprite
39 if ( !reader.get( "sprite", m_sprite_name ) ){
40 return;
41 }
42 if (m_sprite_name.empty()) {
43 m_sprite_name = "images/creatures/mr_bomb/mr_bomb.sprite";
44 return;
45 }
46 //Replace sprite
47 m_sprite = SpriteManager::current()->create( m_sprite_name );
48}
49
50HitResponse
51MrBomb::collision(GameObject& object, const CollisionHit& hit)
52{
53 if (is_grabbed())
54 return FORCE_MOVE;
55 return WalkingBadguy::collision(object, hit);
56}
57
58HitResponse
59MrBomb::collision_player(Player& player, const CollisionHit& hit)
60{
61 if (is_grabbed())
62 return FORCE_MOVE;
63 return WalkingBadguy::collision_player(player, hit);
64}
65
66bool
67MrBomb::collision_squished(GameObject& object)
68{
69 auto player = dynamic_cast<Player*>(&object);
70 if (player && player->is_invincible()) {
71 player->bounce(*this);
72 kill_fall();
73 return true;
74 }
75 if (is_valid()) {
76 auto& bomb = Sector::get().add<Bomb>(get_pos(), m_dir, m_sprite_name);
77
78 // Do not trigger dispenser because we need to wait for
79 // the bomb instance to explode.
80 if (get_parent_dispenser() != nullptr)
81 {
82 bomb.set_parent_dispenser(get_parent_dispenser());
83 set_parent_dispenser(nullptr);
84 }
85
86 remove_me();
87 }
88 kill_squished(object);
89 return true;
90}
91
92void
93MrBomb::active_update(float dt_sec)
94{
95 if (is_grabbed())
96 return;
97 WalkingBadguy::active_update(dt_sec);
98}
99
100void
101MrBomb::kill_fall()
102{
103 if (is_valid()) {
104 remove_me();
105 Sector::get().add<Explosion>(m_col.m_bbox.get_middle());
106 }
107
108 run_dead_script();
109}
110
111void
112MrBomb::ignite()
113{
114 kill_fall();
115}
116
117void
118MrBomb::grab(MovingObject& object, const Vector& pos, Direction dir_)
119{
120 Portable::grab(object, pos, dir_);
121 assert(m_frozen);
122 m_col.m_movement = pos - get_pos();
123 m_dir = dir_;
124 m_sprite->set_action(dir_ == Direction::LEFT ? "iced-left" : "iced-right");
125 set_colgroup_active(COLGROUP_DISABLED);
126}
127
128void
129MrBomb::ungrab(MovingObject& object, Direction dir_)
130{
131 m_dir = dir_;
132 set_colgroup_active(COLGROUP_MOVING);
133 Portable::ungrab(object, dir_);
134}
135
136bool
137MrBomb::is_freezable() const
138{
139 return true;
140}
141
142bool
143MrBomb::is_portable() const
144{
145 return m_frozen;
146}
147
148/* EOF */
149