кнопки зло

This commit is contained in:
Extrimal 2023-10-17 23:50:10 +03:00
parent d24c649c28
commit 321144f895
11 changed files with 474 additions and 131 deletions

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.DrawningObjects;
namespace AircraftCarrier.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; }
/// Установка данных
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
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();
}
/// Перемещение влево
/// <returns>Результат перемещения (true - удалось переместиться, false -неудача)</returns>
protected bool MoveLeft() => MoveTo(Direction.Left);
/// Перемещение вправо
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveRight() => MoveTo(Direction.Right);
/// Перемещение вверх
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveUp() => MoveTo(Direction.Up);
/// Перемещение вниз
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveDown() => MoveTo(Direction.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();
/// Попытка перемещения в требуемом направлении
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.Entities;
namespace AircraftCarrier.DrawningObjects
{
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningAircraft
{
/// Класс-сущность
public EntityAircraft? EntityAircraft { get; protected set; }
/// Ширина окна
private int _pictureWidth;
/// Высота окна
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _AircraftWidth = 164;
protected readonly int _AircraftHeight = 40;
/// Координата X объекта
public int GetPosX => _startPosX;
/// Координата Y объекта
public int GetPosY => _startPosY;
/// Ширина объекта
public int GetWidth => _AircraftWidth;
/// Высота объекта
public int GetHeight => _AircraftHeight;
/// Проверка, что объект может переместится по указанному направлению
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(Direction direction)
{
if (EntityAircraft == null)
{
return false;
}
return direction switch
{
//влево
Direction.Left => _startPosX - EntityAircraft.Step > 0,
//вверх
Direction.Up => _startPosY - EntityAircraft.Step > 0,
// вправо
Direction.Right => _startPosX + EntityAircraft.Step + _AircraftWidth < _pictureWidth,
//вниз
Direction.Down => _startPosY + EntityAircraft.Step + _AircraftHeight < _pictureHeight,
_ => false
};
}
/// Изменение направления перемещения
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityAircraft == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
_startPosX -= (int)EntityAircraft.Step;
break;
//вверх
case Direction.Up:
_startPosY -= (int)EntityAircraft.Step;
break;
// вправо
case Direction.Right:
_startPosX += (int)EntityAircraft.Step;
break;
//вниз
case Direction.Down:
_startPosY += (int)EntityAircraft.Step;
break;
}
}
/// Конструктор
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawningAircraft(int speed, double weight, Color bodyColor, int
width, int height)
{
if (width < _AircraftWidth || height < _AircraftHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityAircraft = new EntityAircraft(speed, weight, bodyColor);
}
/// Конструктор
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="carWidth">Ширина прорисовки автомобиля</param>
/// <param name="carHeight">Высота прорисовки автомобиля</param>
protected DrawningAircraft(int speed, double weight, Color bodyColor,int
width, int height, int AircraftWidth, int AircraftHeight)
{
if (width < _AircraftWidth || height < _AircraftHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_AircraftWidth = AircraftWidth;
_AircraftHeight = AircraftHeight;
EntityAircraft = new EntityAircraft(speed, weight, bodyColor);
}
/// Установка позиции
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftWidth) || _startPosY > (_pictureHeight - _AircraftHeight))
{
_startPosX = 50;
_startPosY = 50;
}
}
/// Изменение направления перемещения
/// Прорисовка объекта
public virtual void DrawTransport(Graphics g)
{
if (EntityAircraft == null)
{
return;
}
Pen pen = new(Color.Black);
Brush budyBrush = new SolidBrush(EntityAircraft.BodyColor);
g.DrawLine(pen, _startPosX + 4, _startPosY, _startPosX + 124, _startPosY);
g.DrawLine(pen, _startPosX + 124, _startPosY, _startPosX + 164, _startPosY + 20);
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
g.DrawLine(pen, _startPosX + 4, _startPosY + 40, _startPosX + 4, _startPosY);
g.FillRectangle(budyBrush, _startPosX, _startPosY + 6, 4, 12);
g.FillRectangle(budyBrush, _startPosX, _startPosY + 24, 4, 12);
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14);
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22);
}
}
}

View File

@ -3,108 +3,37 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AircraftCarrier using AircraftCarrier.Entities;
namespace AircraftCarrier.DrawningObjects
{ {
internal class DrawningAircraftCarrier public class DrawningAircraftCarrier : DrawningAircraft
{ {
public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; } public DrawningAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway, int width, int height) :
private int _pictureWidth; base(speed, weight, bodyColor, width, height, 164, 40)
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _AircraftCarrierWidth = 164;
private readonly int _AircraftCarrierHeight = 40;
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height)
{ {
_pictureWidth = width; if (EntityAircraft != null)
_pictureHeight = height;
if(_AircraftCarrierWidth <_pictureWidth || _AircraftCarrierHeight <_pictureHeight)
{ {
EntityAircraftCarrier = new EntityAircraftCarrier(); EntityAircraft = new EntityAircraftCarrier(speed, weight, bodyColor,
EntityAircraftCarrier.Init(speed, weight, bodyColor, additionalColor, additionalColor, cabin, runway);
bodyKit, wing, sportLine);
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftCarrierWidth) || _startPosY>(_pictureHeight - _AircraftCarrierHeight))
{
_startPosX = 50;
_startPosY = 50;
} }
} }
public void MoveTransport(Direction direction) public override void DrawTransport(Graphics g)
{ {
if (EntityAircraftCarrier == null) if (EntityAircraft is not EntityAircraftCarrier aircraftCarrie)
{ {
return; return;
} }
switch (direction) Brush additionalBrush = new SolidBrush(aircraftCarrie.AdditionalColor);
{ Pen pen = new(Color.White);
//влево if (aircraftCarrie.Cabin)
case Direction.Left:
if (_startPosX - EntityAircraftCarrier.Step > 0)
{
_startPosX -= (int)EntityAircraftCarrier.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityAircraftCarrier.Step > 0)
{
_startPosY -= (int)EntityAircraftCarrier.Step;
}
break;
// вправо
case Direction.Right:
if (_startPosX + EntityAircraftCarrier.Step + _AircraftCarrierWidth < _pictureWidth)
{
_startPosX += (int)EntityAircraftCarrier.Step;
}
break;
//вниз
case Direction.Down:
if(_startPosY + EntityAircraftCarrier.Step + _AircraftCarrierHeight < _pictureHeight)
{
_startPosY += (int)EntityAircraftCarrier.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (EntityAircraftCarrier == null)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityAircraftCarrier.AdditionalColor);
g.DrawLine(pen, _startPosX+4, _startPosY, _startPosX + 124, _startPosY);
g.DrawLine(pen, _startPosX+124, _startPosY, _startPosX + 164, _startPosY+20);
g.DrawLine(pen, _startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40);
g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40);
g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40);
g.DrawLine(pen, _startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY);
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 6, 4, 12);
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 24, 4, 12);
g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14);
g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22);
Pen penWhite = new(Color.White);
if (EntityAircraftCarrier.BodyKit)
{ {
g.FillRectangle(additionalBrush, _startPosX + 4, _startPosY + 13, 59, 14); g.FillRectangle(additionalBrush, _startPosX + 4, _startPosY + 13, 59, 14);
g.DrawLine(penWhite, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19); g.DrawLine(pen, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19);
g.DrawLine(penWhite, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19); g.DrawLine(pen, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19);
g.DrawLine(penWhite, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19); g.DrawLine(pen, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19);
g.DrawLine(penWhite, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19); g.DrawLine(pen, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19);
} }
if (EntityAircraftCarrier.Wing) if (aircraftCarrie.Runway)
{ {
g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9); g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9);
} }

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.DrawningObjects;
namespace AircraftCarrier.MovementStrategy
{
public class DrawningObjectAircraft : IMoveableObject
{
private readonly DrawningAircraft? _drawningAircraft = null;
public DrawningObjectAircraft(DrawningAircraft drawningAircraft)
{
_drawningAircraft = drawningAircraft;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningAircraft == null || _drawningAircraft.EntityAircraft ==
null)
{
return null;
}
return new ObjectParameters(_drawningAircraft.GetPosX,
_drawningAircraft.GetPosY, _drawningAircraft.GetWidth, _drawningAircraft.GetHeight);
}
}
public int GetStep => (int)(_drawningAircraft?.EntityAircraft?.Step ?? 0);
public bool CheckCanMove(Direction direction) =>
_drawningAircraft?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) =>
_drawningAircraft?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier.Entities
{
/// Класс-сущность "Автомобиль"
public class EntityAircraft
{
/// Скорость
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;
/// Конструктор с параметрами
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityAircraft(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -3,53 +3,23 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AircraftCarrier.Entities
namespace AircraftCarrier
{ {
public class EntityAircraftCarrier /// Класс-сущность "Спортивный автомобиль"
public class EntityAircraftCarrier : EntityAircraft
{ {
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов) /// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; } public Color AdditionalColor { get; private set; }
/// <summary> /// Признак (опция) наличия кабины
/// Признак (опция) наличия обвеса public bool Cabin { get; private set; }
/// </summary> /// Признак (опция) наличия взлетной полосы
public bool BodyKit { get; private set; } public bool Runway { get; private set; }
/// <summary>
/// Признак (опция) наличия антикрыла
/// </summary>
public bool Wing { get; private set; }
/// <summary>
/// Признак (опция) наличия гоночной полосы
/// </summary>
public bool SportLine { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight; public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway) : base(speed, weight, bodyColor)
additionalColor, bool bodyKit, bool wing, bool sportLine)
{ {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
BodyKit = bodyKit; Cabin = cabin;
Wing = wing; Runway = runway;
SportLine = sportLine;
} }
} }
} }

View File

@ -0,0 +1,22 @@
using AircraftCarrier.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.DrawningObjects;
namespace AircraftCarrier.MovementStrategy
{
public interface IMoveableObject
{
/// Получение координаты X объекта
ObjectParameters? GetObjectPosition { get; }
/// Шаг объекта
int GetStep { get; }
/// Проверка, можно ли переместиться по нужному направлению
bool CheckCanMove(Direction direction);
/// Изменение направления пермещения объекта
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier.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();
}
}
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier.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;
/// Конструктор
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -2,9 +2,7 @@ namespace AircraftCarrier
{ {
internal static class Program internal static class Program
{ {
/// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary>
[STAThread] [STAThread]
static void Main() static void Main()
{ {

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}