1+ using System . Collections . Generic ;
12using Godot ;
23using KentingStation . Common . Util ;
34using KentingStation . Interface ;
@@ -7,6 +8,7 @@ namespace KentingStation.Item;
78
89public partial class ItemDropService : Node2D
910{
11+ private readonly List < ItemSpawnInfo > _spawnQueue = [ ] ;
1012 private PackedScene _itemDrop = ResourceLoader . Load < PackedScene > ( "res://Scene/ItemDrop.tscn" ) ;
1113
1214 private ItemDropService ( )
@@ -24,15 +26,27 @@ public override void _Ready()
2426 // Called every frame. 'delta' is the elapsed time since the previous frame.
2527 public override void _Process ( double delta )
2628 {
29+ if ( _spawnQueue . Count == 0 )
30+ return ;
31+ foreach ( var itemInfo in _spawnQueue )
32+ Spawn ( itemInfo ) ;
33+ _spawnQueue . Clear ( ) ;
2734 }
2835
29- public void Spawn ( IItem item , int count , Vector2 spawnLocation )
36+ public void QueueSpawn ( IItem item , int count , Vector2 spawnLocation )
37+ {
38+ _spawnQueue . Add ( new ItemSpawnInfo ( item , count , spawnLocation ) ) ;
39+ }
40+
41+ private void Spawn ( ItemSpawnInfo itemInfo )
3042 {
3143 var itemDrop = _itemDrop . Instantiate < ItemDrop > ( ) ;
32- itemDrop . SetItem ( item , count ) ;
44+ itemDrop . SetItem ( itemInfo . Item , itemInfo . Count ) ;
3345
34- var boundedSpawnLocation = Rect2Ex . ClosestPointWithin ( WorldBoundary . Singleton . Boundary , spawnLocation ) ;
46+ var boundedSpawnLocation = Rect2Ex . ClosestPointWithin ( WorldBoundary . Singleton . Boundary , itemInfo . SpawnLocation ) ;
3547 itemDrop . Position = boundedSpawnLocation ;
3648 Singleton . AddChild ( itemDrop ) ;
3749 }
50+
51+ private record struct ItemSpawnInfo ( IItem Item , int Count , Vector2 SpawnLocation ) ;
3852}
0 commit comments