Refactor layers.

This commit is contained in:
Gandifil
2023-12-01 23:21:03 +03:00
parent 1a31bd7728
commit 094e9a1563
2 changed files with 32 additions and 27 deletions
@@ -32,8 +32,8 @@ namespace MonoGame.Extended.Collisions
/// <param name="boundary">Boundary of the collision tree.</param>
public CollisionComponent(RectangleF boundary)
{
var layer = new Layer(DEFAULT_LAYER_NAME, new QuadTreeSpace(boundary));
Add(layer);
var layer = new Layer(new QuadTreeSpace(boundary));
Add(DEFAULT_LAYER_NAME, layer);
AddCollisionBetweenLayer(layer, layer);
}
@@ -103,22 +103,30 @@ namespace MonoGame.Extended.Collisions
/// <summary>
/// Add the new layer. The name of layer must be unique.
/// </summary>
/// <param name="name">Name of layer</param>
/// <param name="layer">The new layer</param>
public void Add(Layer layer)
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null</exception>
public void Add(string name, Layer layer)
{
if (!_layers.TryAdd(layer.Name, layer))
throw new DuplicateNameException(layer.Name);
if (layer.Name != DEFAULT_LAYER_NAME)
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (!_layers.TryAdd(name, layer))
throw new DuplicateNameException(name);
if (name != DEFAULT_LAYER_NAME)
AddCollisionBetweenLayer(_layers[DEFAULT_LAYER_NAME], layer);
}
/// <summary>
/// Add the new layer. The name of layer must be unique.
/// Remove the layer and all layer's collisions.
/// </summary>
/// <param name="layer">The new layer</param>
public void Remove(Layer layer)
/// <param name="name">The name of the layer to delete</param>
/// <param name="layer">The layer to delete</param>
public void Remove(string name = null, Layer layer = null)
{
_layers.Remove(layer.Name);
name ??= _layers.First(x => x.Value == layer).Key;
_layers.Remove(name, out layer);
_layerCollision.RemoveWhere(tuple => tuple.Item1 == layer || tuple.Item2 == layer);
}
@@ -1,34 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MonoGame.Extended.Collisions.QuadTree;
namespace MonoGame.Extended.Collisions.Layers;
/// <summary>
///
/// Layer is a group of collision's actors.
/// </summary>
public class Layer
{
/// <summary>
/// Name of layer
/// If this property equals true, layer always will reset collision space.
/// </summary>
public string Name { get; }
public bool IsDynamic { get; set; } = true;
public ISpaceAlgorithm Space { get; }
public Layer(string name, ISpaceAlgorithm spaceAlgorithm)
/// <summary>
/// The space, which contain actors.
/// </summary>
public readonly ISpaceAlgorithm Space;
/// <summary>
/// Constructor for layer
/// </summary>
/// <param name="spaceAlgorithm">A space algorithm for actors</param>
/// <exception cref="ArgumentNullException"><paramref name="spaceAlgorithm"/> is null</exception>
public Layer(ISpaceAlgorithm spaceAlgorithm)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (spaceAlgorithm is null)
throw new ArgumentNullException(nameof(spaceAlgorithm));
Name = name;
Space = spaceAlgorithm;
Space = spaceAlgorithm ?? throw new ArgumentNullException(nameof(spaceAlgorithm));
}
/// <summary>