Made buttjump a bit easier to perform
[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 = 10.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 peeking;
74   bool swimming;
75   float speedlimit;
76   Controller* scripting_controller_old; /**< Saves the old controller while the scripting_controller is used */
77
78 public:
79   Direction dir;
80   Direction old_dir;
81
82   float last_ground_y;
83   FallMode fall_mode;
84
85   bool on_ground_flag;
86   bool jumping;
87   bool can_jump;
88   bool wants_buttjump;
89   bool does_buttjump;
90
91   Timer invincible_timer;
92   Timer skidding_timer;
93   Timer safe_timer;
94   Timer kick_timer;
95   Timer shooting_timer;   // used to show the arm when Tux is shooting
96   Timer dying_timer;
97   bool growing;
98   Timer idle_timer;
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() const
133   {
134     return peeking;
135   }
136
137   void kill(bool completely);
138   void check_bounds(Camera* camera);
139   void move(const Vector& vector);
140
141   virtual bool add_bonus(const std::string& bonus);
142   virtual void add_coins(int count);
143   virtual int get_coins();
144
145   /**
146    * picks up a bonus, taking care not to pick up lesser bonus items than we already have
147    *
148    * @returns true if the bonus has been set (or was already good enough)
149    *          false if the bonus could not be set (for example no space for big tux)
150    */
151   bool add_bonus(BonusType type, bool animate = false);
152   /**
153    * like add_bonus, but can also downgrade the bonus items carried
154    */
155   bool set_bonus(BonusType type, bool animate = false);
156
157   PlayerStatus* get_status()
158   {
159     return player_status;
160   }
161   // set kick animation
162   void kick();
163
164   /**
165    * play cheer animation.
166    * This might need some space and behave in an unpredictable way. Best to use this at level end.
167    */
168   void do_cheer();
169
170   /**
171    * duck down if possible.
172    * this won't last long as long as input is enabled.
173    */
174   void do_duck();
175
176   /**
177    * stand back up if possible.
178    */
179   void do_standup();
180
181   /**
182    * do a backflip if possible.
183    */
184   void do_backflip();
185
186   /**
187    * jump in the air if possible
188    * sensible values for yspeed are negative - unless we want to jump into the ground of course
189    */
190   void do_jump(float yspeed);
191
192   /**
193    * Adds velocity to the player (be carefull when using this)
194    */
195   void add_velocity(const Vector& velocity);
196
197   /**
198    * Adds velocity to the player until given end speed is reached
199    */
200   void add_velocity(const Vector& velocity, const Vector& end_speed);
201   
202   /**
203    * Returns the current velocity of the player
204    */
205   Vector get_velocity();
206
207   void bounce(BadGuy& badguy);
208
209   bool is_dead() const
210   { return dead; }
211   bool is_big();
212
213   void set_visible(bool visible);
214   bool get_visible();
215
216   bool on_ground();
217
218   Portable* get_grabbed_object() const
219   {
220       return grabbed_object;
221   }
222
223   /**
224    * Switches ghost mode on/off.
225    * Lets Tux float around and through solid objects.
226    */
227   void set_ghost_mode(bool enable);
228
229   /**
230    * Switches edit mode on/off.
231    * In edit mode, Tux will enter ghost_mode instead of dying.
232    */
233   void set_edit_mode(bool enable);
234
235   /**
236    * Returns whether ghost mode is currently enabled
237    */
238   bool get_ghost_mode() { return ghost_mode; }
239
240   /**
241    * Changes height of bounding box.
242    * Returns true if successful, false otherwise
243    */
244   bool adjust_height(float new_height);
245
246   /**
247    * Orders the current GameSession to start a sequence
248    */
249   void trigger_sequence(std::string sequence_name);
250   
251   /**
252    * Requests that the player start climbing the given Climbable
253    */
254   void start_climbing(Climbable& climbable);
255
256   /**
257    * Requests that the player stop climbing the given Climbable
258    */
259   void stop_climbing(Climbable& climbable);
260
261 private:
262   void handle_input();
263   void handle_input_ghost(); /**< input handling while in ghost mode */
264   void handle_input_climbing(); /**< input handling while climbing */
265   bool deactivated;
266
267   void init();
268
269   void handle_horizontal_input();
270   void handle_vertical_input();
271
272   void activate();
273   void deactivate();
274   void walk(float speed);
275
276   /**
277    * slows Tux down a little, based on where he's standing
278    */
279   void apply_friction();
280
281   bool visible;
282
283   Portable* grabbed_object;
284
285   Sprite* sprite; /**< The main sprite representing Tux */
286   Sprite* smalltux_gameover;
287   Sprite* smalltux_star;
288   Sprite* bigtux_star;
289
290   std::auto_ptr<Surface> airarrow; /**< arrow indicating Tux' position when he's above the camera */
291
292   Vector floor_normal;
293   void try_grab();
294
295   bool ghost_mode; /**< indicates if Tux should float around and through solid objects */
296   bool edit_mode; /**< indicates if Tux should switch to ghost mode rather than dying */
297
298   Timer unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
299
300   Climbable* climbing; /**< Climbable object we are currently climbing, null if none */
301 };
302
303 #endif /*SUPERTUX_PLAYER_H*/