Сравнение объектов

This commit is contained in:
Данила Мочалов 2022-11-21 17:38:47 +04:00
parent 88b702d5b5
commit 38def0fbc5
4 changed files with 43 additions and 3 deletions

View File

@ -39,5 +39,44 @@ namespace Locomotive
public string getInfo() => _locomotive?.getDataForSave();
public static IDrawningObject Create(string data) => new DrawningObjectLocomotive(data.createDrawningLocomotive());
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherLocomotive = other as DrawningObjectLocomotive;
if (otherLocomotive == null)
{
return false;
}
var locomotive = _locomotive.Locomotive;
var otherLocomotiveLocomotive = otherLocomotive._locomotive.Locomotive;
if (locomotive.GetType() != otherLocomotiveLocomotive.GetType()) return false;
if (locomotive.Speed != otherLocomotiveLocomotive.Speed)
{
return false;
}
if (locomotive.Weight != otherLocomotiveLocomotive.Weight)
{
return false;
}
if (locomotive.BodyColor != otherLocomotiveLocomotive.BodyColor)
{
return false;
}
// проверка в случае продвинутого объекта
if (locomotive is EntityWarmlyLocomotive entityWarmlyLocomotive && otherLocomotiveLocomotive is EntityWarmlyLocomotive otherEntityWarmlyLocomotive)
{
if (entityWarmlyLocomotive.ExtraColor != otherEntityWarmlyLocomotive.ExtraColor) return false;
if (entityWarmlyLocomotive.Pipe != otherEntityWarmlyLocomotive.Pipe) return false;
if (entityWarmlyLocomotive.FuelStorage != otherEntityWarmlyLocomotive.FuelStorage) return false;
}
return true;
}
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal interface IDrawningObject
internal interface IDrawningObject : IEquatable<IDrawningObject>
{
/// Шаг перемещения объекта
public float Step { get; }

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal class MapWithSetLocomotivesGeneric <T, U>
where T : class, IDrawningObject
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// Ширина окна отрисовки

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Locomotive
{
internal class SetLocomotivesGeneric <T>
where T : class
where T : class, IEquatable<T>
{
/// Список хранимых объектов
private readonly List<T> _places;
@ -31,6 +31,7 @@ namespace Locomotive
/// Добавление объекта в набор на конкретную позицию
public int Insert(T locomotive, int position)
{
if (_places.Contains(locomotive)) return -1; // Проверка на уникальность
if (position < 0) return -1;
if (Count >= _maxCount) {
Log.Warning("StorageOverflowException");