From 094e9a1563f5f62cc9b40f60f9c5db6a801124f2 Mon Sep 17 00:00:00 2001 From: Gandifil Date: Fri, 1 Dec 2023 23:21:03 +0300 Subject: [PATCH] Refactor layers. --- .../CollisionComponent.cs | 28 +++++++++++------ .../Layers/Layer.cs | 31 +++++++++---------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/cs/MonoGame.Extended.Collisions/CollisionComponent.cs b/src/cs/MonoGame.Extended.Collisions/CollisionComponent.cs index 5ec376d2..349acf8e 100644 --- a/src/cs/MonoGame.Extended.Collisions/CollisionComponent.cs +++ b/src/cs/MonoGame.Extended.Collisions/CollisionComponent.cs @@ -32,8 +32,8 @@ namespace MonoGame.Extended.Collisions /// Boundary of the collision tree. 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 /// /// Add the new layer. The name of layer must be unique. /// + /// Name of layer /// The new layer - public void Add(Layer layer) + /// is null + 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); } /// - /// Add the new layer. The name of layer must be unique. + /// Remove the layer and all layer's collisions. /// - /// The new layer - public void Remove(Layer layer) + /// The name of the layer to delete + /// The layer to delete + 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); } diff --git a/src/cs/MonoGame.Extended.Collisions/Layers/Layer.cs b/src/cs/MonoGame.Extended.Collisions/Layers/Layer.cs index dd18a69e..6e97ac87 100644 --- a/src/cs/MonoGame.Extended.Collisions/Layers/Layer.cs +++ b/src/cs/MonoGame.Extended.Collisions/Layers/Layer.cs @@ -1,34 +1,31 @@ using System; -using System.Collections.Generic; -using System.Linq; -using MonoGame.Extended.Collisions.QuadTree; namespace MonoGame.Extended.Collisions.Layers; /// -/// +/// Layer is a group of collision's actors. /// public class Layer { /// - /// Name of layer + /// If this property equals true, layer always will reset collision space. /// - public string Name { get; } - public bool IsDynamic { get; set; } = true; - public ISpaceAlgorithm Space { get; } - public Layer(string name, ISpaceAlgorithm spaceAlgorithm) + /// + /// The space, which contain actors. + /// + public readonly ISpaceAlgorithm Space; + + /// + /// Constructor for layer + /// + /// A space algorithm for actors + /// is null + 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)); } ///