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