* Revert breaking changes from last revision
[supertux.git] / src / badguy / jumpy.cpp
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 "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22
23 static const float JUMPYSPEED=-600;
24 static const float JUMPY_MID_TOLERANCE=4;
25 static const float JUMPY_LOW_TOLERANCE=2;
26
27 Jumpy::Jumpy(const Reader& reader) :
28   BadGuy(reader, "images/creatures/snowjumpy/snowjumpy.sprite"), 
29   pos_groundhit(),
30   groundhit_pos_set(false)
31 {
32   // TODO create a nice sound for this...
33   //sound_manager->preload("sounds/skid.wav");
34 }
35
36 void
37 Jumpy::collision_solid(const CollisionHit& chit)
38 {
39   hit(chit);
40 }
41
42 HitResponse
43 Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
44 {
45   return hit(chit);
46 }
47
48 HitResponse
49 Jumpy::hit(const CollisionHit& chit)
50 {
51   if(chit.bottom) {
52     if (!groundhit_pos_set)
53     {
54       pos_groundhit = get_pos();
55       groundhit_pos_set = true;
56     }
57
58     physic.set_velocity_y((frozen || get_state() == STATE_FALLING) ? 0 : JUMPYSPEED);
59     // TODO create a nice sound for this...
60     //sound_manager->play("sounds/skid.wav");
61   } else if(chit.top) {
62     physic.set_velocity_y(0);
63   }
64
65   return CONTINUE;
66 }
67
68 void
69 Jumpy::active_update(float elapsed_time)
70 {
71   BadGuy::active_update(elapsed_time);
72
73   if(frozen)
74     return;
75
76   Player* player = this->get_nearest_player();
77   if (player)
78   {
79     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
80   }
81
82   if (!groundhit_pos_set)
83   {
84     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
85     return;
86   }
87
88   if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
89     sprite->set_action(dir == LEFT ? "left-up" : "right-up");
90   else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
91             get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
92     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
93   else
94     sprite->set_action(dir == LEFT ? "left-down" : "right-down");
95 }
96
97 void
98 Jumpy::freeze()
99 {
100   BadGuy::freeze();
101   physic.set_velocity_y(std::max(0.0f, physic.get_velocity_y()));
102   sprite->set_action(dir == LEFT ? "left-iced" : "right-iced");
103 }
104
105 bool
106 Jumpy::is_freezable() const
107 {
108   return true;
109 }
110
111 /* EOF */