Badguys read hitbox from .sprite file
[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     : groundhit_pos_set(false)
30 {
31   reader.get("x", start_position.x);
32   reader.get("y", start_position.y);
33   sprite = sprite_manager->create("images/creatures/jumpy/jumpy.sprite");
34   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
35 }
36
37 void
38 Jumpy::write(lisp::Writer& writer)
39 {
40   writer.start_list("jumpy");
41
42   writer.write_float("x", start_position.x);
43   writer.write_float("y", start_position.y);
44
45   writer.end_list("jumpy");
46 }
47
48 HitResponse
49 Jumpy::collision_solid(GameObject& , const CollisionHit& chit)
50 {
51   return hit(chit);
52 }
53
54 HitResponse
55 Jumpy::collision_badguy(BadGuy& , const CollisionHit& chit)
56 {
57   return hit(chit);
58 }
59
60 HitResponse
61 Jumpy::hit(const CollisionHit& chit)
62 {
63   // hit floor?
64   if(chit.normal.y < -.5) {
65     if (!groundhit_pos_set)
66     {
67       pos_groundhit = get_pos();
68       groundhit_pos_set = true;
69     }
70     
71     physic.set_velocity_y(JUMPSPEED);
72     // TODO create a nice sound for this...
73     //sound_manager->play("sounds/skid.wav");
74   } else if(chit.normal.y < .5) { // bumped on roof
75     physic.set_velocity_y(0);
76   }
77
78   return CONTINUE;
79 }
80
81 void
82 Jumpy::active_update(float elapsed_time)
83 {
84   BadGuy::active_update(elapsed_time);
85   
86   Player* player = this->get_nearest_player();
87   if (player)
88   {
89     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
90   }
91     
92   if (!groundhit_pos_set)
93   {
94     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
95     return;
96   }
97   
98   if ( get_pos().y < (pos_groundhit.y - JUMPY_MID_TOLERANCE ) )
99     sprite->set_action(dir == LEFT ? "left-up" : "right-up");
100   else if ( get_pos().y >= (pos_groundhit.y - JUMPY_MID_TOLERANCE) &&
101       get_pos().y < (pos_groundhit.y - JUMPY_LOW_TOLERANCE) )
102     sprite->set_action(dir == LEFT ? "left-middle" : "right-middle");
103   else
104     sprite->set_action(dir == LEFT ? "left-down" : "right-down");
105 }
106
107 IMPLEMENT_FACTORY(Jumpy, "jumpy")