diff --git a/AirBomber/AirBomber/AbstractStrategy.cs b/AirBomber/AirBomber/AbstractStrategy.cs new file mode 100644 index 0000000..6859bd6 --- /dev/null +++ b/AirBomber/AirBomber/AbstractStrategy.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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; + _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(DirectionType.Left); + protected bool MoveRight() => MoveTo(DirectionType.Right); + protected bool MoveUp() => MoveTo(DirectionType.Up); + protected bool MoveDown() => MoveTo(DirectionType.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(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} diff --git a/AirBomber/AirBomber/DrawningAirPlane.cs b/AirBomber/AirBomber/DrawningAirPlane.cs index 29c5d16..455d9f6 100644 --- a/AirBomber/AirBomber/DrawningAirPlane.cs +++ b/AirBomber/AirBomber/DrawningAirPlane.cs @@ -15,8 +15,7 @@ namespace AirBomber protected int _startPosY; protected readonly int _airPlaneWidth = 150; protected readonly int _airPlaneHeight = 118; - public DrawningAirPlane(int speed, double weight, Color bodyColor, int - width, int height) + public DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height) { if (width < _airPlaneWidth || height < _airPlaneHeight) { @@ -26,8 +25,7 @@ namespace AirBomber _pictureHeight = height; EntityAirPlane = new EntityAirPlane(speed, weight, bodyColor); } - protected DrawningAirPlane(int speed, double weight, Color bodyColor, int - width, int height, int airPlaneWidth, int airPlaneHeight) + protected DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height, int airPlaneWidth, int airPlaneHeight) { if (width < _airPlaneWidth || height < _airPlaneHeight) { diff --git a/AirBomber/AirBomber/DrawningObjectAirPlane.cs b/AirBomber/AirBomber/DrawningObjectAirPlane.cs new file mode 100644 index 0000000..90aa636 --- /dev/null +++ b/AirBomber/AirBomber/DrawningObjectAirPlane.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public class DrawningObjectAirPlane : IMoveableObject + { + private readonly DrawningAirPlane? _drawningAirPlane = null; + public DrawningObjectAirPlane(DrawningAirPlane drawningAirPlane) + { + _drawningAirPlane = drawningAirPlane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAirPlane == null || _drawningAirPlane.EntityAirPlane == null) + { + return null; + } + return new ObjectParameters(_drawningAirPlane.GetPosX, _drawningAirPlane.GetPosY, _drawningAirPlane.GetWidth, _drawningAirPlane.GetHeight); + } + } + public int GetStep => (int)(_drawningAirPlane?.EntityAirPlane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawningAirPlane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningAirPlane?.MovePlane(direction); + } +} diff --git a/AirBomber/AirBomber/EntityAirPlane.cs b/AirBomber/AirBomber/EntityAirPlane.cs index 643523a..a4b993e 100644 --- a/AirBomber/AirBomber/EntityAirPlane.cs +++ b/AirBomber/AirBomber/EntityAirPlane.cs @@ -6,7 +6,17 @@ using System.Threading.Tasks; namespace AirBomber { - internal class EntityAirPlane + public class EntityAirPlane { + 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 EntityAirPlane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } } } diff --git a/AirBomber/AirBomber/IMoveableObject.cs b/AirBomber/AirBomber/IMoveableObject.cs new file mode 100644 index 0000000..10bbf1b --- /dev/null +++ b/AirBomber/AirBomber/IMoveableObject.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public interface IMoveableObject + { + ObjectParameters? GetObjectPosition { get; } + int GetStep { get; } + bool CheckCanMove(DirectionType direction); + void MoveObject(DirectionType direction); + } +} diff --git a/AirBomber/AirBomber/ObjectParameters.cs b/AirBomber/AirBomber/ObjectParameters.cs new file mode 100644 index 0000000..b710e36 --- /dev/null +++ b/AirBomber/AirBomber/ObjectParameters.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + 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) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/AirBomber/AirBomber/Status.cs b/AirBomber/AirBomber/Status.cs new file mode 100644 index 0000000..3e0828d --- /dev/null +++ b/AirBomber/AirBomber/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}