move player cursor handling to the player scene

This commit is contained in:
2025-08-29 19:02:11 +10:00
parent 8e26c88aa8
commit 3369f1c31d
6 changed files with 108 additions and 98 deletions

View File

@@ -13,6 +13,7 @@ static var instances: Array[Player]
@export_group("References")
@export var attack: PlayerAttacker
@export var _cursor: PlayerCursor
var stats: PlayerStats = PlayerStats.new()
var movement: PlayerMovementHandler = PlayerMovementHandler.new()
@@ -49,6 +50,7 @@ func _physics_process(delta: float) -> void:
look_at(global_position + aiming.aim_offset, Vector3.UP, true)
_process_respawning()
_cursor.handle_cursor(self, delta)
func _unhandled_input(event: InputEvent) -> void:

View File

@@ -0,0 +1,50 @@
class_name PlayerCursor
extends CanvasLayer
@export var _side_change_speed: float = 15
@export var _screen_inset: float = 100
@export_group("References")
@export var _base: Control
@export var _bat: Control
@export var _arrow: Control
var _side: float = 0
func handle_cursor(player: Player, delta: float) -> void:
var cursor_pos_world := player.attack.global_position + player.aiming.aim_offset
var cursor_pos_screen := MainCamera.instance.unproject_position(cursor_pos_world)
var clamp_corner_min := Vector2.ZERO
var clamp_corner_max := get_viewport().get_visible_rect().size
if player.input_mode_is(Inputer.Mode.CONTROLLER):
clamp_corner_min += Vector2(_screen_inset, _screen_inset)
clamp_corner_max -= Vector2(_screen_inset, _screen_inset)
_base.position = (
cursor_pos_screen.clamp(clamp_corner_min, clamp_corner_max) - _base.size / 2
)
_side = lerpf(
_side as float,
(PI / 2.0) * (1.0 if player.attack.side == PlayerAttacker.Side.LEFT else -1.0),
_side_change_speed * delta
)
var aim_offset_normalized := player.aiming.aim_offset.normalized()
var bat_rotation_point_screen := MainCamera.instance.unproject_position(
cursor_pos_world + aim_offset_normalized.rotated(Vector3.UP, _side as float)
)
var arrow_rotation_point_screen := MainCamera.instance.unproject_position(
cursor_pos_world + aim_offset_normalized
)
_bat.rotation = cursor_pos_screen.angle_to_point(bat_rotation_point_screen)
_arrow.rotation = (
cursor_pos_screen.angle_to_point(arrow_rotation_point_screen) + PI / 2
)
_base.modulate = player.cursor_color
if player.input_mode_is(Inputer.Mode.KB_MOUSE):
_base.position = (get_viewport().get_mouse_position() - _base.size / 2)

View File

@@ -0,0 +1 @@
uid://ddx56siie1sf4

View File

@@ -1,15 +1,9 @@
extends CanvasLayer
@export var _side_change_speed: float = 15
@export var _screen_inset: float = 100
@export_group("References")
@export var _single_cursor: Control
var _cursor_scene := preload("res://scenes/ui/cursor_base.tscn")
var _cursors: Dictionary[int, PlayerCursor]
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
@@ -22,87 +16,3 @@ func _process(_delta: float) -> void:
_single_cursor.visible = true
return
_single_cursor.visible = false
func _physics_process(delta: float) -> void:
call_deferred("_handle_cursors", delta)
func _handle_cursors(delta: float) -> void:
for player_index in range(Player.instances.size()):
_handle_cursor(player_index, delta)
func _init_player_cursor(id: int) -> void:
var cursor_node: Control = _cursor_scene.instantiate() as Control
add_child(cursor_node)
_cursors[id] = PlayerCursor.new(
cursor_node,
cursor_node.get_node("CursorBat") as Control,
cursor_node.get_node("CursorArrow") as Control
)
func _handle_cursor(player_index: int, delta: float) -> void:
var player := Player.instances[player_index]
var id := player.get_instance_id()
if not _cursors.has(id):
_init_player_cursor(id)
return
var cursor_pos_world := player.attack.global_position + player.aiming.aim_offset
var cursor_pos_screen := MainCamera.instance.unproject_position(cursor_pos_world)
var clamp_corner_min := Vector2.ZERO
var clamp_corner_max := get_viewport().get_visible_rect().size
if player.input_mode_is(Inputer.Mode.CONTROLLER):
clamp_corner_min += Vector2(_screen_inset, _screen_inset)
clamp_corner_max -= Vector2(_screen_inset, _screen_inset)
var cursor: PlayerCursor = _cursors[id]
cursor.base.position = (
cursor_pos_screen.clamp(clamp_corner_min, clamp_corner_max)
- cursor.base.size / 2
)
cursor.side = lerpf(
cursor.side as float,
(PI / 2.0) * (1.0 if player.attack.side == PlayerAttacker.Side.LEFT else -1.0),
_side_change_speed * delta
)
var aim_offset_normalized := player.aiming.aim_offset.normalized()
var bat_rotation_point_screen := MainCamera.instance.unproject_position(
(
cursor_pos_world
+ aim_offset_normalized.rotated(Vector3.UP, cursor.side as float)
)
)
var arrow_rotation_point_screen := MainCamera.instance.unproject_position(
cursor_pos_world + aim_offset_normalized
)
cursor.bat.rotation = cursor_pos_screen.angle_to_point(bat_rotation_point_screen)
cursor.arrow.rotation = (
cursor_pos_screen.angle_to_point(arrow_rotation_point_screen) + PI / 2
)
cursor.base.modulate = player.cursor_color
if player.input_mode_is(Inputer.Mode.KB_MOUSE):
cursor.base.position = (
get_viewport().get_mouse_position() - cursor.base.size / 2
)
class PlayerCursor:
var base: Control
var bat: Control
var arrow: Control
var side: float = 0
func _init(_base: Control, _bat: Control, _arrow: Control) -> void:
base = _base
bat = _bat
arrow = _arrow