51 lines
1.3 KiB
GDScript
51 lines
1.3 KiB
GDScript
class_name NewPlayer
|
|
extends CharacterBody2D
|
|
|
|
|
|
@export_group("Ground Movement")
|
|
@export var run_acceleration: float
|
|
@export var max_run_speed: float
|
|
@export var turn_acceleration: float
|
|
@export var stopping_force: float
|
|
|
|
@export_group("Internal References")
|
|
@export var state_chart: StateChart
|
|
@export var graphics: Node2D
|
|
@export var run_animation: SpritesheetAnimation
|
|
|
|
|
|
var input_dir: Vector2 = Vector2.ZERO:
|
|
set(value):
|
|
input_dir = value.sign()
|
|
state_chart.set_expression_property(&"input_dir", input_dir)
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
move_and_slide()
|
|
state_chart.set_expression_property(&"velocity", velocity)
|
|
if velocity.x != 0.0:
|
|
graphics.scale.x = signf(velocity.x)
|
|
|
|
input_dir = Input.get_vector(&"move_left", &"move_right", &"move_up", &"move_down")
|
|
|
|
|
|
#region Idle
|
|
func _slow_to_stop(delta: float) -> void:
|
|
velocity.x = move_toward(velocity.x, 0.0, stopping_force * delta)
|
|
#endregion
|
|
|
|
|
|
#region Running
|
|
func _apply_run_acceleration(delta: float) -> void:
|
|
if absf(velocity.x) < max_run_speed:
|
|
velocity.x += input_dir.x * run_acceleration * delta
|
|
|
|
func _scale_run_animation(delta:float) -> void:
|
|
run_animation.speed_scale = inverse_lerp(0.0, max_run_speed, absf(velocity.x))
|
|
#endregion
|
|
|
|
|
|
#region Turning
|
|
func _apply_turn_acceleration(delta: float) -> void:
|
|
velocity.x += input_dir.x * turn_acceleration * delta
|
|
#endregion
|