Support for idle animations. Idle stage sprite names must be 'stand', 'idle', 'idle...
[supertux.git] / src / object / player.hpp
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 #ifndef SUPERTUX_PLAYER_H
21 #define SUPERTUX_PLAYER_H
22
23 #include <vector>
24 #include <SDL.h>
25
26 #include "timer.hpp"
27 #include "direction.hpp"
28 #include "video/surface.hpp"
29 #include "moving_object.hpp"
30 #include "sprite/sprite.hpp"
31 #include "physic.hpp"
32 #include "control/controller.hpp"
33 #include "control/codecontroller.hpp"
34 #include "scripting/player.hpp"
35 #include "player_status.hpp"
36 #include "display_effect.hpp"
37 #include "script_interface.hpp"
38 #include "console.hpp"
39 #include "coin.hpp"
40
41 class BadGuy;
42 class Portable;
43 class Climbable;
44
45 /* Times: */
46 static const float TUX_SAFE_TIME = 1.8f;
47 static const float TUX_INVINCIBLE_TIME = 14.0f;
48 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0f;
49 static const float GROWING_TIME = 0.35f;
50 static const int GROWING_FRAMES = 7;
51
52 class Camera;
53 class PlayerStatus;
54
55 class Player : public MovingObject, public UsesPhysic, public Scripting::Player, public ScriptInterface
56 {
57 public:
58   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
59
60   Controller* controller;
61   CodeController* scripting_controller; /**< This controller is used when the Player is controlled via scripting */
62   PlayerStatus* player_status;
63   bool duck;
64   bool dead;
65   //Tux can only go this fast. If set to 0 no special limit is used, only the default limits.
66   void set_speedlimit(float newlimit);
67   float get_speedlimit();
68
69 private:
70   bool dying;
71   bool backflipping;
72   int  backflip_direction;
73   Direction peekingX, peekingY;
74   bool swimming;
75   float speedlimit;
76   Controller* scripting_controller_old; /**< Saves the old controller while the scripting_controller is used */
77   bool jump_early_apex;
78
79 public:
80   Direction dir;
81   Direction old_dir;
82
83   float last_ground_y;
84   FallMode fall_mode;
85
86   bool on_ground_flag;
87   bool jumping;
88   bool can_jump;
89   bool wants_buttjump;
90   bool does_buttjump;
91
92   Timer invincible_timer;
93   Timer skidding_timer;
94   Timer safe_timer;
95   Timer kick_timer;
96   Timer shooting_timer;   // used to show the arm when Tux is shooting
97   Timer dying_timer;
98   bool growing;
99   Timer backflip_timer;
100
101 public:
102   Player(PlayerStatus* player_status, const std::string& name);
103   virtual ~Player();
104
105   virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
106   virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
107
108   void set_controller(Controller* controller);
109   Controller* get_controller()
110   {
111     return controller;
112   }
113
114   void use_scripting_controller(bool use_or_release);
115   void do_scripting_controller(std::string control, bool pressed);
116
117   virtual void update(float elapsed_time);
118   virtual void draw(DrawingContext& context);
119   virtual void collision_solid(const CollisionHit& hit);
120   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
121   virtual void collision_tile(uint32_t tile_attributes);
122
123   void make_invincible();
124   bool is_invincible() const
125   {
126     return invincible_timer.started();
127   }
128   bool is_dying() const
129   {
130     return dying;
131   }
132   Direction peeking_direction_x() const
133   {
134     return peekingX;
135   }
136
137   Direction peeking_direction_y() const
138   {
139     return peekingY;
140   }
141
142   void kill(bool completely);
143   void check_bounds(Camera* camera);
144   void move(const Vector& vector);
145
146   virtual bool add_bonus(const std::string& bonus);
147   virtual void add_coins(int count);
148   virtual int get_coins();
149
150   /**
151    * picks up a bonus, taking care not to pick up lesser bonus items than we already have
152    *
153    * @returns true if the bonus has been set (or was already good enough)
154    *          false if the bonus could not be set (for example no space for big tux)
155    */
156   bool add_bonus(BonusType type, bool animate = false);
157   /**
158    * like add_bonus, but can also downgrade the bonus items carried
159    */
160   bool set_bonus(BonusType type, bool animate = false);
161
162   PlayerStatus* get_status()
163   {
164     return player_status;
165   }
166   // set kick animation
167   void kick();
168
169   /**
170    * play cheer animation.
171    * This might need some space and behave in an unpredictable way. Best to use this at level end.
172    */
173   void do_cheer();
174
175   /**
176    * duck down if possible.
177    * this won't last long as long as input is enabled.
178    */
179   void do_duck();
180
181   /**
182    * stand back up if possible.
183    */
184   void do_standup();
185
186   /**
187    * do a backflip if possible.
188    */
189   void do_backflip();
190
191   /**
192    * jump in the air if possible
193    * sensible values for yspeed are negative - unless we want to jump into the ground of course
194    */
195   void do_jump(float yspeed);
196
197   /**
198    * Adds velocity to the player (be careful when using this)
199    */
200   void add_velocity(const Vector& velocity);
201
202   /**
203    * Adds velocity to the player until given end speed is reached
204    */
205   void add_velocity(const Vector& velocity, const Vector& end_speed);
206   
207   /**
208    * Returns the current velocity of the player
209    */
210   Vector get_velocity();
211
212   void bounce(BadGuy& badguy);
213
214   bool is_dead() const
215   { return dead; }
216   bool is_big();
217
218   void set_visible(bool visible);
219   bool get_visible();
220
221   bool on_ground();
222
223   Portable* get_grabbed_object() const
224   {
225       return grabbed_object;
226   }
227   void stop_grabbing()
228   {
229     grabbed_object = NULL;
230   }
231
232   /**
233    * Switches ghost mode on/off.
234    * Lets Tux float around and through solid objects.
235    */
236   void set_ghost_mode(bool enable);
237
238   /**
239    * Switches edit mode on/off.
240    * In edit mode, Tux will enter ghost_mode instead of dying.
241    */
242   void set_edit_mode(bool enable);
243
244   /**
245    * Returns whether ghost mode is currently enabled
246    */
247   bool get_ghost_mode() { return ghost_mode; }
248
249   /**
250    * Changes height of bounding box.
251    * Returns true if successful, false otherwise
252    */
253   bool adjust_height(float new_height);
254
255   /**
256    * Orders the current GameSession to start a sequence
257    */
258   void trigger_sequence(std::string sequence_name);
259   
260   /**
261    * Requests that the player start climbing the given Climbable
262    */
263   void start_climbing(Climbable& climbable);
264
265   /**
266    * Requests that the player stop climbing the given Climbable
267    */
268   void stop_climbing(Climbable& climbable);
269
270 private:
271   void handle_input();
272   void handle_input_ghost(); /**< input handling while in ghost mode */
273   void handle_input_climbing(); /**< input handling while climbing */
274   bool deactivated;
275
276   void init();
277
278   void handle_horizontal_input();
279   void handle_vertical_input();
280
281   void activate();
282   void deactivate();
283   void walk(float speed);
284
285   void do_jump_apex();
286   void early_jump_apex();
287
288   /**
289    * slows Tux down a little, based on where he's standing
290    */
291   void apply_friction();
292
293   bool visible;
294
295   Portable* grabbed_object;
296
297   Sprite* sprite; /**< The main sprite representing Tux */
298   Sprite* smalltux_gameover;
299   Sprite* smalltux_star;
300   Sprite* bigtux_star;
301
302   std::auto_ptr<Surface> airarrow; /**< arrow indicating Tux' position when he's above the camera */
303
304   Vector floor_normal;
305   void try_grab();
306
307   bool ghost_mode; /**< indicates if Tux should float around and through solid objects */
308   bool edit_mode; /**< indicates if Tux should switch to ghost mode rather than dying */
309
310   Timer unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
311
312   Timer idle_timer;
313   unsigned int idle_stage;
314
315   Climbable* climbing; /**< Climbable object we are currently climbing, null if none */
316 };
317
318 #endif /*SUPERTUX_PLAYER_H*/