extends CharacterBody3D
class_name Player
+##########
+# settings
+##########
-# player settings
@export_group("Movement")
-@export var walk_speed := 10.0
+@export var walk_speed := 8.0
+@export var dash_length := 10.0
@export var air_speed := 3.0
-@export var acceleration := 50.0
+@export var acceleration := 30.0
@export var rotation_speed := 10.0
@export var idle_timeout := 5.0
-@export var hard_landing_limit := 10.0
-
-@export_group("Physics")
-@export var push_force := 5.0
@export_group("Camera")
@export_range(1.0, 10.0) var camera_distance := 2.0
@export_range(0.0, 10.0) var joystick_sensitivity_x := 4.0
@export_range(0.0, 10.0) var joystick_sensitivity_y := 2.0
+######
+# init
+######
@onready var _camera_pivot: Node3D = %camera_pivot
@onready var _camera: Camera3D = %camera
var _floor_normal := Vector3.ONE
var _ground_slope_input := 0.0
var _camera_input_direction := Vector2.ZERO
-
-enum {CAMERA_MOUSE_INPUT, CAMERA_JOYSTICK_INPUT}
-var _camera_input_method := CAMERA_MOUSE_INPUT
-
var _idle_time: float = 0.0
var _player_speed: float = walk_speed
+enum CameraInput {MOUSE, JOYSTICK}
+var _camera_input_method := CameraInput.MOUSE
+
func _ready() -> void:
_camera_spring.spring_length = camera_distance
+
+
-
-func _physics_process(delta: float) -> void:
- _process_camera(delta)
- _process_player(delta)
-
+#######
+# input
+#######
func _unhandled_input(event: InputEvent) -> void:
# If user clicks on the window, capture the mouse and direct the camera with it
#_camera_input_direction *= mouse_sensitivity
if event is InputEventMouseMotion:
- _camera_input_method = CAMERA_MOUSE_INPUT
+ _camera_input_method = CameraInput.MOUSE
_camera_input_direction = event.screen_relative * mouse_sensitivity
elif event is InputEventJoypadMotion:
- _camera_input_method = CAMERA_JOYSTICK_INPUT
+ # TODO: add these settings!
+ _camera_input_method = CameraInput.JOYSTICK
_camera_input_direction = Input.get_vector("camera-left", "camera-right", "camera-up", "camera-down")
_camera_input_direction *= Vector2(joystick_sensitivity_x, -joystick_sensitivity_y)
func _input(event: InputEvent):
if event.is_action_pressed("player-dash"):
_skin.transition_dash()
+ _player_dash()
elif event.is_action_pressed("player-slash"):
_skin.sword_visible()
_skin.transition_slash()
elif event.is_action_pressed("player-shoot"):
_skin.gun_visible()
_skin.transition_gunfire()
+
+
+##########
+# movement
+##########
+
+func _physics_process(delta: float) -> void:
+ _process_camera(delta)
+ _process_player(delta)
# Get the XZ input direction based on player's input relative to the camera
_camera_pivot.rotation.y -= _camera_input_direction.x * mouse_sensitivity_x * delta
# reset mouse movement vector if mouse input
- if _camera_input_method == CAMERA_MOUSE_INPUT:
+ if _camera_input_method == CameraInput.MOUSE:
_camera_input_direction = Vector2.ZERO
# change spring length depending on player speed
velocity += get_gravity() * air_speed * delta
move_and_slide()
+
+#########
+# actions
+#########
+
+func _player_dash():
+ if _last_movement_direction != Vector3.ZERO:
+ var dash_local_pos = _last_movement_direction * dash_length
+ # TODO: check if valid position, crop the vector to last valid position
+ global_position += dash_local_pos