Добавлены классы для сравнения объектов по цвету и типу

This commit is contained in:
Данияр Аглиуллов 2022-10-31 06:03:27 +04:00
parent b0731d4f14
commit d81aa61eb0
4 changed files with 79 additions and 9 deletions

View File

@ -0,0 +1,31 @@
namespace AirBomber
{
internal class AirplaneCompareByColor : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = x as DrawningObject;
var yAirplane = y as DrawningObject;
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
var xEntity = xAirplane.Airplane.Airplane;
var yEntity = yAirplane.Airplane.Airplane;
var colorWeight = xEntity.BodyColor.ToArgb().CompareTo(yEntity.BodyColor.ToArgb());
if (colorWeight != 0 || xEntity is not EntityAirBomber || yEntity is not EntityAirBomber)
{
return colorWeight;
}
return ((EntityAirBomber)xEntity).DopColor.ToArgb().CompareTo(((EntityAirBomber)yEntity).DopColor.ToArgb());
}
}
}

View File

@ -0,0 +1,37 @@
namespace AirBomber
{
internal class AirplaneCompareByType : IComparer<IDrawningObject>
{
public int Compare(IDrawningObject? x, IDrawningObject? y)
{
var xAirplane = x as DrawningObject;
var yAirplane = y as DrawningObject;
if (xAirplane == yAirplane)
{
return 0;
}
if (xAirplane == null)
{
return 1;
}
if (yAirplane == null)
{
return -1;
}
if (xAirplane.GetType().Name != yAirplane.Airplane.GetType().Name)
{
if (xAirplane.Airplane.GetType().Name == "DrawningAirplane")
{
return -1;
}
return 1;
}
var speedCompare = xAirplane.Airplane.Airplane.Speed.CompareTo(yAirplane.Airplane.Airplane.Speed);
if (speedCompare != 0)
{
return speedCompare;
}
return xAirplane.Airplane.Airplane.Weight.CompareTo(yAirplane.Airplane.Airplane.Weight);
}
}
}

View File

@ -8,36 +8,36 @@ namespace AirBomber
{
internal class DrawningObject : IDrawningObject
{
private DrawningAirplane _airplane = null;
public DrawningObject(DrawningAirplane airplane)
{
_airplane = airplane;
Airplane = airplane;
}
public float Step => _airplane?.Airplane?.Step ?? 0;
public float Step => Airplane?.Airplane?.Step ?? 0;
public DrawningAirplane Airplane { get; private set; }
public RectangleF GetCurrentPosition()
{
return _airplane.GetCurrentPosition();
return Airplane.GetCurrentPosition();
}
public void MoveObject(Direction direction)
{
_airplane?.MoveTransport(direction);
Airplane?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_airplane?.SetPosition(x, y, width, height);
Airplane?.SetPosition(x, y, width, height);
}
public void DrawObject(Graphics g)
{
_airplane?.DrawTransport(g);
Airplane?.DrawTransport(g);
}
public string GetInfo() => _airplane?.GetDataForSave();
public string GetInfo() => Airplane?.GetDataForSave() ?? string.Empty;
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
}

View File

@ -17,6 +17,8 @@ namespace AirBomber
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
var cmp = new AirplaneCompareByType();
cmp.Compare(null, null);
ApplicationConfiguration.Initialize();
var services = new ServiceCollection();
ConfigureServices(services);