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
27 @export_group("Camera")
29 ## Distance that the camera retreats from the player.
30 ## Mostly relevant for the player's own CameraHandler instance.
31 @export_range(1.0, 10.0) var camera_distance := 4.0
33 ## Remaps absolute input relative to current camera.
34 ## Bad idea for robots, good idea for player control! [br]
36 ## TODO: Probably should be all managed by the InputHandler!
37 @export var camera_relative_input: bool = false
40 @export_group("Movement")
42 ## Rate of speed while walking.
43 ## Used to override the Walk state's default speed.
44 @export var walk_speed := 5.0
46 ## Rate of acceleration while doing any sort of movement.
47 ## Used mostly by the Walk state.
48 @export var acceleration := 20.0
50 ## Rate of rotation while turning.
51 @export var rotation_speed := 10.0
53 ## Vertical velocity limit for when a fall should trigger a hard landing.
54 @export var fall_landing_limit := 3.0
56 ## Vertical velocity limit for when a fall should trigger a roll.
57 @export var fall_roll_limit := 6.0
60 @export_group("Combat")
62 ## The time window in seconds for player to escape subsequent hits.
63 ## Used by the Hit state.
64 @export var hit_escape_window := 1.0
67 @export_group("Resources")
69 ## Current hit points.
70 @export var hit_points: float = 100.0
72 ## Current stamina points.
73 @export var stamina_points: float = 100.0
75 ## Current energy points.
76 @export var energy_points: float = 0.0
78 ## Current state name of the PlayerModel state machine.
82 ## How long the player has been idle.
83 ## Populated by the Idle state.
86 ## Points to the last meaningful movement of the Player.
87 ## Useful for debugging, movement, and targeting.
88 var last_movement_vector: Vector3
90 ## Records the floor normal.
92 var floor_normal := Vector3.ZERO
94 ## How much the immediate slope influences velocity.
96 var ground_slope_input := 0.0
100 @onready var input: InputHandler = $Input
101 @onready var model: CharacterModel = $Model
102 @onready var visual: CharacterVisual = $Model/Visual
103 @onready var cameraHandler: CameraHandler = $CameraHandler
104 @onready var collision: CollisionShape3D = $Collision
107 func _ready() -> void:
108 visual.assign_skeleton(model.skeleton)
109 last_movement_vector = Vector3.BACK.rotated(Vector3.UP, rotation.y)
112 func _physics_process(delta: float) -> void:
113 var input_pkt := input.get_player_input()
115 if camera_relative_input:
116 cameraHandler.update(input_pkt, delta)
117 cameraHandler.camera_distance = camera_distance + velocity.length() / 4
118 input_pkt.player_movement_direction = cameraHandler.get_xz_direction_relative_to_camera(
119 input_pkt.player_movement_direction
122 model.update(input_pkt, delta)
123 input_pkt.queue_free()