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