diff --git a/modules/going/player_body.cpp b/modules/going/player_body.cpp index 6297820e..f6cb23ad 100644 --- a/modules/going/player_body.cpp +++ b/modules/going/player_body.cpp @@ -25,6 +25,7 @@ void PlayerBody::_bind_methods() { BIND_PROPERTY(Variant::FLOAT, target_speed); BIND_PROPERTY(Variant::FLOAT, split_step_time); BIND_PROPERTY(Variant::FLOAT, split_step_stop_time); + BIND_PROPERTY(Variant::VECTOR2, jump_impulse); BIND_PROPERTY(Variant::FLOAT, model_lean); BIND_PROPERTY(Variant::FLOAT, model_lean_speed); } @@ -165,6 +166,14 @@ double PlayerBody::get_split_step_stop_time() const { return this->split_step_stop_time; } +void PlayerBody::set_jump_impulse(Vector2 value) { + this->jump_impulse = value; +} + +Vector2 PlayerBody::get_jump_impulse() const { + return this->jump_impulse; +} + void PlayerBody::set_model_lean(float value) { this->model_lean = value; } diff --git a/modules/going/player_body.h b/modules/going/player_body.h index 0567bbe3..b1867c9c 100644 --- a/modules/going/player_body.h +++ b/modules/going/player_body.h @@ -38,6 +38,8 @@ public: double get_split_step_time() const; void set_split_step_stop_time(double value); double get_split_step_stop_time() const; + void set_jump_impulse(Vector2 value); + Vector2 get_jump_impulse() const; void set_model_lean(float value); float get_model_lean() const; void set_model_lean_speed(float value); @@ -57,6 +59,7 @@ private: float target_speed{30.f}; double split_step_time{0.5}; double split_step_stop_time{0.5}; + Vector2 jump_impulse{5.f, 5.f}; float max_speed_fov{100.f}; float min_fov{80.f}; double max_delta_fov{100.f}; diff --git a/modules/going/player_states.cpp b/modules/going/player_states.cpp index c693eb77..49d3422b 100644 --- a/modules/going/player_states.cpp +++ b/modules/going/player_states.cpp @@ -71,7 +71,7 @@ void RunningState::physics_process(double delta) { : float(this->get_body()->get_acceleration() * delta) // in-motion velocity ) }; - this->get_body()->set_velocity(current.move_toward(desired, speed_delta)); + this->get_body()->set_velocity(current.move_toward(desired, speed_delta) + Vector3{0.f, -0.01f, 0.f}); } void RunningState::state_exited() { @@ -82,10 +82,12 @@ void RunningState::state_exited() { PlayerState::StateID SplitStepState::get_next_state() const { if(!this->get_body()->is_on_floor()) { return FallingState::get_class_static(); + } else if(this->timer <= 0.0 && Input::get_singleton()->is_action_pressed("jump")) { + return JumpingState::get_class_static(); + } else if(this->timer <= 0.0) { + return RunningState::get_class_static(); } else { - return this->timer <= 0.0 - ? RunningState::get_class_static() - : self_type::get_class_static(); + return self_type::get_class_static(); } } @@ -131,6 +133,31 @@ void FallingState::process(double delta) { this->get_body()->set_velocity((flattened - (flattened * 0.025f)) + Vector3{0.f, current.y - float(9.8 * delta), 0.f}); } +PlayerState::StateID JumpingState::get_next_state() const { + if(this->get_body()->get_velocity().y <= 0.f) { + return FallingState::get_class_static(); + } else { + return self_type::get_class_static(); + } +} + +void JumpingState::state_entered() { + Vector3 const current{this->get_body()->get_velocity()}; + Vector2 const impulse{this->get_body()->get_jump_impulse()}; + float const force{(Vector2{current.x, current.z}.length() / this->get_body()->get_target_speed())}; + this->get_body()->set_velocity( ( + Vector3{current.x, impulse.y, current.z} + + current.normalized() * impulse.x + ) * force); + this->get_body()->get_anim()->play("jump"); +} + +void JumpingState::process(double delta) { + Vector3 const current{this->get_body()->get_velocity()}; + Vector3 const flattened{current.x, 0.f, current.z}; + this->get_body()->set_velocity((flattened - (flattened * 0.025f)) + Vector3{0.f, current.y - float(9.8 * delta), 0.f}); +} + void PlayerStateMachine::_bind_methods() { } @@ -162,6 +189,7 @@ void PlayerStateMachine::ready() { this->add_state(); this->add_state(); this->add_state(); + this->add_state(); } void PlayerStateMachine::try_transition() { diff --git a/modules/going/player_states.h b/modules/going/player_states.h index a7338afa..786c6a64 100644 --- a/modules/going/player_states.h +++ b/modules/going/player_states.h @@ -65,6 +65,14 @@ public: virtual void process(double delta) override; }; +class JumpingState : public PlayerState { + GDCLASS(JumpingState, PlayerState); +public: + virtual StateID get_next_state() const override; + virtual void state_entered() override; + virtual void process(double delta) override; +}; + class PlayerStateMachine : public Node { GDCLASS(PlayerStateMachine, Node); static void _bind_methods(); diff --git a/modules/going/register_types.cpp b/modules/going/register_types.cpp index c8de7259..930c5d1d 100644 --- a/modules/going/register_types.cpp +++ b/modules/going/register_types.cpp @@ -13,6 +13,8 @@ void initialize_going_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); } diff --git a/project/models/player/character.blend b/project/models/player/character.blend index e23c4bac..56943d7f 100644 Binary files a/project/models/player/character.blend and b/project/models/player/character.blend differ diff --git a/project/models/player/character.blend1 b/project/models/player/character.blend1 index 93566274..6c595c55 100644 Binary files a/project/models/player/character.blend1 and b/project/models/player/character.blend1 differ diff --git a/project/objects/player.tscn b/project/objects/player.tscn index f1609b34..de52095d 100644 --- a/project/objects/player.tscn +++ b/project/objects/player.tscn @@ -22,8 +22,9 @@ func _process(_delta): " [node name="Player" type="PlayerBody"] -wall_min_slide_angle = 0.0 -floor_max_angle = 0.523599 +wall_min_slide_angle = 0.785398 +floor_max_angle = 0.968658 +floor_snap_length = 1.5 [node name="PlayerStateMachine" type="PlayerStateMachine" parent="."] diff --git a/project/project.godot b/project/project.godot index 1a2d634b..4752121f 100644 --- a/project/project.godot +++ b/project/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="going" -run/main_scene="uid://bp0vgy3lnsucd" +run/main_scene="uid://sofv1apr4467" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" @@ -42,6 +42,11 @@ split_step={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null) ] } +jump={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":102,"location":0,"echo":false,"script":null) +] +} [layer_names]