Tux can peek to the left and to the right as far as the camera would move in best...
[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.8;
45 static const float TUX_INVINCIBLE_TIME = 10.0;
46 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0;
47 static const float GROWING_TIME = 0.35;
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 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
94 private:
95   bool dying;
96   bool backflipping;
97   int  backflip_direction;
98   Direction peeking;
99   
100 public:
101   Direction dir;
102   Direction old_dir;
103
104   float last_ground_y;
105   FallMode fall_mode;
106
107   bool on_ground_flag;
108   bool jumping;
109   bool can_jump;
110   bool butt_jump;
111
112   Timer invincible_timer;
113   Timer skidding_timer;
114   Timer safe_timer;
115   Timer kick_timer;
116   Timer shooting_timer;   // used to show the arm when Tux is shooting
117   Timer dying_timer;
118   Timer growing_timer;
119   Timer idle_timer;
120   Timer backflip_timer;
121   Physic physic;
122   
123 public:
124   Player(PlayerStatus* player_status);
125   virtual ~Player();
126
127   virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
128   virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
129
130   void set_controller(Controller* controller);
131   Controller* get_controller()
132   {
133     return controller;
134   }
135
136   virtual void update(float elapsed_time);
137   virtual void draw(DrawingContext& context);
138   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
139   virtual void collision_tile(uint32_t tile_attributes);
140
141   void make_invincible();
142   bool is_invincible() const
143   {
144     return invincible_timer.started();
145   }
146   bool is_dying() const
147   {
148     return dying;
149   }
150   Direction peeking_direction() const
151   {
152     return peeking;
153   }
154   
155   void kill(bool completely);
156   void check_bounds(Camera* camera);
157   void move(const Vector& vector);
158
159   virtual void add_bonus(const std::string& bonus);
160   virtual void add_coins(int count);
161   void add_bonus(BonusType type, bool animate = false); /**< picks up a bonus, taking care not to pick up lesser bonus items than we already have */
162   void set_bonus(BonusType type, bool animate = false); /**< like add_bonus, but can also downgrade the bonus items carried */
163   PlayerStatus* get_status()
164   {
165     return player_status;
166   }
167   // set kick animation
168   void kick();
169
170   /** 
171    * play cheer animation.
172    * This might need some space and behave in an unpredictable way. Best to use this at level end.
173    */
174   void do_cheer();
175
176   /**
177    * duck down if possible.
178    * this won't last long as long as input is enabled.
179    */
180   void do_duck();
181
182   /**
183    * stand back up if possible.
184    */
185   void do_standup();
186
187   /**
188    * do a backflip if possible.
189    */
190   void do_backflip();
191
192   /**
193    * jump in the air if possible
194    * sensible values for yspeed are negative - unless we want to jump into the ground of course
195    */
196   void do_jump(float yspeed);
197
198   /**
199    * Adds velocity to the player (be carefull when using this)
200    */
201   void add_velocity(const Vector& velocity);
202
203   /**
204    * Adds velocity to the player until given end speed is reached
205    */
206   void add_velocity(const Vector& velocity, const Vector& end_speed);
207
208   void bounce(BadGuy& badguy);
209
210   bool is_dead() const
211   { return dead; }
212   bool is_big();
213
214   void set_visible(bool visible);
215   bool get_visible();
216
217   bool on_ground();
218
219   Portable* get_grabbed_object() const
220   {
221       return grabbed_object;
222   }
223
224   /**
225    * Switches ghost mode on/off. 
226    * Lets Tux float around and through solid objects.
227    */
228   void set_ghost_mode(bool enable);
229
230   /**
231    * Returns whether ghost mode is currently enabled
232    */
233   bool get_ghost_mode() { return ghost_mode; }
234
235   /**
236    * Changes height of bounding box.
237    * Returns true if successful, false otherwise
238    */
239   bool adjust_height(float new_height);
240
241 private:
242   void handle_input();
243   void handle_input_ghost(); /**< input handling while in ghost mode */
244   bool deactivated;
245   
246   void init();
247   
248   void handle_horizontal_input();
249   void handle_vertical_input();
250
251   void activate();
252   void deactivate();
253   void walk(float speed);
254
255   /**
256    * slows Tux down a little, based on where he's standing
257    */
258   void apply_friction();
259
260   bool visible;
261
262   Portable* grabbed_object;
263
264   Sprite* smalltux_gameover;
265   Sprite* smalltux_star;
266   Sprite* bigtux_star;
267   Vector floor_normal;
268
269   bool ghost_mode; /**< indicates if Tux should float around and through solid objects */
270
271   Timer unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
272 };
273
274 #endif /*SUPERTUX_PLAYER_H*/