1 extends CharacterBody3D
3 ## The base class for any input-driven character
5 ## The Player class drives the associated Model with the input from the InputHandler,
6 ## which drives the Visual. Each submodule can be swapped out for easy customization.
7 ## The CameraHandler is to allow input to be made relative to the current camera's position
11 ## Human-readable name for UI display
12 @export var display_name := ""
14 ## Ceiling for hit points
15 @export var max_hit_points: float = 100.0
17 ## Ceiling for stamina points
18 @export var max_stamina_points: float = 100.0
20 ## Ceiling for energy points
21 @export var max_energy_points: float = 100.0
23 ## Rate of stamina regeneration
24 @export var stamina_regain_per_sec: float = 80.0
26 ## Remaps absolute input relative to current camera.
27 ## Bad idea for robots, good idea for player control! [br]
29 ## TODO: Probably should be all managed by the InputHandler!
30 @export var input_is_relative_to_camera: bool = false
33 ## Current state name of the PlayerModel state machine. For debugging.
36 ## How long the player has been idle. Populated by the Idle state.
39 ## Points to the last meaningful movement of the Player.
40 var last_movement_vector: Vector3
42 ## For debugging. Records the floor normal.
43 var floor_normal := Vector3.ZERO
45 ## For debugging. How much the immediate slope influences velocity.
46 var ground_slope_input := 0.0
49 @export_category("Instance")
52 @export var hit_points: float = 100.0
54 ## Current stamina points
55 @export var stamina_points: float = 100.0
57 ## Current energy points
58 @export var energy_points: float = 0.0
61 @onready var input: InputHandler = $Input
62 @onready var model: CharacterModel = $Model
63 @onready var visual: CharacterVisual = $Model/Visual
64 @onready var cameraHandler: CameraHandler = $CameraHandler
65 @onready var collision: CollisionShape3D = $Collision
68 func _ready() -> void:
69 visual.assign_skeleton(model.skeleton)
70 last_movement_vector = Vector3.BACK.rotated(Vector3.UP, rotation.y)
73 func _physics_process(delta: float) -> void:
74 var input_pkt := input.get_player_input()
76 if input_is_relative_to_camera:
77 cameraHandler.update(input_pkt, delta)
78 cameraHandler.camera_distance = PlayerVariables.camera_distance + velocity.length() / 4
79 input_pkt.player_movement_direction = cameraHandler.get_xz_direction_relative_to_camera(
80 input_pkt.player_movement_direction
83 model.update(input_pkt, delta)
85 input_pkt.queue_free()