mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 23:19:29 +00:00
Merge pull request #839 from safoster88/feature/check_for_undefined_layer
Check for undefined layer and throw on collision actor insertion
This commit is contained in:
@@ -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")
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ using Xunit;
|
||||
|
||||
namespace MonoGame.Extended.Collisions.Tests
|
||||
{
|
||||
using MonoGame.Extended.Collisions.Layers;
|
||||
|
||||
/// <summary>
|
||||
/// Test collision of actors with various shapes.
|
||||
/// </summary>
|
||||
@@ -376,6 +378,16 @@ namespace MonoGame.Extended.Collisions.Tests
|
||||
Assert.True(dynamicActor.IsColliding);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InsertActor_ThrowsUndefinedLayerException_IfThereIsNoLayerDefined()
|
||||
{
|
||||
var sut = new CollisionComponent();
|
||||
|
||||
var act = () => sut.Insert(new CollisionIndicatingActor(RectangleF.Empty));
|
||||
|
||||
Assert.Throws<UndefinedLayerException>(act);
|
||||
}
|
||||
|
||||
private class CollisionIndicatingActor : ICollisionActor
|
||||
{
|
||||
private RectangleF _bounds;
|
||||
|
||||
Reference in New Issue
Block a user