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/bouncing_snowball.hpp"
18
19#include "sprite/sprite.hpp"
20
21#include <algorithm>
22
23static const float JUMPSPEED = -450;
24static const float BSNOWBALL_WALKSPEED = 80;
25
26BouncingSnowball::BouncingSnowball(const ReaderMapping& reader)
27 : BadGuy(reader, "images/creatures/bouncing_snowball/bouncing_snowball.sprite")
28{
29}
30
31void
32BouncingSnowball::initialize()
33{
34 m_physic.set_velocity_x(m_dir == Direction::LEFT ? -BSNOWBALL_WALKSPEED : BSNOWBALL_WALKSPEED);
35 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
36}
37
38bool
39BouncingSnowball::collision_squished(GameObject& object)
40{
41 m_sprite->set_action("squished");
42 kill_squished(object);
43 return true;
44}
45
46void
47BouncingSnowball::collision_solid(const CollisionHit& hit)
48{
49 if (m_sprite->get_action() == "squished")
50 {
51 return;
52 }
53
54 if (hit.bottom) {
55 if (get_state() == STATE_ACTIVE) {
56 float bounce_speed = -m_physic.get_velocity_y()*0.8f;
57 m_physic.set_velocity_y(std::min(JUMPSPEED, bounce_speed));
58 } else {
59 m_physic.set_velocity_y(0);
60 }
61 } else if (hit.top) {
62 m_physic.set_velocity_y(0);
63 }
64
65 // left or right collision
66 // The direction must correspond, else we got fake bounces on slopes.
67 if ((hit.left && m_dir == Direction::LEFT) || (hit.right && m_dir == Direction::RIGHT)) {
68 m_dir = m_dir == Direction::LEFT ? Direction::RIGHT : Direction::LEFT;
69 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
70 m_physic.set_velocity_x(-m_physic.get_velocity_x());
71 }
72
73}
74
75HitResponse
76BouncingSnowball::collision_badguy(BadGuy& , const CollisionHit& hit)
77{
78 collision_solid(hit);
79 return CONTINUE;
80}
81
82void
83BouncingSnowball::after_editor_set()
84{
85 BadGuy::after_editor_set();
86 m_sprite->set_action(m_dir == Direction::LEFT ? "left" : "right");
87}
88
89/* EOF */
90