Check for undefined layer and throw on collision actor insertion

This commit is contained in:
Stephen
2024-02-17 13:15:56 +00:00
parent 6acc822be1
commit 8d95d49924
2 changed files with 25 additions and 1 deletions
@@ -104,7 +104,13 @@ namespace MonoGame.Extended.Collisions
/// <param name="target">Target to insert.</param>
public void Insert(ICollisionActor target)
{
_layers[target.LayerName ?? DEFAULT_LAYER_NAME].Space.Insert(target);
var layerName = target.LayerName ?? DEFAULT_LAYER_NAME;
if (!_layers.TryGetValue(layerName, out var layer))
{
throw new UndefinedLayerException(layerName);
}
layer.Space.Insert(target);
}
/// <summary>
@@ -0,0 +1,18 @@
namespace MonoGame.Extended.Collisions.Layers;
using System;
/// <summary>
/// Thrown when the collision system has no layer defined with the specified name
/// </summary>
public class UndefinedLayerException : Exception
{
/// <summary>
/// Thrown when the collision system has no layer defined with the specified name
/// </summary>
/// <param name="layerName">The undefined layer name</param>
public UndefinedLayerException(string layerName)
: base($"Layer with name '{layerName}' is undefined")
{
}
}