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/jumpy.hpp"
18
19#include <algorithm>
20
21#include "object/player.hpp"
22#include "sprite/sprite.hpp"
23
24static const float JUMPYSPEED=-600;
25static const float JUMPY_MID_TOLERANCE=4;
26static const float JUMPY_LOW_TOLERANCE=2;
27
28Jumpy::Jumpy(const ReaderMapping& reader) :
29 BadGuy(reader, "images/creatures/snowjumpy/snowjumpy.sprite"),
30 pos_groundhit(),
31 groundhit_pos_set(false)
32{
33 m_sprite->set_action("left-middle");
34 // TODO create a nice sound for this...
35 //SoundManager::current()->preload("sounds/skid.wav");
36}
37
38void
39Jumpy::collision_solid(const CollisionHit& chit)
40{
41 hit(chit);
42}
43
44HitResponse
45Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
46{
47 return hit(chit);
48}
49
50HitResponse
51Jumpy::hit(const CollisionHit& chit)
52{
53 if (chit.bottom) {
54 if (!groundhit_pos_set)
55 {
56 pos_groundhit = get_pos();
57 groundhit_pos_set = true;
58 }
59
60 m_physic.set_velocity_y((m_frozen || get_state() != STATE_ACTIVE) ? 0 : JUMPYSPEED);
61 // TODO create a nice sound for this...
62 //SoundManager::current()->play("sounds/skid.wav");
63 update_on_ground_flag(chit);
64 } else if (chit.top) {
65 m_physic.set_velocity_y(0);
66 }
67
68 return CONTINUE;
69}
70
71void
72Jumpy::active_update(float dt_sec)
73{
74 BadGuy::active_update(dt_sec);
75
76 if (m_frozen)
77 return;
78
79 auto player = get_nearest_player();
80 if (player)
81 {
82 m_dir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
83 }
84
85 if (!groundhit_pos_set)
86 {
87 m_sprite->set_action(m_dir == Direction::LEFT ? "left-middle" : "right-middle");
88 return;
89 }
90
91 if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
92 m_sprite->set_action(m_dir == Direction::LEFT ? "left-up" : "right-up");
93 else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
94 get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
95 m_sprite->set_action(m_dir == Direction::LEFT ? "left-middle" : "right-middle");
96 else
97 m_sprite->set_action(m_dir == Direction::LEFT ? "left-down" : "right-down");
98}
99
100void
101Jumpy::freeze()
102{
103 BadGuy::freeze();
104 m_physic.set_velocity_y(std::max(0.0f, m_physic.get_velocity_y()));
105}
106
107bool
108Jumpy::is_freezable() const
109{
110 return true;
111}
112
113bool
114Jumpy::is_flammable() const
115{
116 return true;
117}
118
119/* EOF */
120