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