supertux
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 "sprite/sprite_ptr.hpp"
21 #include "supertux/direction.hpp"
22 #include "supertux/moving_object.hpp"
23 #include "supertux/object_remove_listener.hpp"
24 #include "supertux/physic.hpp"
25 #include "supertux/player_status.hpp"
26 #include "supertux/sequence.hpp"
27 #include "supertux/timer.hpp"
28 #include "video/layer.hpp"
29 #include "video/surface_ptr.hpp"
30 
31 class BadGuy;
32 class Climbable;
33 class Controller;
34 class CodeController;
35 class Key;
36 class Portable;
37 
38 extern const float TUX_INVINCIBLE_TIME_WARNING;
39 
47 class Player final : public MovingObject
48 {
49 public:
50  static void register_class(ssq::VM& vm);
51 
52 public:
53  enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
54 
55 private:
56  class GrabListener final : public ObjectRemoveListener
57  {
58  public:
59  GrabListener(Player& player) : m_player(player)
60  {}
61 
62  virtual void object_removed(GameObject* object) override {
63  m_player.ungrab_object(object);
64  }
65 
66  private:
67  Player& m_player;
68 
69  private:
70  GrabListener(const GrabListener&) = delete;
71  GrabListener& operator=(const GrabListener&) = delete;
72  };
73 
74 public:
75  static Color get_player_color(int id);
76 
77 public:
78  Player(PlayerStatus& player_status, const std::string& name, int player_id);
79  ~Player() override;
80 
81  virtual void update(float dt_sec) override;
82  virtual void draw(DrawingContext& context) override;
83  virtual void collision_solid(const CollisionHit& hit) override;
84  virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
85  virtual void collision_tile(uint32_t tile_attributes) override;
86  virtual void on_flip(float height) override;
87  virtual bool is_saveable() const override { return false; }
88  virtual bool is_singleton() const override { return false; }
89  virtual bool has_object_manager_priority() const override { return true; }
90  virtual std::string get_exposed_class_name() const override { return "Player"; }
91  virtual void remove_me() override;
92  virtual GameObjectClasses get_class_types() const override { return MovingObject::get_class_types().add(typeid(Player)); }
93 
94  int get_id() const { return m_id; }
95  void set_id(int id);
96 
97  virtual int get_layer() const override { return LAYER_OBJECTS + 1; }
98 
99  void set_controller(const Controller* controller);
101  void set_winning();
102  bool is_winning() const { return m_winning; }
103 
104  // Tux can only go this fast. If set to 0 no special limit is used, only the default limits.
105  void set_speedlimit(float newlimit);
106  float get_speedlimit() const;
107 
108  const Controller& get_controller() const { return *m_controller; }
109 
115  void use_scripting_controller(bool enable);
124  void do_scripting_controller(const std::string& control, bool pressed);
125 
127  void move_to_sector(Sector& other);
128 
132  void make_invincible();
133  void make_temporarily_safe(float safe_time);
134 
135  bool is_invincible() const { return m_invincible_timer.started(); }
136  bool is_dying() const { return m_dying; }
137 
138  Direction peeking_direction_x() const { return m_peekingX; }
139  Direction peeking_direction_y() const { return m_peekingY; }
140 
146  void kill(bool completely);
147 
148  void set_pos(const Vector& vector) override;
149 
155  bool add_bonus(const std::string& bonus);
161  bool set_bonus(const std::string& bonus);
162 #ifdef DOXYGEN_SCRIPTING
163 
167  std::string get_bonus() const;
168 #endif
169 
178  void add_coins(int count);
183  int get_coins() const;
184 
189  bool add_bonus(BonusType type, bool animate = false);
190 
192  bool set_bonus(BonusType type, bool animate = false, bool increment_powerup_counter = true);
193  BonusType get_bonus() const;
194 
195  std::string bonus_to_string() const;
196 
197  PlayerStatus& get_status() const { return m_player_status; }
198 
203  void kick();
204 
209  std::string get_action() const;
210 
216  void do_cheer();
217 
222  void do_duck();
223 
228  void do_standup();
233  void do_standup(bool force_standup);
234 
239  void do_backflip();
240 
246  void do_jump(float yspeed);
247 
249  void add_velocity(const Vector& velocity);
250 
252  void add_velocity(const Vector& velocity, const Vector& end_speed);
253 
255  Vector get_velocity() const;
260  float get_velocity_x() const;
265  float get_velocity_y() const;
272  void set_velocity(float x, float y);
273 
274  void bounce(BadGuy& badguy);
275  void override_velocity() { m_velocity_override = true; }
276 
277  bool is_dead() const { return m_dead; }
278  bool is_big() const { return get_bonus() != NO_BONUS; }
279  bool is_stone() const { return m_stone; }
280  bool is_sliding() const { return m_sliding; }
281  bool is_swimming() const { return m_swimming; }
282  bool is_swimboosting() const { return m_swimboosting; }
283  bool is_water_jumping() const { return m_water_jump; }
284  bool is_skidding() const { return m_skidding_timer.started(); }
285  float get_swimming_angle() const { return m_swimming_angle; }
286 
293  void set_visible(bool visible);
299  bool get_visible() const;
300 
301  bool on_ground() const;
302  void set_on_ground(bool flag);
303 
304  Portable* get_grabbed_object() const { return m_grabbed_object; }
305  void stop_grabbing() { ungrab_object(); }
306 
312  bool has_grabbed(const std::string& name) const;
313 
319  void set_ghost_mode(bool enable);
324  bool get_ghost_mode() const;
325 
328  bool adjust_height(float new_height, float bottom_offset = 0);
329 
335  void trigger_sequence(const std::string& sequence_name);
336 
340  void trigger_sequence(const std::string& sequence_name, const SequenceData* data);
341 
345  void trigger_sequence(Sequence seq, const SequenceData* data = nullptr);
346 
348  void start_climbing(Climbable& climbable);
349 
351  void stop_climbing(Climbable& climbable);
352 
353  Physic& get_physic() { return m_physic; }
354 
359  void activate();
365  void deactivate();
366 
374  bool get_input_pressed(const std::string& input);
380  bool get_input_held(const std::string& input);
386  bool get_input_released(const std::string& input);
387 
393  void walk(float speed);
399  void set_dir(bool right);
400  void stop_backflipping();
401 
402  void position_grabbed_object(bool teleport = false);
403  bool try_grab();
404 
406  void sideways_push(float delta);
407 
408  void multiplayer_prepare_spawn();
409 
410  void set_ending_direction(int direction) { m_ending_direction = direction; }
411  int get_ending_direction() const { return m_ending_direction; }
412 
413  const std::vector<Key*>& get_collected_keys() const { return m_collected_keys; }
414  void add_collected_key(Key* key);
415  void remove_collected_key(Key* key);
416 
417  bool track_state() const override { return false; }
418 
419 private:
420  void handle_input();
421  void handle_input_ghost();
422  void handle_input_climbing();
423  void handle_input_rolling();
424 
425  void handle_input_swimming();
426 
427  void handle_horizontal_input();
428  void handle_vertical_input();
429 
431  void set_pos_reset(const Vector& vector);
432 
433  void do_jump_apex();
434  void early_jump_apex();
435 
436  void slide();
437  void swim(float pointx, float pointy, bool boost);
438 
439  BonusType string_to_bonus(const std::string& bonus) const;
440 
442  void apply_friction();
443 
444  void check_bounds();
445 
450  void ungrab_object(GameObject* gameobject = nullptr);
451 
452  void next_target();
453  void prev_target();
454 
455  void multiplayer_respawn();
456 
457  void stop_rolling(bool violent = true);
458 
459 private:
460  int m_id;
461  std::unique_ptr<UID> m_target;
462  bool m_deactivated;
463 
464  const Controller* m_controller;
465  std::unique_ptr<CodeController> m_scripting_controller;
466  PlayerStatus& m_player_status;
467  bool m_duck;
468  bool m_crawl;
469  bool m_dead;
470  bool m_dying;
471  bool m_winning;
472  bool m_backflipping;
473  int m_backflip_direction;
474  Direction m_peekingX;
475  Direction m_peekingY;
476  bool m_stone;
477  bool m_sliding;
478  bool m_slidejumping;
479  bool m_swimming;
480  bool m_swimboosting;
481  bool m_no_water;
482  bool m_on_left_wall;
483  bool m_on_right_wall;
484  bool m_in_walljump_tile;
485  bool m_can_walljump;
486  float m_boost;
487  float m_speedlimit;
488  bool m_velocity_override;
489  const Controller* m_scripting_controller_old;
490  bool m_jump_early_apex;
491  bool m_on_ice;
492  bool m_ice_this_frame;
493  //SpritePtr m_santahatsprite;
494  SpritePtr m_multiplayer_arrow;
495 
496  // Multiplayer tag stuff (number displayed over the players)
497  Timer m_tag_timer;
498  std::unique_ptr<FadeHelper> m_tag_fade;
499  float m_tag_alpha;
500  bool m_has_moved; // If the player sent input to move the player
501 
502 public:
503  Direction m_dir;
504 
505 private:
506  Direction m_old_dir;
507 
508 public:
509  float m_last_ground_y;
510  FallMode m_fall_mode;
511 
512 private:
513  bool m_on_ground_flag;
514  bool m_jumping;
515  bool m_can_jump;
516  Timer m_jump_button_timer;
517  Timer m_coyote_timer;
518  bool m_wants_buttjump;
519  bool m_buttjump_stomp;
520 
521 public:
522  bool m_does_buttjump;
523  Timer m_invincible_timer;
524 
525 private:
526  Timer m_skidding_timer;
527  Timer m_safe_timer;
528  bool m_is_intentionally_safe;
529  Timer m_kick_timer;
530  Timer m_buttjump_timer;
531 
532 public:
533  Timer m_dying_timer;
534 
535 private:
536  Timer m_second_growup_sound_timer;
537  bool m_growing;
538  Timer m_backflip_timer;
539 
540  Physic m_physic;
541 
542  bool m_visible;
543 
544  Portable* m_grabbed_object;
545  std::unique_ptr<ObjectRemoveListener> m_grabbed_object_remove_listener;
546  bool m_released_object;
547 
548  SpritePtr m_sprite;
550  float m_swimming_angle;
551  float m_swimming_accel_modifier;
552  bool m_water_jump;
553 
554  SurfacePtr m_airarrow;
556  Vector m_floor_normal;
557 
558  bool m_ghost_mode;
560  Timer m_unduck_hurt_timer;
562  Timer m_idle_timer;
563  unsigned int m_idle_stage;
564 
565  Climbable* m_climbing;
567  int m_ending_direction;
568  std::vector<Key*> m_collected_keys;
569 
570  float m_last_sliding_angle;
571  float m_current_sliding_angle;
572  float m_target_sliding_angle;
573  Timer m_sliding_rotation_timer;
574  bool m_is_slidejump_falling;
575  bool m_was_crawling_before_slide;
576 
577 private:
578  Player(const Player&) = delete;
579  Player& operator=(const Player&) = delete;
580 };
581 
582 #endif
583 
584 /* EOF */
void do_duck()
Makes Tux duck down, if possible.
Definition: player.cpp:1250
virtual void on_flip(float height) override
When level is flipped vertically.
Definition: player.cpp:2378
This class keeps player status between different game sessions (for example when switching maps in th...
Definition: player_status.hpp:39
Base class for moving sprites that can hurt the Player.
Definition: badguy.hpp:38
virtual bool has_object_manager_priority() const override
Indicates if the object should be added at the beginning of the object list.
Definition: player.hpp:89
Definition: controller.hpp:57
void activate()
Give control back to user/scripting.
Definition: player.cpp:2613
void set_velocity(float x, float y)
Sets the velocity of the player to a programmable/variable speed.
Definition: player.cpp:2595
void do_cheer()
Play cheer animation.
Definition: player.cpp:1242
void set_dir(bool right)
Face Tux in the proper direction.
Definition: player.cpp:2657
void add_velocity(const Vector &velocity)
Adds velocity to the player (be careful when using this)
Definition: player.cpp:2558
void use_scripting_controller(bool enable)
Uses a scriptable controller for all user input (or restores controls).
Definition: player.cpp:306
void make_invincible()
Make Tux invincible for a short amount of time.
Definition: player.cpp:2393
bool track_state() const override
Indicates if the object&#39;s state should be tracked.
Definition: player.hpp:417
virtual void draw(DrawingContext &context) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: player.cpp:1990
bool adjust_height(float new_height, float bottom_offset=0)
Changes height of bounding box.
Definition: player.cpp:347
Physics engine.
Definition: physic.hpp:27
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: player.hpp:92
void set_visible(bool visible)
Definition: player.cpp:1966
bool has_grabbed(const std::string &name) const
Returns whether the player is carrying a certain object.
Definition: player.cpp:2866
Vector get_velocity() const
Returns the current velocity of the player.
Definition: player.cpp:2577
Definition: object_remove_listener.hpp:22
Definition: sequence.hpp:33
void start_climbing(Climbable &climbable)
Requests that the player start climbing the given Climbable.
Definition: player.cpp:2692
float get_velocity_x() const
Returns Tux’s velocity in X direction.
Definition: player.cpp:2583
virtual bool is_singleton() const override
If true only a single object of this type is allowed in a given GameObjectManager.
Definition: player.hpp:88
An object that inherits from this object is considered "portable" and can be carried around by the pl...
Definition: portable.hpp:29
void move_to_sector(Sector &other)
Move the player to a different sector, including any objects that it points to, or references...
Definition: player.cpp:329
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: player.cpp:390
Represents one of (potentially) multiple, separate parts of a Level.
Definition: sector.hpp:61
void add_coins(int count)
Gives the player a number of coins.
Definition: player.cpp:1812
virtual bool is_saveable() const override
Indicates if the object will be saved.
Definition: player.hpp:87
bool get_visible() const
Definition: player.cpp:1972
bool get_input_held(const std::string &input)
Gets whether the current input on the keyboard/controller/touchpad is being held. ...
Definition: player.cpp:2639
void do_backflip()
Makes Tux do a backflip, if possible.
Definition: player.cpp:1318
void trigger_sequence(const std::string &sequence_name)
Orders the current ""GameSession"" to start a sequence.
Definition: player.cpp:369
virtual void collision_solid(const CollisionHit &hit) override
this function is called when the object collided with something solid
Definition: player.cpp:2279
Definition: climbable.hpp:28
void do_standup()
Makes Tux stand back up, if possible.
Definition: player.cpp:1276
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
bool add_bonus(const std::string &bonus)
Gives Tux the specified bonus unless Tux’s current bonus is superior.
Definition: player.cpp:1869
void sideways_push(float delta)
Boosts Tux in a certain direction, sideways.
Definition: player.cpp:2880
virtual HitResponse collision(GameObject &other, const CollisionHit &hit) override
this function is called when the object collided with any other object
Definition: player.cpp:2339
int get_coins() const
Returns the number of coins the player currently has.
Definition: player.cpp:1818
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
virtual void remove_me() override
schedules this object to be removed at the end of the frame
Definition: player.cpp:2386
void kill(bool completely)
Hurts Tux.
Definition: player.cpp:2409
void deactivate()
Deactivate user/scripting input for Tux.
Definition: player.cpp:2621
This is a dummy controller that doesn&#39;t react to any user input but should be controlled by code...
Definition: codecontroller.hpp:24
Definition: key.hpp:27
float get_velocity_y() const
Returns Tux’s velocity in Y direction.
Definition: player.cpp:2589
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_object.hpp:49
bool get_input_released(const std::string &input)
Gets whether the current input on the keyboard/controller/touchpad has been released.
Definition: player.cpp:2645
Definition: color.hpp:26
bool get_input_pressed(const std::string &input)
Gets whether the current input on the keyboard/controller/touchpad has been pressed.
Definition: player.cpp:2633
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
void do_jump(float yspeed)
Makes Tux jump in the air, if possible.
Definition: player.cpp:1334
void set_winning()
Level solved.
Definition: player.cpp:297
bool set_bonus(const std::string &bonus)
Gives Tux the specified bonus.
Definition: player.cpp:1875
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
std::string get_action() const
Gets the player&#39;s current action/animation.
Definition: player.cpp:1984
void set_ghost_mode(bool enable)
Switches ghost mode on/off.
Definition: player.cpp:2663
void stop_climbing(Climbable &climbable)
Requests that the player stop climbing the given Climbable.
Definition: player.cpp:2710
void walk(float speed)
Makes Tux walk.
Definition: player.cpp:2651
void kick()
Start kick animation.
Definition: player.cpp:1978
This module contains methods controlling the player.
Definition: player.hpp:47
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
void do_scripting_controller(const std::string &control, bool pressed)
Instructs the scriptable controller to press or release a button.
Definition: player.cpp:321
This class collects data about a collision.
Definition: collision_hit.hpp:44
bool get_ghost_mode() const
Returns whether ghost mode is currently enabled.
Definition: player.cpp:2686
virtual void collision_tile(uint32_t tile_attributes) override
called when tiles with special attributes have been touched
Definition: player.cpp:2253