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 "object/rock.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "object/explosion.hpp"
21#include "object/coin.hpp"
22#include "supertux/sector.hpp"
23#include "supertux/tile.hpp"
24#include "util/reader_mapping.hpp"
25
26namespace {
27const std::string ROCK_SOUND = "sounds/brick.wav"; //TODO use own sound.
28}
29
30Rock::Rock(const Vector& pos, const std::string& spritename) :
31 MovingSprite(pos, spritename),
32 ExposedObject<Rock, scripting::Rock>(this),
33 physic(),
34 on_ground(false),
35 last_movement(),
36 on_grab_script(),
37 on_ungrab_script()
38{
39 SoundManager::current()->preload(ROCK_SOUND);
40 set_group(COLGROUP_MOVING_STATIC);
41}
42
43Rock::Rock(const ReaderMapping& reader) :
44 MovingSprite(reader, "images/objects/rock/rock.sprite"),
45 ExposedObject<Rock, scripting::Rock>(this),
46 physic(),
47 on_ground(false),
48 last_movement(),
49 on_grab_script(),
50 on_ungrab_script()
51{
52 reader.get("on-grab-script", on_grab_script, "");
53 reader.get("on-ungrab-script", on_ungrab_script, "");
54 SoundManager::current()->preload(ROCK_SOUND);
55 set_group(COLGROUP_MOVING_STATIC);
56}
57
58Rock::Rock(const ReaderMapping& reader, const std::string& spritename) :
59 MovingSprite(reader, spritename),
60 ExposedObject<Rock, scripting::Rock>(this),
61 physic(),
62 on_ground(false),
63 last_movement(),
64 on_grab_script(),
65 on_ungrab_script()
66{
67 if (!reader.get("on-grab-script", on_grab_script)) on_grab_script = "";
68 if (!reader.get("on-ungrab-script", on_ungrab_script)) on_ungrab_script = "";
69 SoundManager::current()->preload(ROCK_SOUND);
70 set_group(COLGROUP_MOVING_STATIC);
71}
72
73void
74Rock::update(float dt_sec)
75{
76 if (!is_grabbed())
77 m_col.m_movement = physic.get_movement(dt_sec);
78}
79
80void
81Rock::collision_solid(const CollisionHit& hit)
82{
83 if (is_grabbed()) {
84 return;
85 }
86 if (hit.top || hit.bottom)
87 physic.set_velocity_y(0);
88 if (hit.left || hit.right) {
89 // Bounce back slightly when hitting a wall
90 float velx = physic.get_velocity_x();
91 physic.set_velocity_x(-0.1f * velx);
92 }
93 if (hit.crush)
94 physic.set_velocity(0, 0);
95
96 if (hit.bottom && !on_ground && !is_grabbed()) {
97 SoundManager::current()->play(ROCK_SOUND, get_pos());
98 physic.set_velocity_x(0);
99 on_ground = true;
100 }
101}
102
103HitResponse
104Rock::collision(GameObject& other, const CollisionHit& hit)
105{
106 auto heavy_coin = dynamic_cast<HeavyCoin*> (&other);
107 if (heavy_coin) {
108 return ABORT_MOVE;
109 }
110
111 auto explosion = dynamic_cast<Explosion*> (&other);
112 if (explosion) {
113 return ABORT_MOVE;
114 }
115
116 if (is_grabbed()) {
117 return ABORT_MOVE;
118 }
119 if (!on_ground) {
120 if (hit.bottom && physic.get_velocity_y() > 200) {
121 auto moving_object = dynamic_cast<MovingObject*> (&other);
122 if (moving_object) {
123 //Getting a rock on the head hurts. A lot.
124 moving_object->collision_tile(Tile::HURTS);
125 physic.set_velocity_y(0);
126 }
127 }
128 return FORCE_MOVE;
129 }
130
131 return FORCE_MOVE;
132}
133
134void
135Rock::grab(MovingObject& object, const Vector& pos, Direction dir_)
136{
137 Portable::grab(object, pos, dir_);
138 m_col.m_movement = pos - get_pos();
139 last_movement = m_col.m_movement;
140 set_group(COLGROUP_TOUCHABLE); //needed for lanterns catching willowisps
141 on_ground = false;
142
143 if (!on_grab_script.empty()) {
144 Sector::get().run_script(on_grab_script, "Rock::on_grab");
145 }
146}
147
148void
149Rock::ungrab(MovingObject& object, Direction dir)
150{
151 set_group(COLGROUP_MOVING_STATIC);
152 on_ground = false;
153 if (dir == Direction::UP) {
154 physic.set_velocity(0, -500);
155 } else if (dir == Direction::DOWN) {
156 physic.set_velocity(0, 500);
157 } else if (last_movement.norm() > 1) {
158 physic.set_velocity((dir == Direction::RIGHT) ? 200.0f : -200.0f, -200.0f);
159 } else {
160 physic.set_velocity(0, 0);
161 }
162
163 if (!on_ungrab_script.empty()) {
164 Sector::get().run_script(on_ungrab_script, "Rock::on_ungrab");
165 }
166 Portable::ungrab(object, dir);
167}
168
169ObjectSettings
170Rock::get_settings()
171{
172 auto result = MovingSprite::get_settings();
173 result.add_script(_("On-grab script"), &on_grab_script, "on-grab-script");
174 result.add_script(_("On-ungrab script"), &on_ungrab_script, "on-ungrab-script");
175 return result;
176}
177
178
179/* EOF */
180