player.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #ifndef HEADER_SUPERTUX_OBJECT_PLAYER_HPP
  17. #define HEADER_SUPERTUX_OBJECT_PLAYER_HPP
  18. #include "scripting/player.hpp"
  19. #include "sprite/sprite_ptr.hpp"
  20. #include "squirrel/exposed_object.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. class BadGuy;
  31. class Climbable;
  32. class Controller;
  33. class CodeController;
  34. class Key;
  35. class Portable;
  36. extern const float TUX_INVINCIBLE_TIME_WARNING;
  37. class Player final : public MovingObject,
  38. public ExposedObject<Player, scripting::Player>
  39. {
  40. public:
  41. enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
  42. private:
  43. class GrabListener final : public ObjectRemoveListener
  44. {
  45. public:
  46. GrabListener(Player& player) : m_player(player)
  47. {}
  48. virtual void object_removed(GameObject* object) override {
  49. m_player.ungrab_object(object);
  50. }
  51. private:
  52. Player& m_player;
  53. private:
  54. GrabListener(const GrabListener&) = delete;
  55. GrabListener& operator=(const GrabListener&) = delete;
  56. };
  57. public:
  58. static Color get_player_color(int id);
  59. public:
  60. Player(PlayerStatus& player_status, const std::string& name, int player_id);
  61. ~Player() override;
  62. virtual void update(float dt_sec) override;
  63. virtual void draw(DrawingContext& context) override;
  64. virtual void collision_solid(const CollisionHit& hit) override;
  65. virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
  66. virtual void collision_tile(uint32_t tile_attributes) override;
  67. virtual void on_flip(float height) override;
  68. virtual bool is_saveable() const override { return false; }
  69. virtual bool is_singleton() const override { return false; }
  70. virtual bool has_object_manager_priority() const override { return true; }
  71. virtual void remove_me() override;
  72. int get_id() const { return m_id; }
  73. void set_id(int id);
  74. virtual int get_layer() const override { return LAYER_OBJECTS + 1; }
  75. void set_controller(const Controller* controller);
  76. /** Level solved. Don't kill Tux any more. */
  77. void set_winning();
  78. bool is_winning() const { return m_winning; }
  79. // Tux can only go this fast. If set to 0 no special limit is used, only the default limits.
  80. void set_speedlimit(float newlimit);
  81. float get_speedlimit() const;
  82. const Controller& get_controller() const { return *m_controller; }
  83. void use_scripting_controller(bool use_or_release);
  84. void do_scripting_controller(const std::string& control, bool pressed);
  85. /** Move the player to a different sector, including any objects that it points to, or references. */
  86. void move_to_sector(Sector& other);
  87. void make_invincible();
  88. void make_temporarily_safe(float safe_time);
  89. bool is_invincible() const { return m_invincible_timer.started(); }
  90. bool is_dying() const { return m_dying; }
  91. Direction peeking_direction_x() const { return m_peekingX; }
  92. Direction peeking_direction_y() const { return m_peekingY; }
  93. void kill(bool completely);
  94. void move(const Vector& vector);
  95. bool add_bonus(const std::string& bonus);
  96. bool set_bonus(const std::string& bonus);
  97. void add_coins(int count);
  98. int get_coins() const;
  99. /** picks up a bonus, taking care not to pick up lesser bonus items than we already have
  100. @returns true if the bonus has been set (or was already good enough)
  101. false if the bonus could not be set (for example no space for big tux) */
  102. bool add_bonus(BonusType type, bool animate = false);
  103. /** like add_bonus, but can also downgrade the bonus items carried */
  104. bool set_bonus(BonusType type, bool animate = false, bool increment_powerup_counter = true);
  105. BonusType get_bonus() const;
  106. std::string bonus_to_string() const;
  107. PlayerStatus& get_status() const { return m_player_status; }
  108. /** set kick animation */
  109. void kick();
  110. /** gets the players action */
  111. std::string get_action() const;
  112. /** play cheer animation.
  113. This might need some space and behave in an unpredictable way.
  114. Best to use this at level end. */
  115. void do_cheer();
  116. /** duck down if possible.
  117. this won't last long as long as input is enabled. */
  118. void do_duck();
  119. /** stand back up if possible. */
  120. void do_standup(bool force_standup);
  121. /** do a backflip if possible. */
  122. void do_backflip();
  123. /** jump in the air if possible
  124. sensible values for yspeed are negative - unless we want to jump
  125. into the ground of course */
  126. void do_jump(float yspeed);
  127. /** Adds velocity to the player (be careful when using this) */
  128. void add_velocity(const Vector& velocity);
  129. /** Adds velocity to the player until given end speed is reached */
  130. void add_velocity(const Vector& velocity, const Vector& end_speed);
  131. /** Returns the current velocity of the player */
  132. Vector get_velocity() const;
  133. void bounce(BadGuy& badguy);
  134. void override_velocity() { m_velocity_override = true; }
  135. bool is_dead() const { return m_dead; }
  136. bool is_big() const { return get_bonus() != NO_BONUS; }
  137. bool is_stone() const { return m_stone; }
  138. bool is_sliding() const { return m_sliding; }
  139. bool is_swimming() const { return m_swimming; }
  140. bool is_swimboosting() const { return m_swimboosting; }
  141. bool is_water_jumping() const { return m_water_jump; }
  142. bool is_skidding() const { return m_skidding_timer.started(); }
  143. float get_swimming_angle() const { return m_swimming_angle; }
  144. void set_visible(bool visible);
  145. bool get_visible() const;
  146. bool on_ground() const;
  147. void set_on_ground(bool flag);
  148. Portable* get_grabbed_object() const { return m_grabbed_object; }
  149. void stop_grabbing() { ungrab_object(); }
  150. /** Checks whether the player has grabbed a certain object
  151. @param name Name of the object to check */
  152. bool has_grabbed(const std::string& object_name) const;
  153. /** Switches ghost mode on/off.
  154. Lets Tux float around and through solid objects. */
  155. void set_ghost_mode(bool enable);
  156. /** Returns whether ghost mode is currently enabled */
  157. bool get_ghost_mode() const { return m_ghost_mode; }
  158. /** Changes height of bounding box.
  159. Returns true if successful, false otherwise */
  160. bool adjust_height(float new_height, float bottom_offset = 0);
  161. /** Orders the current GameSession to start a sequence
  162. @param sequence_name Name of the sequence to start
  163. @param data Custom additional sequence data */
  164. void trigger_sequence(const std::string& sequence_name, const SequenceData* data = nullptr);
  165. /** Orders the current GameSession to start a sequence
  166. @param sequence Sequence to start
  167. @param data Custom additional sequence data */
  168. void trigger_sequence(Sequence seq, const SequenceData* data = nullptr);
  169. /** Requests that the player start climbing the given Climbable */
  170. void start_climbing(Climbable& climbable);
  171. /** Requests that the player stop climbing the given Climbable */
  172. void stop_climbing(Climbable& climbable);
  173. Physic& get_physic() { return m_physic; }
  174. void activate();
  175. void deactivate();
  176. void walk(float speed);
  177. void set_dir(bool right);
  178. void stop_backflipping();
  179. void position_grabbed_object(bool teleport = false);
  180. bool try_grab();
  181. /** Boosts Tux in a certain direction, sideways. Useful for bumpers/walljumping. */
  182. void sideways_push(float delta);
  183. void multiplayer_prepare_spawn();
  184. void set_ending_direction(int direction) { m_ending_direction = direction; }
  185. int get_ending_direction() const { return m_ending_direction; }
  186. const std::vector<Key*>& get_collected_keys() const { return m_collected_keys; }
  187. void add_collected_key(Key* key);
  188. void remove_collected_key(Key* key);
  189. bool track_state() const override { return false; }
  190. private:
  191. void handle_input();
  192. void handle_input_ghost(); /**< input handling while in ghost mode */
  193. void handle_input_climbing(); /**< input handling while climbing */
  194. void handle_input_rolling();
  195. void handle_input_swimming();
  196. void handle_horizontal_input();
  197. void handle_vertical_input();
  198. void do_jump_apex();
  199. void early_jump_apex();
  200. void slide();
  201. void swim(float pointx, float pointy, bool boost);
  202. BonusType string_to_bonus(const std::string& bonus) const;
  203. /** slows Tux down a little, based on where he's standing */
  204. void apply_friction();
  205. void check_bounds();
  206. /**
  207. * Ungrabs the currently grabbed object, if any. Only call with its argument
  208. * from an ObjectRemoveListener.
  209. */
  210. void ungrab_object(GameObject* gameobject = nullptr);
  211. void next_target();
  212. void prev_target();
  213. void multiplayer_respawn();
  214. void stop_rolling(bool violent = true);
  215. private:
  216. int m_id;
  217. std::unique_ptr<UID> m_target; /**< (Multiplayer) If not null, then the player does not exist in game and is offering the player to spawn at that player's position */
  218. bool m_deactivated;
  219. const Controller* m_controller;
  220. std::unique_ptr<CodeController> m_scripting_controller; /**< This controller is used when the Player is controlled via scripting */
  221. PlayerStatus& m_player_status;
  222. bool m_duck;
  223. bool m_crawl;
  224. bool m_dead;
  225. bool m_dying;
  226. bool m_winning;
  227. bool m_backflipping;
  228. int m_backflip_direction;
  229. Direction m_peekingX;
  230. Direction m_peekingY;
  231. bool m_stone;
  232. bool m_sliding;
  233. bool m_slidejumping;
  234. bool m_swimming;
  235. bool m_swimboosting;
  236. bool m_no_water;
  237. bool m_on_left_wall;
  238. bool m_on_right_wall;
  239. bool m_in_walljump_tile;
  240. bool m_can_walljump;
  241. float m_boost;
  242. float m_speedlimit;
  243. bool m_velocity_override;
  244. const Controller* m_scripting_controller_old; /**< Saves the old controller while the scripting_controller is used */
  245. bool m_jump_early_apex;
  246. bool m_on_ice;
  247. bool m_ice_this_frame;
  248. //SpritePtr m_santahatsprite;
  249. SpritePtr m_multiplayer_arrow;
  250. // Multiplayer tag stuff (number displayed over the players)
  251. Timer m_tag_timer;
  252. std::unique_ptr<FadeHelper> m_tag_fade;
  253. float m_tag_alpha;
  254. bool m_has_moved; // If the player sent input to move the player
  255. public:
  256. Direction m_dir;
  257. private:
  258. Direction m_old_dir;
  259. public:
  260. float m_last_ground_y;
  261. FallMode m_fall_mode;
  262. private:
  263. bool m_on_ground_flag;
  264. bool m_jumping;
  265. bool m_can_jump;
  266. Timer m_jump_button_timer; /**< started when player presses the jump button; runs until Tux jumps or JUMP_GRACE_TIME runs out */
  267. Timer m_coyote_timer; /**< started when Tux falls off a ledge; runs until Tux jumps or COYOTE_TIME runs out */
  268. bool m_wants_buttjump;
  269. bool m_buttjump_stomp;
  270. public:
  271. bool m_does_buttjump;
  272. Timer m_invincible_timer;
  273. private:
  274. Timer m_skidding_timer;
  275. Timer m_safe_timer;
  276. bool m_is_intentionally_safe;
  277. Timer m_kick_timer;
  278. Timer m_buttjump_timer;
  279. public:
  280. Timer m_dying_timer;
  281. private:
  282. Timer m_second_growup_sound_timer;
  283. bool m_growing;
  284. Timer m_backflip_timer;
  285. Physic m_physic;
  286. bool m_visible;
  287. Portable* m_grabbed_object;
  288. std::unique_ptr<ObjectRemoveListener> m_grabbed_object_remove_listener;
  289. bool m_released_object;
  290. SpritePtr m_sprite; /**< The main sprite representing Tux */
  291. float m_swimming_angle;
  292. float m_swimming_accel_modifier;
  293. bool m_water_jump;
  294. SurfacePtr m_airarrow; /**< arrow indicating Tux' position when he's above the camera */
  295. Vector m_floor_normal;
  296. bool m_ghost_mode; /**< indicates if Tux should float around and through solid objects */
  297. Timer m_unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
  298. Timer m_idle_timer;
  299. unsigned int m_idle_stage;
  300. Climbable* m_climbing; /**< Climbable object we are currently climbing, null if none */
  301. int m_ending_direction;
  302. std::vector<Key*> m_collected_keys;
  303. float m_last_sliding_angle;
  304. float m_current_sliding_angle;
  305. float m_target_sliding_angle;
  306. Timer m_sliding_rotation_timer;
  307. bool m_is_slidejump_falling;
  308. bool m_was_crawling_before_slide;
  309. private:
  310. Player(const Player&) = delete;
  311. Player& operator=(const Player&) = delete;
  312. };
  313. #endif
  314. /* EOF */