From 00627a7e917f228dc173794b64f15ac3aa32d5ab Mon Sep 17 00:00:00 2001 From: Adrian Mazur Date: Mon, 24 Aug 2026 17:26:31 +0200 Subject: [PATCH] feat: add spawn item console command --- .../Application/Commands/SpawnItemCommand.cs | 44 +++++++++++++++++++ .../Commands/SpawnItemCommand.cs.uid | 1 + src/Godot/GameBootstrap.cs | 1 + 3 files changed, 46 insertions(+) create mode 100644 src/Features/Console/Application/Commands/SpawnItemCommand.cs create mode 100644 src/Features/Console/Application/Commands/SpawnItemCommand.cs.uid diff --git a/src/Features/Console/Application/Commands/SpawnItemCommand.cs b/src/Features/Console/Application/Commands/SpawnItemCommand.cs new file mode 100644 index 0000000..22988a6 --- /dev/null +++ b/src/Features/Console/Application/Commands/SpawnItemCommand.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Game.Features.Console.Contracts; +using Game.Features.Items.Contracts; + +namespace Game.Features.Console.Application.Commands; + +public sealed class SpawnItemCommand : ICommand +{ + private readonly IItemSpawner _spawner; + private readonly IItemSpawnPoint _spawnPoint; + + public string Name => "spawn_item"; + public string Description => "Spawns an item in front of the player."; + public IReadOnlyList Args { get; } + + public SpawnItemCommand(IItemSpawner spawner, IItemSpawnPoint spawnPoint) + { + _spawner = spawner; + _spawnPoint = spawnPoint; + + string[] definitions = spawner.DefinitionIds + .Select(id => id.Value) + .OrderBy(id => id, StringComparer.Ordinal) + .ToArray(); + + Args = new CommandArgsBuilder().AddChoiceArg("item", definitions).Build(); + } + + public CommandResult Execute(CommandArgValues args) + { + ItemDefinitionId definitionId = new(args.GetString(0)); + ItemActionResult result = _spawner.TrySpawn( + definitionId, + _spawnPoint.GetItemSpawnPosition(), + out ItemInstanceId itemId + ); + + return result.Success + ? CommandResult.Ok($"Spawned {definitionId} ({itemId}).") + : CommandResult.Fail(result.Message); + } +} diff --git a/src/Features/Console/Application/Commands/SpawnItemCommand.cs.uid b/src/Features/Console/Application/Commands/SpawnItemCommand.cs.uid new file mode 100644 index 0000000..e3d37ec --- /dev/null +++ b/src/Features/Console/Application/Commands/SpawnItemCommand.cs.uid @@ -0,0 +1 @@ +uid://e8m3q6v1r9k4w diff --git a/src/Godot/GameBootstrap.cs b/src/Godot/GameBootstrap.cs index 945602b..2320d01 100644 --- a/src/Godot/GameBootstrap.cs +++ b/src/Godot/GameBootstrap.cs @@ -48,6 +48,7 @@ public partial class GameBootstrap : Node registry.Register(new EchoCommand()); registry.Register(new ClearCommand()); registry.Register(new NoclipCommand(Player)); + registry.Register(new SpawnItemCommand(ItemSpawner, PlayerInteraction)); registry.Register(new HostCommand(MultiplayerController)); registry.Register(new JoinCommand(MultiplayerController));