From 18d5f5f4666731220fd3de51fa3bccbc090242c7 Mon Sep 17 00:00:00 2001 From: DavidMakarov Date: Sun, 15 Oct 2023 21:35:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirplaneWithRadar/AbstractStrategy.cs | 85 ++++++++++ .../AirplaneWithRadar/AirplaneEntity.cs | 23 +++ .../AirplaneWithRadar.csproj | 6 + .../AirplaneWithRadarEntity.cs | 23 +++ .../AirplanesGenericCollection.cs | 53 +++++++ AirplaneWithRadar/AirplaneWithRadar/Class1.cs | 18 --- .../AirplaneWithRadar/IMoveableObject.cs | 16 ++ .../AirplaneWithRadar/MoveToBorder.cs | 59 +++++++ .../AirplaneWithRadar/MoveToCenter.cs | 59 +++++++ .../AirplaneWithRadar/Movement.cs | 2 +- .../AirplaneWithRadar/ObjectParameters.cs | 35 ++++ .../{Paint.cs => PaintAirplane.cs} | 149 +++++++++--------- .../PaintAirplaneWithRadar.cs | 48 ++++++ .../AirplaneWithRadar/PaintObjectAirplane.cs | 37 +++++ .../AirplaneWithRadar/PlaneEntity.cs | 29 ---- .../AirplaneWithRadar/PlaneVisual.Designer.cs | 46 +++++- .../AirplaneWithRadar/PlaneVisual.cs | 61 +++++-- .../AirplaneWithRadar/Program.cs | 2 +- .../AirplaneWithRadar/SetGeneric.cs | 65 ++++++++ AirplaneWithRadar/AirplaneWithRadar/Status.cs | 15 ++ 20 files changed, 695 insertions(+), 136 deletions(-) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs delete mode 100644 AirplaneWithRadar/AirplaneWithRadar/Class1.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs rename AirplaneWithRadar/AirplaneWithRadar/{Paint.cs => PaintAirplane.cs} (58%) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs delete mode 100644 AirplaneWithRadar/AirplaneWithRadar/PlaneEntity.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/Status.cs diff --git a/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs new file mode 100644 index 0000000..cc4bbf8 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public abstract class AbstractStrategy + { + private IMoveableObject? moveableObject; + + private Status state = Status.NotInit; + + protected int FieldWidth { get; private set; } + + protected int FieldHeight { get; private set; } + + public Status GetStatus() { return state; } + + public void SetData(IMoveableObject moveableObject, int width, int + height) + { + if (moveableObject == null) + { + state = Status.NotInit; + return; + } + state = Status.InProgress; + this.moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected bool MoveLeft() => MoveTo(Movement.Left); + + protected bool MoveRight() => MoveTo(Movement.Right); + + protected bool MoveUp() => MoveTo(Movement.Up); + + protected bool MoveDown() => MoveTo(Movement.Down); + + protected ObjectParameters? GetObjectParameters => moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if (state != Status.InProgress) + { + return null; + } + return moveableObject?.GetStep; + } + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestinaion(); + + private bool MoveTo(Movement directionType) + { + if (state != Status.InProgress) + { + return false; + } + if (moveableObject?.CheckCanMove(directionType) ?? false) + { + moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs new file mode 100644 index 0000000..5446a8e --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneEntity.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.Entities +{ + public class AirplaneEntity + { + public int Speed { get; private set; } + public double Weight { get; private set; } + public Color BodyColor { get; private set; } + public double Step => (double)Speed * 100 / Weight; + + public AirplaneEntity(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadar.csproj b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadar.csproj index b57c89e..20cd17e 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadar.csproj +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadar.csproj @@ -8,4 +8,10 @@ enable + + + + + + \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs new file mode 100644 index 0000000..7b20c5b --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarEntity.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.Entities +{ + public class AirplaneWithRadarEntity : AirplaneEntity + { + public Color AdditColor { get; private set; } + public bool RadarOnBoard { get; private set; } + public bool AdditFuelPod { get; private set; } + + public AirplaneWithRadarEntity(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod) + : base (speed, weight, mainColor) + { + AdditColor = additColor; + RadarOnBoard = radarOnBoard; + AdditFuelPod = additFuelPod; + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs new file mode 100644 index 0000000..10d01c4 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplanesGenericCollection.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using AirplaneWithRadar.PaintObjects; +using AirplaneWithRadar.MovementStrategy; + +namespace AirplaneWithRadar.Generics; + +internal class AirplanesGenericCollection + where T : PaintAirplane + where U : IMoveableObject +{ + private readonly int pictWidth; + private readonly int pictHeight; + private readonly int placeSizeWidth = 210; + private readonly int placeSizeHeight = 90; + private readonly SetGeneric collection; + + public AirplanesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / placeSizeWidth; + int height = picHeight / placeSizeHeight; + pictWidth = picWidth; + pictHeight = picHeight; + collection = new SetGeneric(width * height); + + } + + public static bool operator +(AirplanesGenericCollection collect, T? +obj) + { + if (obj == null) + { + return false; + } + return collect?.collection.Insert(obj) ?? false; + } + public static T? operator -(AirplanesGenericCollection collect, int + pos) + { + T? obj = collect.collection.Get(pos); + if (obj != null) + { + collect.collection.Remove(pos); + } + return obj; + } + + +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/Class1.cs b/AirplaneWithRadar/AirplaneWithRadar/Class1.cs deleted file mode 100644 index 275298d..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/Class1.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plane -{ - public class Class1 - { - private int x; - public int y { private get; set; } - public int Sum(int num1, int num2) - { - return num1 + num2; - } - } -} diff --git a/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs new file mode 100644 index 0000000..7a91c94 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(Movement direction); + void MoveObject(Movement direction); + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs new file mode 100644 index 0000000..bb71a67 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + var diffY = objParams.DownBorder - FieldHeight; + + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs new file mode 100644 index 0000000..f220cfb --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs @@ -0,0 +1,59 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) { + return false; + } + return (objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2); + } + + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/Movement.cs b/AirplaneWithRadar/AirplaneWithRadar/Movement.cs index 06c3509..797b628 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/Movement.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/Movement.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Plane +namespace AirplaneWithRadar { public enum Movement { diff --git a/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs new file mode 100644 index 0000000..cf4c3de --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public class ObjectParameters + { + private readonly int x; + private readonly int y; + private readonly int width; + private readonly int height; + public int LeftBorder => x; + + public int TopBorder => y; + + public int RightBorder => x + width; + + public int DownBorder => y + height; + + public int ObjectMiddleHorizontal => x + width / 2; + + public int ObjectMiddleVertical => y + height / 2; + public ObjectParameters(int x, int y, int width, int height) + { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/Paint.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs similarity index 58% rename from AirplaneWithRadar/AirplaneWithRadar/Paint.cs rename to AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs index 407118c..e2be915 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/Paint.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplane.cs @@ -2,111 +2,123 @@ using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Linq; -using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using AirplaneWithRadar.Entities; -namespace Plane +namespace AirplaneWithRadar.PaintObjects { - public class Paint + public class PaintAirplane { - public PlaneEntity? PlaneEntity { get; private set; } + public AirplaneEntity? AirplaneEntity { get; protected set; } private int pictWidth; private int pictHeight; - private int startPosX; - private int startPosY; - private readonly int planeWidth = 140; - private readonly int planeHeight = 60; + protected int startPosX; + protected int startPosY; + protected readonly int planeWidth = 140; + protected readonly int planeHeight = 60; - public bool Init(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod, int width, int height) + public int GetPosX => startPosX; + public int GetPosY => startPosY; + public int GetWidth => planeWidth; + public int GetHeight => planeHeight; + + public bool CanMove(Movement dir) { - if (planeWidth > width || planeHeight > height) { return false; } + if (AirplaneEntity == null) + { + return false; + } + return dir switch + { + //влево + Movement.Left => startPosX - AirplaneEntity.Step > 0, + //вверх + Movement.Up => startPosY - AirplaneEntity.Step > 0, + // вправо + Movement.Right => startPosX + planeWidth + AirplaneEntity.Step < pictWidth, + //вниз + Movement.Down => startPosY + planeHeight + AirplaneEntity.Step < pictHeight, + _ => false, + }; + + } + + public PaintAirplane(int speed, double weight, Color bodyColor, int width, int height) + { + if (planeWidth > width || planeHeight > height) { return; } pictHeight = height; pictWidth = width; - PlaneEntity = new PlaneEntity(); - PlaneEntity.Init(speed, weight, mainColor, additColor, radarOnBoard, additFuelPod); + AirplaneEntity = new AirplaneEntity(speed, weight, bodyColor); + } - return true; + protected PaintAirplane(int speed, double weight, Color bodyColor, int width, int height, int planeWidth, int planeHeight) + { + if (this.planeWidth > width || this.planeHeight > height) { return; } + + pictHeight = height; + pictWidth = width; + this.planeWidth = planeWidth; + this.planeHeight = planeHeight; + AirplaneEntity = new AirplaneEntity(speed, weight, bodyColor); } public void SetPosition(int x, int y) { - if (x + planeWidth < pictWidth && y + planeHeight < pictHeight) - { + if ((x >= 0 && y >= 0)&&(x + planeHeight <= pictHeight || y + planeWidth <= pictWidth)) { startPosX = x; startPosY = y; } - else - { - if (x > pictWidth) - { - while (x + planeWidth > pictWidth) - { - x -= 1; - } - } - if (y > pictWidth) - { - while (y + planeHeight > pictHeight) - { - y -= 1; - } - } - } } public void Move(Movement dir) { - if (PlaneEntity == null) + if (!CanMove(dir) || AirplaneEntity == null) { return; } switch (dir) { + //влево case Movement.Left: - if (startPosX - PlaneEntity.Step > 0) - { - startPosX -= (int) PlaneEntity.Step; - } + startPosX -= (int)AirplaneEntity.Step; break; + //вверх case Movement.Up: - if (startPosY - PlaneEntity.Step > 0) - { - startPosY -= (int) PlaneEntity.Step; - } + startPosY -= (int)AirplaneEntity.Step; break; + // вправо case Movement.Right: - if (startPosX + planeWidth + PlaneEntity.Step < pictWidth) - { - startPosX += (int)PlaneEntity.Step; - } + startPosX += (int)AirplaneEntity.Step; break; + //вниз case Movement.Down: - if (startPosY + planeHeight + PlaneEntity.Step < pictHeight) - { - startPosY += (int)PlaneEntity.Step; - } + startPosY += (int)AirplaneEntity.Step; break; } + } - public void DrawTransport(Graphics g) + + public virtual void DrawTransport(Graphics g) { - if (PlaneEntity == null) + if (AirplaneEntity == null) { return; } Pen pen = new(Color.Black); - Brush mainBrush = new SolidBrush(PlaneEntity.MainColor); - Brush additBrush = new SolidBrush(PlaneEntity.AdditColor); + Brush brush = new SolidBrush(AirplaneEntity.BodyColor); //основа - + g.FillRectangle(brush, startPosX + planeWidth / 44, startPosY + planeHeight / 2, planeWidth - 2* planeWidth/11, planeHeight / 2 - planeHeight / 8); + g.FillEllipse(brush, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8)); g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight / 2), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2)); g.DrawLine(pen, new Point(startPosX + planeWidth / 44, startPosY + planeHeight - planeHeight / 8), new Point(startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight - planeHeight / 8)); + + g.DrawArc(pen, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight / 2 - planeHeight / 8), 90.0f, 180.0f); + - g.DrawArc(pen, new Rectangle(startPosX, startPosY + planeHeight / 2, planeWidth / 24, planeHeight/2 - planeHeight / 8), 90.0f, 180.0f); DrawTriangle(g, pen, startPosX + planeWidth - planeWidth / 6, startPosY + planeHeight / 2, // 1 @@ -115,9 +127,9 @@ namespace Plane DrawTriangle(g, pen, startPosX, startPosY, // 1 - startPosX + planeWidth/4, startPosY + planeHeight/2, // 2 + startPosX + planeWidth / 4, startPosY + planeHeight / 2, // 2 startPosX, startPosY + planeHeight / 2); // 3 - g.DrawLine(pen, new Point(startPosX + planeWidth - planeWidth / 6, startPosY + 5 * planeHeight / 7), + g.DrawLine(pen, new Point(startPosX + planeWidth - planeWidth / 6, startPosY + 5 * planeHeight / 7), new Point(startPosX + planeWidth, startPosY + 5 * planeHeight / 7)); // колеса g.DrawLine(pen, new Point(startPosX + planeWidth / 4, startPosY + planeHeight - planeHeight / 8), @@ -132,20 +144,12 @@ namespace Plane g.DrawEllipse(pen, new Rectangle(startPosX + planeWidth - 5 * planeWidth / 21, startPosY + 11 * planeHeight / 12, planeHeight / 12, planeHeight / 12)); - FillRoundedRectangle(g,new SolidBrush(Color.Black), new Rectangle(startPosX + 2*planeWidth / 9, startPosY + planeHeight / 2 + planeHeight / 7, planeWidth/2 ,planeHeight/7), 5); - FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX, startPosY + planeHeight / 2 - planeHeight/9, planeWidth / 5, planeHeight / 6), 5); - - if (PlaneEntity.RadarOnBoard) { - g.DrawLine(pen, new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 2), new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 3)); - g.FillPie(additBrush, new Rectangle(startPosX + planeWidth / 4, startPosY + planeHeight / 10, planeWidth / 6, planeHeight / 4), 30.0f, 180.0f); - } - if (PlaneEntity.AdditFuelPod) - { - FillRoundedRectangle(g, additBrush, new Rectangle(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6), 5); - } + FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX + 2 * planeWidth / 9, startPosY + planeHeight / 2 + planeHeight / 7, planeWidth / 2, planeHeight / 7), 5); + FillRoundedRectangle(g, new SolidBrush(Color.Black), new Rectangle(startPosX, startPosY + planeHeight / 2 - planeHeight / 9, planeWidth / 5, planeHeight / 6), 5); + } - private void DrawTriangle(Graphics g, Pen pen, int x1, int y1, int x2, int y2, int x3, int y3) + protected void DrawTriangle(Graphics g, Pen pen, int x1, int y1, int x2, int y2, int x3, int y3) { Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); @@ -155,7 +159,7 @@ namespace Plane g.DrawLine(pen, p3, p1); } - private static GraphicsPath RoundedRect(Rectangle bounds, int radius) + protected static GraphicsPath RoundedRect(Rectangle bounds, int radius) { int diameter = radius * 2; Size size = new Size(diameter, diameter); @@ -187,7 +191,7 @@ namespace Plane return path; } - private static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) + protected static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) { if (graphics == null) throw new ArgumentNullException(nameof(graphics)); @@ -200,7 +204,7 @@ namespace Plane } } - private static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) + protected static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) { if (graphics == null) throw new ArgumentNullException(nameof(graphics)); @@ -212,6 +216,5 @@ namespace Plane graphics.FillPath(brush, path); } } - } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs new file mode 100644 index 0000000..6bb7691 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintAirplaneWithRadar.cs @@ -0,0 +1,48 @@ + +using System.Collections.Generic; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +using AirplaneWithRadar.Entities; + +namespace AirplaneWithRadar.PaintObjects +{ + public class PaintAirplaneWithRadar : PaintAirplane + { + public AirplaneWithRadarEntity? PlaneEntity { get; private set; } + + public PaintAirplaneWithRadar(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod, int width, int height) + : base(speed, weight, mainColor, width, height, 140, 60) + { + if (AirplaneEntity != null) + { + AirplaneEntity = new AirplaneWithRadarEntity(speed, weight, mainColor, additColor, radarOnBoard, additFuelPod); + } + } + + public override void DrawTransport(Graphics g) + { + if (AirplaneEntity is not AirplaneWithRadarEntity AirplaneWithRadar) + { + return; + } + Pen pen = new(Color.Black); + Brush additBrush = new SolidBrush(AirplaneWithRadar.AdditColor); + base.DrawTransport(g); + if (AirplaneWithRadar.RadarOnBoard) { + g.DrawLine(pen, new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 2), new Point(startPosX + planeWidth / 3, startPosY + planeHeight / 3)); + g.FillPie(additBrush, new Rectangle(startPosX + planeWidth / 4, startPosY + planeHeight / 10, planeWidth / 6, planeHeight / 4), 30.0f, 180.0f); + } + if (AirplaneWithRadar.AdditFuelPod) + { + FillRoundedRectangle(g, additBrush, new Rectangle(startPosX, startPosY + planeHeight - 2*planeHeight / 5, planeWidth / 5, planeHeight / 6), 5); + } + + } + + + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs new file mode 100644 index 0000000..82666cf --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/PaintObjectAirplane.cs @@ -0,0 +1,37 @@ +using AirplaneWithRadar.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar.PaintObjects; + + +namespace AirplaneWithRadar.MovementStrategy +{ + public class PaintObjectAirplane : IMoveableObject + { + private readonly PaintAirplane? paintAirplane = null; + + public PaintObjectAirplane(PaintAirplane paintAirplane) + { + this.paintAirplane = paintAirplane; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (paintAirplane == null || paintAirplane.AirplaneEntity == null) { return null; } + return new ObjectParameters(paintAirplane.GetPosX, paintAirplane.GetPosY, paintAirplane.GetWidth, paintAirplane.GetHeight); + } + } + + public int GetStep => (int)(paintAirplane?.AirplaneEntity?.Step ?? 0); + public bool CheckCanMove(Movement dir) => + paintAirplane?.CanMove(dir) ?? false; + public void MoveObject(Movement dir) => + paintAirplane?.Move(dir); + + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/PlaneEntity.cs b/AirplaneWithRadar/AirplaneWithRadar/PlaneEntity.cs deleted file mode 100644 index 5aacc88..0000000 --- a/AirplaneWithRadar/AirplaneWithRadar/PlaneEntity.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plane -{ - public class PlaneEntity - { - public int Speed { get; private set; } - public double Weight { get; private set; } - public Color MainColor { get; private set; } - public Color AdditColor { get; private set; } - public bool RadarOnBoard { get; private set; } - public bool AdditFuelPod { get; private set; } - public double Step => (double)Speed * 100 / Weight; - - public void Init(int speed, double weight, Color mainColor, Color additColor, bool radarOnBoard, bool additFuelPod) - { - Speed = speed; - Weight = weight; - MainColor = mainColor; - AdditColor = additColor; - RadarOnBoard = radarOnBoard; - AdditFuelPod = additFuelPod; - } - } -} diff --git a/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.Designer.cs index 77d73cb..8309e97 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.Designer.cs @@ -1,4 +1,4 @@ -namespace Plane +namespace AirplaneWithRadar { partial class PlaneVisual { @@ -34,6 +34,9 @@ DownButton = new Button(); RightButton = new Button(); PlanesPictureBox = new PictureBox(); + StrategyComboBox = new ComboBox(); + StepButton = new Button(); + CreateAirplaneButton = new Button(); ((System.ComponentModel.ISupportInitialize)PlanesPictureBox).BeginInit(); SuspendLayout(); // @@ -43,7 +46,7 @@ CreateButton.Name = "CreateButton"; CreateButton.Size = new Size(120, 40); CreateButton.TabIndex = 0; - CreateButton.Text = "Создать"; + CreateButton.Text = "Создать самолёт с радаром"; CreateButton.UseVisualStyleBackColor = true; CreateButton.Click += CreateButton_Click; // @@ -103,11 +106,44 @@ PlanesPictureBox.TabIndex = 5; PlanesPictureBox.TabStop = false; // + // StrategyComboBox + // + StrategyComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + StrategyComboBox.FormattingEnabled = true; + StrategyComboBox.Items.AddRange(new object[] { "Центр", "Граница" }); + StrategyComboBox.Location = new Point(667, 12); + StrategyComboBox.Name = "StrategyComboBox"; + StrategyComboBox.Size = new Size(121, 23); + StrategyComboBox.TabIndex = 6; + // + // StepButton + // + StepButton.Location = new Point(713, 41); + StepButton.Name = "StepButton"; + StepButton.Size = new Size(75, 23); + StepButton.TabIndex = 7; + StepButton.Text = "Шаг"; + StepButton.UseVisualStyleBackColor = true; + StepButton.Click += StepButton_Click; + // + // CreateAirplaneButton + // + CreateAirplaneButton.Location = new Point(156, 391); + CreateAirplaneButton.Name = "CreateAirplaneButton"; + CreateAirplaneButton.Size = new Size(120, 40); + CreateAirplaneButton.TabIndex = 8; + CreateAirplaneButton.Text = "Создать самолёт"; + CreateAirplaneButton.UseVisualStyleBackColor = true; + CreateAirplaneButton.Click += CreateAirplaneButton_Click; + // // PlaneVisual // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(CreateAirplaneButton); + Controls.Add(StepButton); + Controls.Add(StrategyComboBox); Controls.Add(RightButton); Controls.Add(DownButton); Controls.Add(LeftButton); @@ -115,8 +151,7 @@ Controls.Add(CreateButton); Controls.Add(PlanesPictureBox); Name = "PlaneVisual"; - Text = "Flying Plane"; - Load += Form1_Load; + Text = "Самолёт"; ((System.ComponentModel.ISupportInitialize)PlanesPictureBox).EndInit(); ResumeLayout(false); } @@ -129,5 +164,8 @@ private Button DownButton; private Button RightButton; private PictureBox PlanesPictureBox; + private ComboBox StrategyComboBox; + private Button StepButton; + private Button CreateAirplaneButton; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.cs b/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.cs index 1661927..e635d5f 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/PlaneVisual.cs @@ -1,10 +1,13 @@ using System; +using AirplaneWithRadar.MovementStrategy; +using AirplaneWithRadar.PaintObjects; -namespace Plane +namespace AirplaneWithRadar { public partial class PlaneVisual : System.Windows.Forms.Form { - private Paint? PaintPlanes; + private PaintAirplane? PaintPlanes; + private AbstractStrategy? abstractStrategy; private void Draw() { @@ -23,13 +26,9 @@ namespace Plane } - private void Form1_Load(object sender, EventArgs e) - { - } - private void buttonMove_Click(object sender, EventArgs e) { - if (PaintPlanes == null) { return; } + if (PaintPlanes == null) { return; } string name = ((Button)sender)?.Name ?? string.Empty; switch (name) @@ -55,12 +54,54 @@ namespace Plane { Random random = new Random(); - PaintPlanes = new Paint(); - PaintPlanes.Init(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), + PaintPlanes = new PaintAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), true, Convert.ToBoolean(random.Next(0, 2)), PlanesPictureBox.Width, PlanesPictureBox.Height); PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + + private void CreateAirplaneButton_Click(object sender, EventArgs e) + { + Random random = new Random(); + PaintPlanes = new PaintAirplane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + PlanesPictureBox.Width, PlanesPictureBox.Height); + PaintPlanes.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void StepButton_Click(object sender, EventArgs e) + { + if (PaintPlanes == null) + { + return; + } + if (StrategyComboBox.Enabled) + { + abstractStrategy = StrategyComboBox.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null + }; + if (abstractStrategy == null) + { + return; + } + abstractStrategy.SetData(new PaintObjectAirplane(PaintPlanes), PlanesPictureBox.Width, PlanesPictureBox.Height); + StrategyComboBox.Enabled = false; + } + if (abstractStrategy == null) + { + return; + } + abstractStrategy.MakeStep(); + Draw(); + if (abstractStrategy.GetStatus() == Status.Finish) + { + StrategyComboBox.Enabled = true; + abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/Program.cs b/AirplaneWithRadar/AirplaneWithRadar/Program.cs index 622c7d7..bb8cbd8 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/Program.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/Program.cs @@ -1,4 +1,4 @@ -namespace Plane +namespace AirplaneWithRadar { internal static class Program { diff --git a/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs new file mode 100644 index 0000000..fa4cead --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/SetGeneric.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.Generics; + +internal class SetGeneric + where T : class +{ + private readonly T?[] places; + public int Count => places.Length; + public SetGeneric(int count) + { + places = new T?[count]; + } + + public bool Insert(T airplane) + { + if (places[0] == null) + { + places[0] = airplane; + } + else + { + for (int i = Count - 1; i < 0; i--) + { + places[i] = places[i - 1]; + } + places[0] = airplane; + } + return true; + } + public bool Insert(T airplane, int position) + { + if (position > Count || position < 0) + { + return false; + } + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + places[position] = airplane; + return true; + } + public bool Remove(int position) + { + if (position > Count || position < 0) + { + return false; + } + places[position] = null; + return true; + } + public T? Get(int position) + { + if (position > Count || position < 0) + { + return null; + } + return places[position]; + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/Status.cs b/AirplaneWithRadar/AirplaneWithRadar/Status.cs new file mode 100644 index 0000000..98eb831 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}