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