2026-02-11 03:11:28 +00:00
@ icon ( " res://assets/icons/player.svg " )
2026-02-13 03:39:13 +00:00
#@tool
2026-02-11 03:01:44 +00:00
class_name Player
2026-02-13 16:35:50 +00:00
extends CharacterBody2D
2026-02-11 03:01:44 +00:00
2026-02-13 03:39:13 +00:00
## signals
signal turn_finished
2026-02-11 03:01:44 +00:00
## export variables
@ export var attributes : PlayerAttributes ## The attributes resource that defines the player's abilities, vitals etc.
2026-02-13 03:39:13 +00:00
## regular variables
var is_active : bool : set = set_active
var player_label_offset : Vector2
2026-02-11 03:01:44 +00:00
## 'onready' variables
2026-02-13 03:39:13 +00:00
## child nodes
@ onready var player_camera : Camera2D = $ PlayerCamera
@ onready var player_label : PlayerLabel = $ PlayerLabel
2026-02-11 03:01:44 +00:00
@ onready var player_nav : NavigationAgent2D = $ PlayerNav
2026-02-13 03:39:13 +00:00
@ onready var nav_target : Sprite2D = $ NavTarget
2026-02-11 03:01:44 +00:00
@ onready var player_sprite : Sprite2D = $ PlayerSprite
2026-02-13 03:39:13 +00:00
@ onready var player_ui : CanvasLayer = $ PlayerUI
@ onready var end_turn_button : Button = % EndTurnButton
2026-02-11 03:01:44 +00:00
func _ready ( ) - > void :
2026-02-13 03:39:13 +00:00
## signals
end_turn_button . connect ( " button_down " , end_turn )
player_nav . connect ( " velocity_computed " , on_velocity_computed )
is_active = false
2026-02-11 03:01:44 +00:00
if attributes :
2026-02-13 03:39:13 +00:00
player_label . player_name = attributes . player_name
player_label . player_number = attributes . player_number
player_label . player_position = attributes . player_position
player_sprite . texture = attributes . player_texture
player_label_offset = player_label . position
func _process ( _delta : float ) - > void :
player_label . global_position = global_position + player_label_offset
if is_active :
nav_target . global_position = get_global_mouse_position ( )
func _physics_process ( delta : float ) - > void :
2026-02-13 16:35:50 +00:00
var desired_velocity : Vector2 = global_position . direction_to ( player_nav . get_next_path_position ( ) ) * attributes . speed ## TODO: this doesn't seem to affect actual movement speed
player_nav . velocity = desired_velocity
#if player_nav.target_position:
#player_nav.velocity = global_position.direction_to(player_nav.get_next_path_position())
##var movement_direction =
##translate(movement_direction)
2026-02-13 03:39:13 +00:00
func _input ( event : InputEvent ) - > void :
if event . is_action_pressed ( " end_turn " ) :
end_turn ( )
if event is InputEventMouseButton and event . is_pressed ( ) and event . button_index == 1 :
var click_position_global = get_global_mouse_position ( )
print_debug ( " %s wants to navigate to %s " % [ attributes . player_name , click_position_global ] )
player_nav . target_position = click_position_global
var target_distance = player_nav . distance_to_target ( )
print_debug ( " That ' s %s pixels/cm away " % target_distance )
look_at ( player_nav . target_position )
## Setters
func set_active ( active : bool ) - > void :
is_active = active
if is_active :
print_debug ( " %s says: ' My turn! ' " % attributes . player_name )
player_ui . visible = true
player_camera . enabled = true
player_camera . make_current ( )
nav_target . visible = true
set_process_input ( true )
else :
print_debug ( " %s says: ' It ' s not my turn ' " % attributes . player_name )
player_ui . visible = false
player_camera . enabled = false
nav_target . visible = false
set_process_input ( false )
## Helper functions/signal handlers
func end_turn ( ) - > void :
print_debug ( " %s says: I ' d like my turn to end. " % attributes . player_name )
is_active = false
turn_finished . emit ( )
func on_velocity_computed ( safe_velocity : Vector2 ) - > void :
2026-02-13 16:35:50 +00:00
velocity = safe_velocity
move_and_slide ( )