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