Path: blob/master/scene/2d/physics/character_body_2d.h
9906 views
/**************************************************************************/1/* character_body_2d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "scene/2d/physics/kinematic_collision_2d.h"33#include "scene/2d/physics/physics_body_2d.h"3435class CharacterBody2D : public PhysicsBody2D {36GDCLASS(CharacterBody2D, PhysicsBody2D);3738public:39enum MotionMode {40MOTION_MODE_GROUNDED,41MOTION_MODE_FLOATING,42};43enum PlatformOnLeave {44PLATFORM_ON_LEAVE_ADD_VELOCITY,45PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,46PLATFORM_ON_LEAVE_DO_NOTHING,47};48bool move_and_slide();49void apply_floor_snap();5051const Vector2 &get_velocity() const;52void set_velocity(const Vector2 &p_velocity);5354bool is_on_floor() const;55bool is_on_floor_only() const;56bool is_on_wall() const;57bool is_on_wall_only() const;58bool is_on_ceiling() const;59bool is_on_ceiling_only() const;60const Vector2 &get_last_motion() const;61Vector2 get_position_delta() const;62const Vector2 &get_floor_normal() const;63const Vector2 &get_wall_normal() const;64const Vector2 &get_real_velocity() const;6566real_t get_floor_angle(const Vector2 &p_up_direction = Vector2(0.0, -1.0)) const;67const Vector2 &get_platform_velocity() const;6869int get_slide_collision_count() const;70PhysicsServer2D::MotionResult get_slide_collision(int p_bounce) const;7172void set_safe_margin(real_t p_margin);73real_t get_safe_margin() const;7475bool is_floor_stop_on_slope_enabled() const;76void set_floor_stop_on_slope_enabled(bool p_enabled);7778bool is_floor_constant_speed_enabled() const;79void set_floor_constant_speed_enabled(bool p_enabled);8081bool is_floor_block_on_wall_enabled() const;82void set_floor_block_on_wall_enabled(bool p_enabled);8384bool is_slide_on_ceiling_enabled() const;85void set_slide_on_ceiling_enabled(bool p_enabled);8687int get_max_slides() const;88void set_max_slides(int p_max_slides);8990real_t get_floor_max_angle() const;91void set_floor_max_angle(real_t p_radians);9293real_t get_floor_snap_length();94void set_floor_snap_length(real_t p_floor_snap_length);9596real_t get_wall_min_slide_angle() const;97void set_wall_min_slide_angle(real_t p_radians);9899uint32_t get_platform_floor_layers() const;100void set_platform_floor_layers(const uint32_t p_exclude_layer);101102uint32_t get_platform_wall_layers() const;103void set_platform_wall_layers(const uint32_t p_exclude_layer);104105void set_motion_mode(MotionMode p_mode);106MotionMode get_motion_mode() const;107108void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity);109PlatformOnLeave get_platform_on_leave() const;110111CharacterBody2D();112113private:114real_t margin = 0.08;115MotionMode motion_mode = MOTION_MODE_GROUNDED;116PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY;117118bool floor_constant_speed = false;119bool floor_stop_on_slope = true;120bool floor_block_on_wall = true;121bool slide_on_ceiling = true;122int max_slides = 4;123int platform_layer = 0;124real_t floor_max_angle = Math::deg_to_rad((real_t)45.0);125real_t floor_snap_length = 1;126real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0);127Vector2 up_direction = Vector2(0.0, -1.0);128uint32_t platform_floor_layers = UINT32_MAX;129uint32_t platform_wall_layers = 0;130Vector2 velocity;131132Vector2 floor_normal;133Vector2 platform_velocity;134Vector2 wall_normal;135Vector2 last_motion;136Vector2 previous_position;137Vector2 real_velocity;138139RID platform_rid;140ObjectID platform_object_id;141bool on_floor = false;142bool on_ceiling = false;143bool on_wall = false;144145Vector<PhysicsServer2D::MotionResult> motion_results;146Vector<Ref<KinematicCollision2D>> slide_colliders;147148void _move_and_slide_floating(double p_delta);149void _move_and_slide_grounded(double p_delta, bool p_was_on_floor);150151Ref<KinematicCollision2D> _get_slide_collision(int p_bounce);152Ref<KinematicCollision2D> _get_last_slide_collision();153const Vector2 &get_up_direction() const;154bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up);155void set_up_direction(const Vector2 &p_up_direction);156void _set_collision_direction(const PhysicsServer2D::MotionResult &p_result);157void _set_platform_data(const PhysicsServer2D::MotionResult &p_result);158void _apply_floor_snap(bool p_wall_as_floor = false);159void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up, bool p_wall_as_floor = false);160161protected:162void _notification(int p_what);163static void _bind_methods();164void _validate_property(PropertyInfo &p_property) const;165};166167VARIANT_ENUM_CAST(CharacterBody2D::MotionMode);168VARIANT_ENUM_CAST(CharacterBody2D::PlatformOnLeave);169170171