diff --git a/AirBomber/AirBomber/AirplaneCompareByColor.cs b/AirBomber/AirBomber/AirplaneCompareByColor.cs new file mode 100644 index 0000000..28e2879 --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByColor.cs @@ -0,0 +1,31 @@ +namespace AirBomber +{ + internal class AirplaneCompareByColor : IComparer + { + 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()); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/AirplaneCompareByType.cs b/AirBomber/AirBomber/AirplaneCompareByType.cs new file mode 100644 index 0000000..464b8f6 --- /dev/null +++ b/AirBomber/AirBomber/AirplaneCompareByType.cs @@ -0,0 +1,37 @@ +namespace AirBomber +{ + internal class AirplaneCompareByType : IComparer + { + 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); + } + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/DrawningObject.cs b/AirBomber/AirBomber/DrawningObject.cs index 4bb3594..487ccef 100644 --- a/AirBomber/AirBomber/DrawningObject.cs +++ b/AirBomber/AirBomber/DrawningObject.cs @@ -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()); } diff --git a/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index f0821c2..4485161 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -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);