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