8666ea5c24a570b558b93a38e3077c841e5fa66f
[supertux.git] / src / badguy / jumpy.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "jumpy.hpp"
23
24 #include "lisp/writer.hpp"
25 #include "sprite/sprite.hpp"
26 #include "object_factory.hpp"
27 #include "object/player.hpp"
28
29 static const float JUMPSPEED=-600;
30 static const float JUMPY_MID_TOLERANCE=4;
31 static const float JUMPY_LOW_TOLERANCE=2;
32
33 Jumpy::Jumpy(const lisp::Lisp& reader)
34     : BadGuy(reader, "images/creatures/snowjumpy/snowjumpy.sprite"), groundhit_pos_set(false)
35 {
36   // TODO create a nice sound for this...
37   //sound_manager->preload("sounds/skid.wav");
38 }
39
40 void
41 Jumpy::write(lisp::Writer& writer)
42 {
43   writer.start_list("jumpy");
44
45   writer.write("x", start_position.x);
46   writer.write("y", start_position.y);
47
48   writer.end_list("jumpy");
49 }
50
51 void
52 Jumpy::collision_solid(const CollisionHit& chit)
53 {
54   hit(chit);
55 }
56
57 HitResponse
58 Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
59 {
60   return hit(chit);
61 }
62
63 HitResponse
64 Jumpy::hit(const CollisionHit& chit)
65 {
66   if(chit.bottom) {
67     if (!groundhit_pos_set)
68     {
69       pos_groundhit = get_pos();
70       groundhit_pos_set = true;
71     }
72
73     physic.set_velocity_y((frozen || get_state() == STATE_FALLING) ? 0 : JUMPSPEED);
74     // TODO create a nice sound for this...
75     //sound_manager->play("sounds/skid.wav");
76   } else if(chit.top) {
77     physic.set_velocity_y(0);
78   }
79
80   return CONTINUE;
81 }
82
83 void
84 Jumpy::active_update(float elapsed_time)
85 {
86   BadGuy::active_update(elapsed_time);
87
88   if(frozen)
89     return;
90
91   Player* player = this->get_nearest_player();
92   if (player)
93   {
94     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
95   }
96
97   if (!groundhit_pos_set)
98   {
99     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
100     return;
101   }
102
103   if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
104     sprite->set_action(dir == LEFT ? "left-up" : "right-up");
105   else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
106       get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
107     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
108   else
109     sprite->set_action(dir == LEFT ? "left-down" : "right-down");
110 }
111
112 void
113 Jumpy::freeze()
114 {
115   BadGuy::freeze();
116   physic.set_velocity_y(std::max(0.0f, physic.get_velocity_y()));
117   sprite->set_action(dir == LEFT ? "left-iced" : "right-iced");
118 }
119
120 bool
121 Jumpy::is_freezable() const
122 {
123   return true;
124 }
125
126 IMPLEMENT_FACTORY(Jumpy, "jumpy")