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