feat: added jump

This commit is contained in:
Sara 2025-05-27 11:22:38 +02:00
parent 0c3cd09d88
commit 9d8ffd5df8
9 changed files with 63 additions and 7 deletions

View file

@ -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;
}

View file

@ -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};

View file

@ -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<RunningState>();
this->add_state<SplitStepState>();
this->add_state<FallingState>();
this->add_state<JumpingState>();
}
void PlayerStateMachine::try_transition() {

View file

@ -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();

View file

@ -13,6 +13,8 @@ void initialize_going_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<StandingState>();
ClassDB::register_class<RunningState>();
ClassDB::register_class<SplitStepState>();
ClassDB::register_class<FallingState>();
ClassDB::register_class<JumpingState>();
ClassDB::register_class<PlayerStateMachine>();
}

View file

@ -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="."]

View file

@ -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]