1 extends CharacterBody3D
8 @export_group("Movement")
9 @export var walk_speed := 8.0
10 @export var dash_length := 10.0
12 @export var air_speed := 3.0
13 @export var acceleration := 50.0
14 @export var rotation_speed := 10.0
15 @export var idle_timeout := 5.0
16 @export var hard_landing_limit := 10.0
18 @export_group("Physics")
19 @export var push_force := 5.0
21 @export_group("Camera")
22 @export_range(1.0, 10.0) var camera_distance := 2.0
23 @export_range(0.0, 1.0) var mouse_sensitivity := 0.15
24 @export_range(0.0, 1.0) var mouse_sensitivity_x := 1.0
25 @export_range(0.0, 1.0) var mouse_sensitivity_y := 0.5
26 @export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
27 @export_range(0.0, 10.0) var joystick_sensitivity_y := 2.0
33 @onready var _camera_pivot: Node3D = %camera_pivot
34 @onready var _camera: Camera3D = %camera
35 @onready var _camera_spring: SpringArm3D = %spring
36 @onready var _skin: AnimatedSkin = %skin
38 var _last_movement_direction := rotation
39 var _floor_normal := Vector3.ONE
40 var _ground_slope_input := 0.0
41 var _camera_input_direction := Vector2.ZERO
43 enum {CAMERA_MOUSE_INPUT, CAMERA_JOYSTICK_INPUT}
44 var _camera_input_method := CAMERA_MOUSE_INPUT
46 var _idle_time: float = 0.0
47 var _player_speed: float = walk_speed
50 func _ready() -> void:
51 _camera_spring.spring_length = camera_distance
59 func _unhandled_input(event: InputEvent) -> void:
60 # If user clicks on the window, capture the mouse and direct the camera with it
61 if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
64 #_camera_input_direction *= mouse_sensitivity
65 if event is InputEventMouseMotion:
66 _camera_input_method = CAMERA_MOUSE_INPUT
67 _camera_input_direction = event.screen_relative * mouse_sensitivity
68 elif event is InputEventJoypadMotion:
69 # TODO: add these settings!
70 _camera_input_method = CAMERA_JOYSTICK_INPUT
71 _camera_input_direction = Input.get_vector("camera-left", "camera-right", "camera-up", "camera-down")
72 _camera_input_direction *= Vector2(joystick_sensitivity_x, -joystick_sensitivity_y)
75 func _input(event: InputEvent):
76 if event.is_action_pressed("player-dash"):
77 _skin.transition_dash()
79 elif event.is_action_pressed("player-slash"):
81 _skin.transition_slash()
82 elif event.is_action_pressed("player-shoot"):
84 _skin.transition_gunfire()
91 func _physics_process(delta: float) -> void:
92 _process_camera(delta)
93 _process_player(delta)
96 # Get the XZ input direction based on player's input relative to the camera
97 func _get_player_move_direction() -> Vector3:
98 var input_dir := Input.get_vector("player-left", "player-right", "player-forward", "player-backward")
99 var forward := _camera.global_basis.z
100 var right := _camera.global_basis.x
101 var move_direction := (forward * input_dir.y + right * input_dir.x).normalized()
103 return move_direction
106 func _process_camera(delta: float) -> void:
107 # vertical camera rotation
108 _camera_pivot.rotation.x += _camera_input_direction.y * mouse_sensitivity_y * delta
109 _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -PI / 6, PI / 3)
111 # horizontal camera rotation
112 _camera_pivot.rotation.y -= _camera_input_direction.x * mouse_sensitivity_x * delta
114 # reset mouse movement vector if mouse input
115 if _camera_input_method == CAMERA_MOUSE_INPUT:
116 _camera_input_direction = Vector2.ZERO
118 # change spring length depending on player speed
119 _camera_spring.spring_length = lerp(
120 _camera_spring.spring_length, camera_distance + velocity.length() / 4, delta
124 func _process_player_on_floor(delta: float):
125 var move_direction := _get_player_move_direction()
127 # if we're not stuck, then it's okay to set the velocity
128 _floor_normal = get_floor_normal()
129 _ground_slope_input = (PI / 2) - velocity.angle_to(_floor_normal)
130 velocity = velocity.move_toward(
131 move_direction * (_player_speed + _ground_slope_input * _player_speed),
134 var movement_speed := Vector3(velocity.x, 0, velocity.z).length()
136 # also, if we're moving, we're not idle
137 if move_direction.length() < 0.2:
138 if velocity == Vector3.ZERO:
141 _last_movement_direction = move_direction
144 # if camera is unlocked, rotate whole skin to face movement direction
145 var skin_target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
146 _skin.global_rotation.y = lerp_angle(
147 _skin.global_rotation.y,
149 rotation_speed * delta
152 # lean into player momentum just a little bit
153 _skin.rotation.z = lerp_angle(
155 clamp(_last_movement_direction.signed_angle_to(velocity, Vector3.UP) * movement_speed * 0.08, -PI/4, PI/ 4),
156 rotation_speed * delta * 0.25
159 # let skin know how fast player is moving along the ground
160 _skin.set_walking_speed(movement_speed)
163 func _process_player(delta: float) -> void:
165 _process_player_on_floor(delta)
167 if _idle_time > idle_timeout:
168 _skin.transition_move()
170 _skin.transition_move()
172 _skin.transition_falling()
173 velocity += get_gravity() * air_speed * delta
182 var move_direction := _get_player_move_direction()
183 if move_direction != Vector3.ZERO:
184 var dash_local_pos = move_direction * dash_length
185 # TODO: check if valid position, crop the vector to last valid position
186 global_position += dash_local_pos