meat_madness_redux/godot/player/OnFootPhysics.gd

131 lines
4.2 KiB
GDScript3
Raw Normal View History

2022-11-27 13:31:09 -07:00
extends KinematicBody
onready var util = get_node("/root/Util")
2022-12-11 12:23:58 -07:00
export var gravity: float = 6.0
# When falling, gravity is artificially increased
2022-12-11 12:59:18 -07:00
export var gravity_downwards_factor: float = 3
2022-12-28 10:37:27 -07:00
export var ground_gravity: float = 3.1
2022-12-11 12:23:58 -07:00
# Rate of gaining speed
export var velocity_acceleration_xz: float = 3
export var velocity_acceleration_y: float = 5
# Rate of losing speed when stopping
export var velocity_friction_xz: float = 15
export var velocity_friction_y: float = 1
export var jump_power: float = 50
2022-11-27 19:55:11 -07:00
2022-12-11 12:23:58 -07:00
# Controls the base speeds of walking and sprinting
2022-12-11 12:59:18 -07:00
export var sprint_factor: float = 3.5
export var walk_factor: float = 2.0
2022-11-27 13:31:09 -07:00
2023-01-02 23:18:07 -07:00
export var can_glide: bool = false
2022-12-11 12:23:58 -07:00
"public" var velocity: Vector3 = Vector3.ZERO
# Value changed by code only to add slow down or speed up effects
var velocity_factor: Vector3 = Vector3.ONE
2022-11-27 13:31:09 -07:00
var target_velocity: Vector3 = Vector3.ZERO
var is_jumping: bool = false
var jump_permission: float = 1.0
2022-12-11 17:02:57 -07:00
var is_falling: bool = false
2022-12-11 17:10:07 -07:00
var is_falling_medium = false
2022-12-11 17:02:57 -07:00
var is_falling_terminally: bool = false
var just_landed: bool = false
2022-12-11 17:10:07 -07:00
var just_landed_medium: bool = false
2022-12-11 17:02:57 -07:00
var just_landed_terminally: bool = false
2022-11-27 13:31:09 -07:00
2022-11-27 21:06:36 -07:00
func get_terminal_falling_velocity():
2022-12-11 17:02:57 -07:00
return -9
2022-11-27 21:06:36 -07:00
func is_falling_velocity_terminal():
return velocity.y <= get_terminal_falling_velocity()
2022-11-27 13:31:09 -07:00
func process_velocity(delta: float):
# Uses the input from MovementInput to compute and update this node's velocity
# velocity_acceleration controls gradual speedup whereas velocity_friction
# controls gradual slowdown
# Gravity
# You can buffer jumps by pressing releaseing the space bar and then pressing it again
if not is_on_floor():
2022-12-11 12:23:58 -07:00
var grav = -gravity
2023-01-02 23:18:07 -07:00
if can_glide and Input.is_action_pressed("player_jump"):
grav /= 4
2022-12-11 12:23:58 -07:00
target_velocity.y = grav
# Gravity is stronger when you're falling. Feels nicer
target_velocity.y *= util.clamped_lerp(gravity_downwards_factor, 1.0, velocity.y/2, 0.0)
2022-11-27 13:31:09 -07:00
else:
velocity.y = max(0.0, velocity.y)
target_velocity.y = max(0.0, max(target_velocity.y, jump_power * $"../MovementInput".jump_intent * jump_permission))
2022-12-28 10:37:27 -07:00
if target_velocity.y == 0.0 and velocity.y == 0.0:
velocity.y = -ground_gravity
2022-11-27 13:31:09 -07:00
if not is_jumping and $"../MovementInput".jump_intent != 0.0 and is_on_floor():
jump_permission = 0.0
is_jumping = true
elif is_jumping and $"../MovementInput".jump_intent == 0.0:
jump_permission = 1.0
is_jumping = false
2022-12-11 17:02:57 -07:00
2022-11-27 13:31:09 -07:00
2022-12-11 12:59:18 -07:00
var target_velocity_xz = $"../MovementInput".input_xz
if $"../MovementInput".sprinting:
target_velocity_xz *= sprint_factor
else:
target_velocity_xz *= walk_factor
2022-12-11 18:07:05 -07:00
target_velocity = util.vec2_xz_to_vec3(target_velocity_xz, target_velocity.y) * velocity_factor
2022-11-27 13:31:09 -07:00
var velocity_xz = util.vec3_xz(velocity)
2022-12-11 12:23:58 -07:00
var xz_weight = velocity_acceleration_xz * delta
2022-11-27 13:31:09 -07:00
if target_velocity_xz.length() < velocity_xz.length():
2022-12-11 12:23:58 -07:00
xz_weight = velocity_friction_xz * delta
2022-11-27 13:31:09 -07:00
2022-12-11 12:23:58 -07:00
var target_xz_length = util.clamped_lerp(velocity_xz.length(), target_velocity_xz.length(), xz_weight, 0.25)
2022-11-27 13:31:09 -07:00
if target_velocity_xz != Vector2.ZERO:
velocity_xz = target_velocity_xz.normalized() * target_xz_length
elif velocity_xz != Vector2.ZERO:
velocity_xz = velocity_xz.normalized() * target_xz_length
2022-12-11 12:23:58 -07:00
var y_weight = velocity_acceleration_y * delta
2022-11-27 13:31:09 -07:00
if target_velocity.y < velocity.y:
2022-12-11 12:23:58 -07:00
y_weight = velocity_friction_y * delta
2022-11-27 13:31:09 -07:00
2022-12-11 12:23:58 -07:00
var velocity_y = util.clamped_lerp(velocity.y, target_velocity.y, y_weight, 0.25)
2022-11-27 13:31:09 -07:00
2022-12-11 18:07:05 -07:00
velocity = util.vec2_xz_to_vec3(velocity_xz, velocity_y)
2022-11-27 19:55:11 -07:00
2022-11-27 13:31:09 -07:00
2022-12-11 17:02:57 -07:00
func process_falling():
2022-12-27 20:06:15 -07:00
if not $Floor.is_on_floor and not is_on_floor():
2022-12-11 17:02:57 -07:00
is_falling = true
just_landed = false
2022-12-11 17:18:29 -07:00
just_landed_medium = false
2022-12-11 17:02:57 -07:00
just_landed_terminally = false
2022-12-11 17:18:29 -07:00
if velocity.y <= -5.0:
2022-12-11 17:10:07 -07:00
is_falling_medium = true
2022-12-11 17:02:57 -07:00
if is_falling_velocity_terminal():
is_falling_terminally = true
2022-12-11 17:18:29 -07:00
return
2022-12-11 17:02:57 -07:00
just_landed = is_falling
2022-12-11 17:10:07 -07:00
just_landed_medium = is_falling_medium
2022-12-11 17:02:57 -07:00
just_landed_terminally = is_falling_terminally
is_falling = false
2022-12-11 17:10:07 -07:00
is_falling_medium = false
2022-12-11 17:02:57 -07:00
is_falling_terminally = false
2022-11-27 13:31:09 -07:00
func increase_velocity_factor(by: Vector3):
velocity_factor *= by
func decrease_velocity_factor(by: Vector3):
velocity_factor /= by
func _physics_process(delta):
process_velocity(delta)
2022-12-28 16:53:17 -07:00
# velocity = move_and_slide(velocity, Vector3.UP, true, 4, 1.3217304764)
velocity = move_and_slide(velocity, Vector3.UP, true, 4)
2022-12-11 17:02:57 -07:00
process_falling()