extends CharacterBody3D class_name Player ## The base class for any input-driven character ## ## The Player class drives the associated Model with the input from the InputHandler, ## which drives the Visual. Each submodule can be swapped out for easy customization. ## The CameraHandler is to allow input to be made relative to the current camera's position ## ## Human-readable name for UI display @export var display_name := "" ## Ceiling for hit points. @export var max_hit_points: float = 100.0 ## Ceiling for stamina points. @export var max_stamina_points: float = 100.0 ## Ceiling for energy points. @export var max_energy_points: float = 100.0 ## Rate of stamina regeneration. @export var stamina_regain_per_sec: float = 80.0 @export_group("Camera") ## Distance that the camera retreats from the player. ## Mostly relevant for the player's own CameraHandler instance. @export_range(1.0, 10.0) var camera_distance := 4.0 ## Remaps absolute input relative to current camera. ## Bad idea for robots, good idea for player control! [br] ## [br] ## TODO: Probably should be all managed by the InputHandler! @export var camera_relative_input: bool = false @export_group("Movement") ## Rate of speed while walking. ## Used to override the Walk state's default speed. @export var walk_speed := 5.0 ## Rate of acceleration while doing any sort of movement. ## Used mostly by the Walk state. @export var acceleration := 20.0 ## Rate of rotation while turning. @export var rotation_speed := 10.0 ## Vertical velocity limit for when a fall should trigger a hard landing. @export var fall_landing_limit := 3.0 ## Vertical velocity limit for when a fall should trigger a roll. @export var fall_roll_limit := 6.0 @export_group("Combat") ## The time window in seconds for player to escape subsequent hits. ## Used by the Hit state. @export var hit_escape_window := 1.0 @export_group("Resources") ## Current hit points. @export var hit_points: float = 100.0 ## Current stamina points. @export var stamina_points: float = 100.0 ## Current energy points. @export var energy_points: float = 0.0 ## Current state name of the PlayerModel state machine. ## For debugging. var state_name := "" ## How long the player has been idle. ## Populated by the Idle state. var idle_time := 0.00 ## Points to the last meaningful movement of the Player. ## Useful for debugging, movement, and targeting. var last_movement_vector: Vector3 ## Records the floor normal. ## For debugging. var floor_normal := Vector3.ZERO ## How much the immediate slope influences velocity. ## For debugging. var ground_slope_input := 0.0 # onready @onready var input: InputHandler = $Input @onready var model: CharacterModel = $Model @onready var visual: CharacterVisual = $Model/Visual @onready var cameraHandler: CameraHandler = $CameraHandler @onready var collision: CollisionShape3D = $Collision func _ready() -> void: visual.assign_skeleton(model.skeleton) last_movement_vector = Vector3.BACK.rotated(Vector3.UP, rotation.y) func _physics_process(delta: float) -> void: var input_pkt := input.get_player_input() if camera_relative_input: cameraHandler.update(input_pkt, delta) cameraHandler.camera_distance = camera_distance + velocity.length() / 4 input_pkt.player_movement_direction = cameraHandler.get_xz_direction_relative_to_camera( input_pkt.player_movement_direction ) model.update(input_pkt, delta) input_pkt.queue_free()