diff --git a/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs b/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs index b137411..bb7441b 100644 --- a/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs +++ b/SelfPropelledArtilleryUnit/DrawingObjectArtillery.cs @@ -40,5 +40,63 @@ namespace Artilleries public string GetInfo() => _artillery?.GetDateForSave(); public static IDrawingObject Create(string data) => new DrawingObjectArtillery(data.CreateDrawingArtillery()); + + public bool Equals(IDrawingObject? other) + { + if (other == null) + { + return false; + } + + var otherArtillery = other as DrawingObjectArtillery; + + if (otherArtillery == null) + { + return false; + } + + var artillery = _artillery.Artillery; + var otherArtilleryArtillery = otherArtillery._artillery.Artillery; + + if (artillery.GetType() != otherArtilleryArtillery.GetType()) + { + return false; + } + + if (artillery.Speed != otherArtilleryArtillery.Speed) + { + return false; + } + + if (artillery.Weight != otherArtilleryArtillery.Weight) + { + return false; + } + + if (artillery.BodyColor != otherArtilleryArtillery.BodyColor) + { + return false; + } + + if (artillery is EntityAdvancedArtillery advanced && otherArtilleryArtillery is EntityAdvancedArtillery otherAdvanced) + { + if (advanced.DopColor != otherAdvanced.DopColor) + { + return false; + } + + if (advanced.Weapon != otherAdvanced.Weapon) + { + return false; + } + + if (advanced.SalvoBattery != otherAdvanced.SalvoBattery) + { + return false; + } + } + + return true; + } } } diff --git a/SelfPropelledArtilleryUnit/IDrawingObject.cs b/SelfPropelledArtilleryUnit/IDrawingObject.cs index e2fddfe..6c2dd75 100644 --- a/SelfPropelledArtilleryUnit/IDrawingObject.cs +++ b/SelfPropelledArtilleryUnit/IDrawingObject.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Artilleries { - internal interface IDrawingObject + internal interface IDrawingObject : IEquatable { public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs index eeab600..aae0c0e 100644 --- a/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs +++ b/SelfPropelledArtilleryUnit/SetArtilleriesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Artilleries { internal class SetArtilleriesGeneric - where T : class + where T : class, IEquatable { private readonly List _places; public int Count => _places.Count; @@ -36,6 +36,11 @@ namespace Artilleries return -1; } + if (_places.Contains(artillery)) + { + return -1; + } + _places.Insert(position, artillery); return position;