batrix/scripts/gameplay/player/player.gd

94 lines
2.3 KiB
GDScript

class_name Player
extends CharacterBody3D
static var instances: Array[Player]
@export var cursor_color: Color
@export var _input_mode: Inputer.Mode = Inputer.Mode.KB_MOUSE
@export var _device_index: int = 0
@export var _respawn_height: float = -5
@export_group("References")
@export var attack: PlayerAttacker
@export var _cursor: PlayerCursor
var stats: PlayerStats = PlayerStats.new()
var movement: PlayerMovementHandler = PlayerMovementHandler.new()
var aiming: PlayerAimingHandler = PlayerAimingHandler.new()
var _respawn_point: Vector3
static func is_single_player() -> bool:
return instances.size() == 1
func _ready() -> void:
_respawn_point = global_position
instances.append(self)
func _process(_delta: float) -> void:
var aim_pos := global_position + aiming.aim_offset
Debugger.marker("aim", aim_pos + Vector3.UP)
Debugger.vector("aimv", Vector3(aim_pos.x, 0, aim_pos.z), aim_pos + Vector3.UP)
func _physics_process(delta: float) -> void:
_aiming()
var can_move := not attack.is_hitting()
velocity = movement.movement(velocity, delta, is_on_floor(), can_move)
move_and_slide()
if not attack.is_hitting() and aiming.aim_offset.length() > 0:
look_at(global_position + aiming.aim_offset, Vector3.UP, true)
_process_respawning()
_cursor.handle_cursor(self, delta)
func _unhandled_input(event: InputEvent) -> void:
var mode := Inputer.get_event_mode(event)
if (
not input_mode_is(mode)
or (
not Player.is_single_player()
and _input_mode == Inputer.Mode.CONTROLLER
and event.device != _device_index
)
):
return
aiming.handle_input(event, mode)
movement.handle_input(event, mode)
if event.is_action_pressed("attack"):
attack.attack()
func input_mode_is(mode: Inputer.Mode) -> bool:
return (
(Player.is_single_player() and mode == Inputer.mode)
or (not Player.is_single_player() and _input_mode == mode)
)
func _aiming() -> void:
if input_mode_is(Inputer.Mode.CONTROLLER):
aiming.controller_aiming(movement.move_input)
if input_mode_is(Inputer.Mode.KB_MOUSE):
var mouse_pos := get_viewport().get_mouse_position()
if get_viewport().get_visible_rect().has_point(mouse_pos):
aiming.mouse_aiming(mouse_pos, global_position, is_on_floor())
func _process_respawning() -> void:
if global_position.y < _respawn_height:
global_position = _respawn_point
velocity = Vector3.ZERO
reset_physics_interpolation()