This commit is contained in:
m1aksim1 2022-11-22 17:55:27 +04:00
parent 4fc7d23e2e
commit 4cd90e298f
4 changed files with 84 additions and 33 deletions

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AirFighter
{
internal abstract class AbstractMap
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawningObject _drawningObject = null;
protected int[,] _map = null;
@ -103,5 +103,32 @@ namespace AirFighter
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null ||
_map != other._map ||
_width != other._width ||
_size_x != other._size_x ||
_size_y != other._size_y ||
_height != other._height ||
GetType() != other.GetType() ||
_map.GetLength(0) != other._map.GetLength(0) ||
_map.GetLength(1) != other._map.GetLength(1))
{
return false;
}
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] != other._map[i, j])
{
return false;
}
}
}
return true;
}
}
}

View File

@ -10,34 +10,6 @@
}
public DrawningWarPlane GetWarPlane => _warplane;
public bool Equals(IDrawningObject? other)
{
if (other == null)
{
return false;
}
var otherWarPlane = other as DrawningObjectWarPlane;
if (otherWarPlane == null)
{
return false;
}
var warplane = _warplane.WarPlane;
var otherWarPlaneWarPlane = otherWarPlane._warplane.WarPlane;
if (warplane.Speed != otherWarPlaneWarPlane.Speed)
{
return false;
}
if (warplane.Weight != otherWarPlaneWarPlane.Weight)
{
return false;
}
if (warplane.BodyColor != otherWarPlaneWarPlane.BodyColor)
{
return false;
}
// TODO доделать проверки в случае продвинутого объекта
return true;
}
public float Step => _warplane?.WarPlane?.Step ?? 0;
public RectangleF GetCurrentPosition()
@ -60,9 +32,34 @@
_warplane?.DrawTransport(g);
}
public string GetInfo() => _warplane?.GetDataForSave();
public string GetInfo() => _warplane?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data) => new DrawningObjectWarPlane(data.CreateDrawningWarPlane());
public bool Equals(IDrawningObject? other)
{
if (other is not DrawningObjectWarPlane otherWarPlane)
{
return false;
}
var entity = _warplane.WarPlane;
var otherEntity = otherWarPlane._warplane.WarPlane;
if (entity.GetType() != otherEntity.GetType() ||
entity.Speed != otherEntity.Speed ||
entity.Weight != otherEntity.Weight ||
entity.BodyColor != otherEntity.BodyColor)
{
return false;
}
if (entity is EntityFighter entityFighter &&
otherEntity is EntityFighter otherEntityAirBomber && (
entityFighter.Rocket != otherEntityAirBomber.Rocket ||
entityFighter.DopColor != otherEntityAirBomber.DopColor ||
entityFighter.Wing != otherEntityAirBomber.Wing))
{
return false;
}
return true;
}
}
}

View File

@ -149,6 +149,11 @@ namespace AirFighter
_logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message);
MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (ArgumentException ex)
{
_logger.LogWarning("Ошибка добавления: {0}. Объект: {@Airplane}", ex.Message, warplane);
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Удаление объекта

View File

@ -4,8 +4,30 @@
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
// TODO реализовать логику сравнения
throw new NotImplementedException();
var xWarPlane = x as DrawningObjectWarPlane;
var yWarPlane = y as DrawningObjectWarPlane;
if (xWarPlane == yWarPlane)
{
return 0;
}
if (xWarPlane == null)
{
return 1;
}
if (yWarPlane == null)
{
return -1;
}
var xEntity = xWarPlane.GetWarPlane.WarPlane;
var yEntity = yWarPlane.GetWarPlane.WarPlane;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 ||
xEntity is not EntityFighter xEntityAirBomber ||
yEntity is not EntityFighter yEntityAirBomber)
{
return colorWeight;
}
return xEntityAirBomber.DopColor.ToArgb().CompareTo(yEntityAirBomber.DopColor.ToArgb());
}
}
}