From 0835f66ac4eb64fe53d66101ead777993f1ca265 Mon Sep 17 00:00:00 2001 From: teatov Date: Mon, 3 Mar 2025 14:06:41 +1000 Subject: [PATCH] change dictionaries to inner classes in `main_camera` and `cursor` --- scenes/ui/cursor.tscn | 2 ++ scripts/debug/debug_collision_shapes.gd | 1 - scripts/globals/cursor.gd | 39 ++++++++++++++++--------- scripts/main_camera.gd | 21 ++++++++----- scripts/player/player_aiming.gd | 1 - scripts/player/player_movement.gd | 1 - 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/scenes/ui/cursor.tscn b/scenes/ui/cursor.tscn index 07e0762..c72a202 100644 --- a/scenes/ui/cursor.tscn +++ b/scenes/ui/cursor.tscn @@ -9,6 +9,8 @@ process_priority = 1000 process_physics_priority = 1000 layer = 128 script = ExtResource("1_tkygf") +side_change_speed = null +screen_inset = null [node name="CursorBase" type="Control" parent="."] layout_mode = 3 diff --git a/scripts/debug/debug_collision_shapes.gd b/scripts/debug/debug_collision_shapes.gd index 250b163..3fc3d98 100644 --- a/scripts/debug/debug_collision_shapes.gd +++ b/scripts/debug/debug_collision_shapes.gd @@ -1,5 +1,4 @@ class_name DebugCollisionShapes -extends RefCounted const MARGIN = 0.01 diff --git a/scripts/globals/cursor.gd b/scripts/globals/cursor.gd index c84f66b..2eabf69 100644 --- a/scripts/globals/cursor.gd +++ b/scripts/globals/cursor.gd @@ -33,12 +33,11 @@ func _handle_cursors(delta: float) -> void: func _init_player_cursor(id: int) -> void: var cursor_node: Control = _cursor_scene.instantiate() as Control add_child(cursor_node) - _cursors[id] = { - "base": cursor_node, - "bat": cursor_node.get_node("CursorBat"), - "arrow": cursor_node.get_node("CursorArrow"), - "side": 0 - } + _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: @@ -58,15 +57,15 @@ func _handle_cursor(player_index: int, delta: float) -> void: clamp_corner_min += Vector2(screen_inset, screen_inset) clamp_corner_max -= Vector2(screen_inset, screen_inset) - var cursor: Dictionary = _cursors[id] + var cursor: PlayerCursor = _cursors[id] - cursor["base"].position = ( + cursor.base.position = ( cursor_pos_screen.clamp(clamp_corner_min, clamp_corner_max) - - cursor["base"].size / 2 + - cursor.base.size / 2 ) - cursor["side"] = lerpf( - cursor["side"] as float, + cursor.side = lerpf( + cursor.side as float, (PI / 2.0) * (1.0 if player.attack.side == PlayerAttack.Side.LEFT else -1.0), side_change_speed * delta ) @@ -75,14 +74,26 @@ func _handle_cursor(player_index: int, delta: float) -> void: var bat_rotation_point_screen := Referencer.main_camera.unproject_position( ( cursor_pos_world - + aim_offset_normalized.rotated(Vector3.UP, cursor["side"] as float) + + aim_offset_normalized.rotated(Vector3.UP, cursor.side as float) ) ) var arrow_rotation_point_screen := Referencer.main_camera.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( + 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 ) + + +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 diff --git a/scripts/main_camera.gd b/scripts/main_camera.gd index 214b5a8..9252575 100644 --- a/scripts/main_camera.gd +++ b/scripts/main_camera.gd @@ -30,33 +30,33 @@ func _process(delta: float) -> void: func _follow(player: Player, delta: float) -> Vector3: var id := player.get_instance_id() if not _player_offsets.has(id): - _player_offsets[id] = {"floor_height": 0, "aim_offset": Vector3.ZERO} + _player_offsets[id] = PlayerOffset.new() - var player_offset: Dictionary = _player_offsets[id] + var player_offset: PlayerOffset = _player_offsets[id] var player_position := player.global_position if player.is_on_floor(): - player_offset["floor_height"] = player_position.y - player_position.y = player_offset["floor_height"] + Projectile.HEIGHT + player_offset.floor_height = player_position.y + player_position.y = player_offset.floor_height + Projectile.HEIGHT if Referencer.players_count > 1: - player_offset["aim_offset"] = Vector3.ZERO + player_offset.aim_offset = Vector3.ZERO elif player.input_mode_is(Inputer.Mode.KB_MOUSE): - player_offset["aim_offset"] = player.aiming.aim_offset + player_offset.aim_offset = player.aiming.aim_offset elif player.input_mode_is(Inputer.Mode.CONTROLLER): var new_aim_offset := ( Vector3.ZERO if player.aiming.aim_input.length() == 0 else player.aiming.aim_offset ) - player_offset["aim_offset"] = player_offset["aim_offset"].lerp( + player_offset.aim_offset = player_offset.aim_offset.lerp( new_aim_offset, _aim_damping * delta ) var follow_position := ( player_position + ( - player_offset["aim_offset"] as Vector3 + player_offset.aim_offset as Vector3 * ( _aim_offset_factor_mouse if player.input_mode_is(Inputer.Mode.KB_MOUSE) @@ -68,3 +68,8 @@ func _follow(player: Player, delta: float) -> Vector3: Debugger.circle("follow_position" + str(player.name), follow_position) return follow_position + + +class PlayerOffset: + var floor_height: float = 0 + var aim_offset: Vector3 = Vector3.ZERO diff --git a/scripts/player/player_aiming.gd b/scripts/player/player_aiming.gd index ab292fd..df8bc61 100644 --- a/scripts/player/player_aiming.gd +++ b/scripts/player/player_aiming.gd @@ -1,5 +1,4 @@ class_name PlayerAiming -extends RefCounted @export var _controller_aim_offset: float = 6 @export var _vertical_aim_aspect: float = 1.5 diff --git a/scripts/player/player_movement.gd b/scripts/player/player_movement.gd index 5ede976..8925d30 100644 --- a/scripts/player/player_movement.gd +++ b/scripts/player/player_movement.gd @@ -1,5 +1,4 @@ class_name PlayerMovement -extends RefCounted @export var _move_speed: float = 8 @export var _move_acceleration: float = 100