fix cr/lfs and remove trailing whitespaces...
[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 static const float JUMPSPEED=-600;
25 static const float JUMPY_MID_TOLERANCE=4;
26 static const float JUMPY_LOW_TOLERANCE=2;
27
28 Jumpy::Jumpy(const lisp::Lisp& reader)
29     : BadGuy(reader, "images/creatures/jumpy/jumpy.sprite"), groundhit_pos_set(false)
30 {
31 }
32
33 void
34 Jumpy::write(lisp::Writer& writer)
35 {
36   writer.start_list("jumpy");
37
38   writer.write_float("x", start_position.x);
39   writer.write_float("y", start_position.y);
40
41   writer.end_list("jumpy");
42 }
43
44 void
45 Jumpy::collision_solid(const CollisionHit& chit)
46 {
47   hit(chit);
48 }
49
50 HitResponse
51 Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
52 {
53   return hit(chit);
54 }
55
56 HitResponse
57 Jumpy::hit(const CollisionHit& chit)
58 {
59   if(chit.bottom) {
60     if (!groundhit_pos_set)
61     {
62       pos_groundhit = get_pos();
63       groundhit_pos_set = true;
64     }
65
66     physic.set_velocity_y(JUMPSPEED);
67     // TODO create a nice sound for this...
68     //sound_manager->play("sounds/skid.wav");
69   } else if(chit.top) {
70     physic.set_velocity_y(0);
71   }
72
73   return CONTINUE;
74 }
75
76 void
77 Jumpy::active_update(float elapsed_time)
78 {
79   BadGuy::active_update(elapsed_time);
80
81   Player* player = this->get_nearest_player();
82   if (player)
83   {
84     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
85   }
86
87   if (!groundhit_pos_set)
88   {
89     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
90     return;
91   }
92
93   if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
94     sprite->set_action(dir == LEFT ? "left-up" : "right-up");
95   else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
96       get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
97     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
98   else
99     sprite->set_action(dir == LEFT ? "left-down" : "right-down");
100 }
101
102 IMPLEMENT_FACTORY(Jumpy, "jumpy")