refactor: throw on uninitialized PlayerInteractionController instead of logging

This commit is contained in:
2026-08-28 23:00:09 +02:00
parent 9fdd565637
commit 888e39e15d
3 changed files with 20 additions and 12 deletions
@@ -3,9 +3,9 @@ using Game.Features.Interactions.Application;
using Game.Features.Interactions.Contracts; using Game.Features.Interactions.Contracts;
using Game.Features.Interactions.Domain; using Game.Features.Interactions.Domain;
using Game.Features.Items.Contracts; using Game.Features.Items.Contracts;
using Game.Features.Logging.Contracts;
using Game.Features.Players.Contracts; using Game.Features.Players.Contracts;
using Game.Godot; using Game.Godot;
using Game.Godot.Exceptions;
using Game.Godot.Extensions; using Game.Godot.Extensions;
using Game.Godot.Mappings; using Game.Godot.Mappings;
using Godot; using Godot;
@@ -35,10 +35,8 @@ public partial class PlayerInteractionController : Node, IItemSpawnPoint
[Export] [Export]
public Node3D RightHandAnchor; public Node3D RightHandAnchor;
private const string LogCategory = "PlayerInteraction";
private IItemInteractionService _items; private IItemInteractionService _items;
private IInteractionHandler _interactions; private IInteractionHandler _interactions;
private ILogger _logger;
private PlayerId _playerId; private PlayerId _playerId;
private readonly HashSet<Hand> _dropActions = []; private readonly HashSet<Hand> _dropActions = [];
private readonly ResolvePickupAvailability _resolvePickupAvailability = new(); private readonly ResolvePickupAvailability _resolvePickupAvailability = new();
@@ -158,14 +156,12 @@ public partial class PlayerInteractionController : Node, IItemSpawnPoint
public void Initialize( public void Initialize(
PlayerId playerId, PlayerId playerId,
IItemInteractionService items, IItemInteractionService items,
IInteractionHandler interactions, IInteractionHandler interactions
ILogger logger
) )
{ {
_playerId = playerId; _playerId = playerId;
_items = items; _items = items;
_interactions = interactions; _interactions = interactions;
_logger = logger;
} }
public Vector3 GetItemSpawnPosition() => CalculateDropPosition(); public Vector3 GetItemSpawnPosition() => CalculateDropPosition();
@@ -179,9 +175,7 @@ public partial class PlayerInteractionController : Node, IItemSpawnPoint
{ {
if (_items is null || _interactions is null) if (_items is null || _interactions is null)
{ {
_logger?.Warning(LogCategory, "Skipped frame: controller not yet initialized."); throw new ControllerNotInitializedException(Name);
PromptController.Clear();
return;
} }
if (GamePause.IsPaused || !MouseCapture.IsCaptured) if (GamePause.IsPaused || !MouseCapture.IsCaptured)
@@ -197,8 +191,7 @@ public partial class PlayerInteractionController : Node, IItemSpawnPoint
{ {
if (_items is null || _interactions is null) if (_items is null || _interactions is null)
{ {
_logger?.Warning(LogCategory, "Ignored input: controller not yet initialized."); throw new ControllerNotInitializedException(Name);
return;
} }
if (GamePause.IsPaused || !MouseCapture.IsCaptured) if (GamePause.IsPaused || !MouseCapture.IsCaptured)
@@ -0,0 +1,15 @@
using System;
namespace Game.Godot.Exceptions;
/// <summary>
/// Thrown when a controller is used before it has been initialized.
/// </summary>
public sealed class ControllerNotInitializedException(string nodeName)
: Exception(BuildMessage(nodeName))
{
public string NodeName { get; } = nodeName;
private static string BuildMessage(string nodeName)
=> $"{nodeName}: controller was used before it was initialized.";
}
+1 -1
View File
@@ -82,7 +82,7 @@ public partial class GameBootstrap : Node
); );
ItemInteractionService itemInteractions = new(new HandsInventory(), ItemWorld, presenter); ItemInteractionService itemInteractions = new(new HandsInventory(), ItemWorld, presenter);
Interact interactions = new(interactionRegistry); Interact interactions = new(interactionRegistry);
PlayerInteraction.Initialize(new PlayerId(Guid.NewGuid()), itemInteractions, interactions, _logger); PlayerInteraction.Initialize(new PlayerId(Guid.NewGuid()), itemInteractions, interactions);
} }
public override void _Ready() public override void _Ready()