- Yet another try in the endless quest for perfect collision detection.
[supertux.git] / src / object / player.hpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.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 #ifndef SUPERTUX_PLAYER_H
20 #define SUPERTUX_PLAYER_H
21
22 #include <vector>
23 #include <SDL.h>
24
25 #include "timer.hpp"
26 #include "direction.hpp"
27 #include "video/surface.hpp"
28 #include "moving_object.hpp"
29 #include "sprite/sprite.hpp"
30 #include "physic.hpp"
31 #include "control/controller.hpp"
32 #include "scripting/player.hpp"
33 #include "player_status.hpp"
34 #include "display_effect.hpp"
35
36 class BadGuy;
37 class Portable;
38
39 /* Times: */
40 static const float TUX_SAFE_TIME = 1.8;
41 static const float TUX_INVINCIBLE_TIME = 10.0;
42 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0;
43 static const float GROWING_TIME = 1.0;
44 static const int GROWING_FRAMES = 7;
45
46 class Camera;
47 class PlayerStatus;
48
49 extern Surface* growingtux_left[GROWING_FRAMES];
50 extern Surface* growingtux_right[GROWING_FRAMES];
51
52 class TuxBodyParts
53 {
54 public:
55   TuxBodyParts()
56     : head(0), body(0), arms(0), feet(0)
57   { }
58   ~TuxBodyParts() {
59     delete head;
60     delete body;
61     delete arms;
62     delete feet;
63   }
64
65   void set_action(std::string action, int loops = -1);
66   void one_time_animation();
67   void draw(DrawingContext& context, const Vector& pos, int layer);
68
69   Sprite* head;
70   Sprite* body;
71   Sprite* arms;
72   Sprite* feet;
73 };
74
75 extern TuxBodyParts* small_tux;
76 extern TuxBodyParts* big_tux;
77 extern TuxBodyParts* fire_tux;
78 extern TuxBodyParts* ice_tux;
79
80 class Player : public MovingObject, public Scripting::Player
81 {
82 public:
83   enum HurtMode { KILL, SHRINK };
84   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
85
86   Controller* controller;
87   PlayerStatus* player_status;
88   bool duck;
89   bool dead;
90
91 private:
92   bool dying;
93   bool backflipping;
94   int  backflip_direction;
95   
96 public:
97   Direction dir;
98   Direction old_dir;
99
100   float last_ground_y;
101   FallMode fall_mode;
102
103   bool on_ground_flag;
104   bool jumping;
105   bool can_jump;
106   bool butt_jump;
107
108   Timer invincible_timer;
109   Timer skidding_timer;
110   Timer safe_timer;
111   Timer kick_timer;
112   Timer shooting_timer;   // used to show the arm when Tux is shooting
113   Timer dying_timer;
114   Timer growing_timer;
115   Timer idle_timer;
116   Timer backflip_timer;
117   Physic physic;
118   
119 public:
120   Player(PlayerStatus* player_status);
121   virtual ~Player();
122
123   void set_controller(Controller* controller);  
124
125   virtual void update(float elapsed_time);
126   virtual void draw(DrawingContext& context);
127   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
128   virtual void collision_tile(uint32_t tile_attributes);
129
130   void make_invincible();
131   bool is_invincible() const
132   {
133     return invincible_timer.started();
134   }
135   bool is_dying() const
136   {
137     return dying;
138   }
139   
140   void kill(HurtMode mode);
141   void check_bounds(Camera* camera);
142   void move(const Vector& vector);
143   void set_bonus(BonusType type, bool animate = false);
144   PlayerStatus* get_status()
145   {
146     return player_status;
147   }
148   // set kick animation
149   void kick();
150
151   /**
152    * Adds velocity to the player (be carefull when using this)
153    */
154   void add_velocity(const Vector& velocity);
155
156   void bounce(BadGuy& badguy);
157
158   bool is_dead() const
159   { return dead; }
160   bool is_big();
161
162   void set_visible(bool visible);
163   bool get_visible();
164   
165 private:
166   void handle_input();
167   bool on_ground();
168   bool deactivated;
169   
170   void init();
171   
172   void handle_horizontal_input();
173   void handle_vertical_input();
174
175   void activate();
176   void deactivate();
177   void walk(float speed);
178
179   bool visible;
180
181   float adjust_height;
182
183   Portable* grabbed_object;
184
185   Sprite* smalltux_gameover;
186   Sprite* smalltux_star;
187   Sprite* bigtux_star;
188   Vector floor_normal;
189 };
190
191 #endif /*SUPERTUX_PLAYER_H*/