character_body_2d.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* character_body_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CHARACTER_BODY_2D_H
  31. #define CHARACTER_BODY_2D_H
  32. #include "scene/2d/physics/kinematic_collision_2d.h"
  33. #include "scene/2d/physics/physics_body_2d.h"
  34. class CharacterBody2D : public PhysicsBody2D {
  35. GDCLASS(CharacterBody2D, PhysicsBody2D);
  36. public:
  37. enum MotionMode {
  38. MOTION_MODE_GROUNDED,
  39. MOTION_MODE_FLOATING,
  40. };
  41. enum PlatformOnLeave {
  42. PLATFORM_ON_LEAVE_ADD_VELOCITY,
  43. PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,
  44. PLATFORM_ON_LEAVE_DO_NOTHING,
  45. };
  46. bool move_and_slide();
  47. void apply_floor_snap();
  48. const Vector2 &get_velocity() const;
  49. void set_velocity(const Vector2 &p_velocity);
  50. bool is_on_floor() const;
  51. bool is_on_floor_only() const;
  52. bool is_on_wall() const;
  53. bool is_on_wall_only() const;
  54. bool is_on_ceiling() const;
  55. bool is_on_ceiling_only() const;
  56. const Vector2 &get_last_motion() const;
  57. Vector2 get_position_delta() const;
  58. const Vector2 &get_floor_normal() const;
  59. const Vector2 &get_wall_normal() const;
  60. const Vector2 &get_real_velocity() const;
  61. real_t get_floor_angle(const Vector2 &p_up_direction = Vector2(0.0, -1.0)) const;
  62. const Vector2 &get_platform_velocity() const;
  63. int get_slide_collision_count() const;
  64. PhysicsServer2D::MotionResult get_slide_collision(int p_bounce) const;
  65. void set_safe_margin(real_t p_margin);
  66. real_t get_safe_margin() const;
  67. bool is_floor_stop_on_slope_enabled() const;
  68. void set_floor_stop_on_slope_enabled(bool p_enabled);
  69. bool is_floor_constant_speed_enabled() const;
  70. void set_floor_constant_speed_enabled(bool p_enabled);
  71. bool is_floor_block_on_wall_enabled() const;
  72. void set_floor_block_on_wall_enabled(bool p_enabled);
  73. bool is_slide_on_ceiling_enabled() const;
  74. void set_slide_on_ceiling_enabled(bool p_enabled);
  75. int get_max_slides() const;
  76. void set_max_slides(int p_max_slides);
  77. real_t get_floor_max_angle() const;
  78. void set_floor_max_angle(real_t p_radians);
  79. real_t get_floor_snap_length();
  80. void set_floor_snap_length(real_t p_floor_snap_length);
  81. real_t get_wall_min_slide_angle() const;
  82. void set_wall_min_slide_angle(real_t p_radians);
  83. uint32_t get_platform_floor_layers() const;
  84. void set_platform_floor_layers(const uint32_t p_exclude_layer);
  85. uint32_t get_platform_wall_layers() const;
  86. void set_platform_wall_layers(const uint32_t p_exclude_layer);
  87. void set_motion_mode(MotionMode p_mode);
  88. MotionMode get_motion_mode() const;
  89. void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity);
  90. PlatformOnLeave get_platform_on_leave() const;
  91. CharacterBody2D();
  92. private:
  93. real_t margin = 0.08;
  94. MotionMode motion_mode = MOTION_MODE_GROUNDED;
  95. PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY;
  96. bool floor_constant_speed = false;
  97. bool floor_stop_on_slope = true;
  98. bool floor_block_on_wall = true;
  99. bool slide_on_ceiling = true;
  100. int max_slides = 4;
  101. int platform_layer = 0;
  102. real_t floor_max_angle = Math::deg_to_rad((real_t)45.0);
  103. real_t floor_snap_length = 1;
  104. real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0);
  105. Vector2 up_direction = Vector2(0.0, -1.0);
  106. uint32_t platform_floor_layers = UINT32_MAX;
  107. uint32_t platform_wall_layers = 0;
  108. Vector2 velocity;
  109. Vector2 floor_normal;
  110. Vector2 platform_velocity;
  111. Vector2 wall_normal;
  112. Vector2 last_motion;
  113. Vector2 previous_position;
  114. Vector2 real_velocity;
  115. RID platform_rid;
  116. ObjectID platform_object_id;
  117. bool on_floor = false;
  118. bool on_ceiling = false;
  119. bool on_wall = false;
  120. Vector<PhysicsServer2D::MotionResult> motion_results;
  121. Vector<Ref<KinematicCollision2D>> slide_colliders;
  122. void _move_and_slide_floating(double p_delta);
  123. void _move_and_slide_grounded(double p_delta, bool p_was_on_floor);
  124. Ref<KinematicCollision2D> _get_slide_collision(int p_bounce);
  125. Ref<KinematicCollision2D> _get_last_slide_collision();
  126. const Vector2 &get_up_direction() const;
  127. bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up);
  128. void set_up_direction(const Vector2 &p_up_direction);
  129. void _set_collision_direction(const PhysicsServer2D::MotionResult &p_result);
  130. void _set_platform_data(const PhysicsServer2D::MotionResult &p_result);
  131. void _apply_floor_snap(bool p_wall_as_floor = false);
  132. void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up, bool p_wall_as_floor = false);
  133. protected:
  134. void _notification(int p_what);
  135. static void _bind_methods();
  136. void _validate_property(PropertyInfo &p_property) const;
  137. };
  138. VARIANT_ENUM_CAST(CharacterBody2D::MotionMode);
  139. VARIANT_ENUM_CAST(CharacterBody2D::PlatformOnLeave);
  140. #endif // CHARACTER_BODY_2D_H