Skip to content

Commit 4bf2fe7

Browse files
committed
Fix physics engine error by queueing ItemDropService.Spawn() rather than queueing Die
1 parent 8d8e95b commit 4bf2fe7

File tree

7 files changed

+24
-31
lines changed

7 files changed

+24
-31
lines changed

Entity/AttackRange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ private void OnBodyEntered(Node2D body)
4040

4141
var entityType = predPreyEntity.EntityType();
4242
if (_predatorTypes.Contains(entityType))
43-
_parentEntity.QueueDie();
43+
_parentEntity.Die();
4444
}
4545
}

Entity/Instance/Cow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public partial class Cow : PredatorPreyEntity<Cow>, IFreeze
1717
protected override void DieCustomLogic()
1818
{
1919
var beef = ItemProvider.Singleton.Get<Beef>();
20-
ItemDropService.Singleton.Spawn(beef, 1, Position);
20+
ItemDropService.Singleton.QueueSpawn(beef, 1, Position);
2121
}
2222
}

Entity/Instance/Player.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void DetectWeaponHit()
6767
return;
6868
if (collider is not IKillableEntity entity)
6969
throw new System.Exception($"Player attacked collider ({collider}) that was not an IEntity");
70-
entity.QueueDie();
70+
entity.Die();
7171
}
7272
}
7373

Entity/Instance/Tree.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,11 @@ public void RegisterEntityContainer(EntityContainer<Tree> container)
1313
_entityContainer = container;
1414
}
1515

16-
public void QueueDie()
17-
{
18-
_died = true;
19-
}
20-
21-
private void Die()
16+
public void Die()
2217
{
2318
if (!_entityContainer.TryRemoveEntity(this))
2419
throw new System.Exception(
2520
$"Internal error: Unable to remove entity {Name} from entity container {_entityContainer.GetType()} on death.");
2621
QueueFree();
2722
}
28-
29-
public override void _PhysicsProcess(double delta)
30-
{
31-
if (_died) Die();
32-
}
3323
}

Entity/PredatorPreyEntity.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,11 @@ public void RegisterEntityContainer(EntityContainer<TEntity> container)
6363
// - Spawning an ItemDrop involves instantiating an Area2D in the scene tree
6464
// - The Godot physics engine does not allow modifying an Area2D while in an event handler
6565
// - Hence, we need to queue the die request and only handle it once we exit the event handler
66-
public void QueueDie()
67-
{
68-
_died = true;
69-
}
70-
71-
private void Die()
66+
public void Die()
7267
{
7368
DieCustomLogic();
7469
if (this is not TEntity derivedEntity)
75-
throw new KsInvalidCastException(nameof(QueueDie), nameof(PredatorPreyEntity<TEntity>), nameof(TEntity),
70+
throw new KsInvalidCastException(nameof(Die), nameof(PredatorPreyEntity<TEntity>), nameof(TEntity),
7671
"Likely because entity inheritance hierarchy was not set up correctly.");
7772
if (!_entityContainer.TryRemoveEntity(derivedEntity))
7873
throw new System.Exception(
@@ -111,12 +106,6 @@ public override void _Ready()
111106

112107
public override void _PhysicsProcess(double delta)
113108
{
114-
if (_died)
115-
{
116-
Die();
117-
return;
118-
}
119-
120109
if (_frozen) return;
121110

122111
var move = Vector2.Zero;

Interface/IKillableEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ namespace KentingStation.Interface;
33
// Represents in-game characters which can be killed, including friendly and hostile mobs
44
public interface IKillableEntity
55
{
6-
public void QueueDie();
6+
public void Die();
77
}

Item/ItemDropService.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using Godot;
23
using KentingStation.Common.Util;
34
using KentingStation.Interface;
@@ -7,6 +8,7 @@ namespace KentingStation.Item;
78

89
public 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

Comments
 (0)