до кнопок

This commit is contained in:
Nastya_Kozlova 2023-10-18 00:52:22 +04:00
parent 2a4c112aa7
commit fbd45d6ab1
10 changed files with 596 additions and 180 deletions

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
namespace AirFighter.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;
_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;
}
}
}

View File

@ -1,178 +1,38 @@
using System;
using AirFighter.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Entities;
namespace AirFighter.DrawningObjects
namespace AirFighter
{
internal class DrawningAirFighter
public class DrawningAirFighter : DrawningAirplane
{
public EntityAirFighter? EntityAirFighter { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _airFigherWidth = 90;
private readonly int _airFigherHeight = 95;
public bool Init(int speed, double weight, Color bodyColor, Color
public DrawningAirFighter(int speed, double weight, Color bodyColor, Color
additionalColor, bool rockets, bool wings, int width, int height)
: base(speed, weight, bodyColor, width, height, 90, 95)
{
if (_airFigherWidth < width || _airFigherHeight < height)
if (EntityAirplane != null)
{
EntityAirFighter = new EntityAirFighter();
EntityAirFighter.Init(speed, weight, bodyColor, additionalColor,
rockets, wings);
_pictureWidth = width;
_pictureHeight = height;
return true;
}
return false;
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _airFigherWidth) || _startPosY > (_pictureHeight - _airFigherHeight))
{
_startPosX = 50;
_startPosY = 50;
EntityAirplane = new EntityAirFighter(speed, weight, bodyColor,
additionalColor, rockets, wings);
}
}
public void MoveTransport(DirectionType direction)
public override void DrawTransport(Graphics g)
{
if (EntityAirFighter == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityAirFighter.Step > 0)
{
_startPosX -= (int)EntityAirFighter.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityAirFighter.Step > 0)
{
_startPosY -= (int)EntityAirFighter.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityAirFighter.Step < _pictureWidth - _airFigherWidth)
{
_startPosX += (int)EntityAirFighter.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityAirFighter.Step < _pictureHeight - _airFigherHeight)
{
_startPosY += (int)EntityAirFighter.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (EntityAirFighter == null)
if (EntityAirplane is not EntityAirFighter airFighter)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityAirFighter.BodyColor);
Brush additionalBrush = new SolidBrush(EntityAirFighter.AdditionalColor);
//тело истребителя
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 40, 80, 15);
//нос
Point nose1 = new Point(_startPosX, _startPosY + 48);
Point nose2 = new Point(_startPosX + 10, _startPosY + 48 - 8);
Point nose3 = new Point(_startPosX + 10, _startPosY + 48 + 8);
Point[] nosePoints =
{
nose1,
nose2,
nose3
};
g.FillPolygon(additionalBrush, nosePoints);
//передние крылья
Point wingUp1 = new Point(_startPosX + 30, _startPosY + 40);
Point wingUp2 = new Point(_startPosX + 30, _startPosY);
Point wingUp3 = new Point(_startPosX + 40, _startPosY);
Point wingUp4 = new Point(_startPosX + 45, _startPosY + 40);
Point[] wingUpPoints =
{
wingUp1,
wingUp2,
wingUp3,
wingUp4
};
g.DrawPolygon(pen, wingUpPoints);
Point wingDown1 = new Point(_startPosX + 30, _startPosY + 55);
Point wingDown2 = new Point(_startPosX + 30, _startPosY + 95);
Point wingDown3 = new Point(_startPosX + 40, _startPosY + 95);
Point wingDown4 = new Point(_startPosX + 45, _startPosY + 55);
Point[] wingDownPoints =
{
wingDown1,
wingDown2,
wingDown3,
wingDown4
};
g.DrawPolygon(pen, wingDownPoints);
//хвост
Point tailUp1 = new Point(_startPosX + 75, _startPosY + 40);
Point tailUp2 = new Point(_startPosX + 75, _startPosY + 30);
Point tailUp3 = new Point(_startPosX + 90, _startPosY + 15);
Point tailUp4 = new Point(_startPosX + 90, _startPosY + 40);
Point[] tailUpPoints =
{
tailUp1,
tailUp2,
tailUp3,
tailUp4,
};
g.DrawPolygon(pen, tailUpPoints);
Point tailDown1 = new Point(_startPosX + 75, _startPosY + 55);
Point tailDown2 = new Point(_startPosX + 75, _startPosY + 65);
Point tailDown3 = new Point(_startPosX + 90, _startPosY + 80);
Point tailDown4 = new Point(_startPosX + 90, _startPosY + 55);
Point[] tailDownPoints =
{
tailDown1,
tailDown2,
tailDown3,
tailDown4,
};
g.DrawPolygon(pen, tailDownPoints);
Brush additionalBrush = new SolidBrush(airFighter.AdditionalColor);
//ракеты
if (EntityAirFighter.Rockets)
if (airFighter.Rockets)
{
Point rocketUp1 = new Point(_startPosX + 50, _startPosY + 15);
Point rocketUp2 = new Point(_startPosX + 50, _startPosY + 5);
@ -210,7 +70,7 @@ namespace AirFighter
}
//дополнительные крылья
if (EntityAirFighter.Wings)
if (airFighter.Wings)
{
Point miniWingUp1 = new Point(_startPosX + 50, _startPosY + 40);
Point miniWingUp2 = new Point(_startPosX + 50, _startPosY + 20);
@ -224,7 +84,7 @@ namespace AirFighter
miniWingUp3,
miniWingUp4
};
g.FillPolygon(bodyBrush, miniWingUpPoints);
g.FillPolygon(additionalBrush, miniWingUpPoints);
Point miniWingDown1 = new Point(_startPosX + 50, _startPosY + 55);
Point miniWingDown2 = new Point(_startPosX + 50, _startPosY + 75);
@ -238,7 +98,7 @@ namespace AirFighter
miniWingDown3,
miniWingDown4
};
g.FillPolygon(bodyBrush, miniWingDownPoints);
g.FillPolygon(additionalBrush, miniWingDownPoints);
}
}
}

View File

@ -0,0 +1,222 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Entities;
namespace AirFighter.DrawningObjects
{
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningAirplane
{
// Класс-сущность
public EntityAirplane? EntityAirplane { get; protected set; }
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки самолета
protected int _startPosX;
// Верхняя кооридната прорисовки самолета
protected int _startPosY;
// Ширина прорисовки самолета
protected readonly int _airplaneWidth = 90;
// Высота прорисовки самолета
protected readonly int _airplaneHeight = 95;
// Координата X объекта
public int GetPosX => _startPosX;
// Координата Y объекта
public int GetPosY => _startPosY;
// Ширина объекта
public int GetWidth => _airplaneWidth;
// Высота объекта
public int GetHeight => _airplaneHeight;
// Конструктор
public DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _airplaneWidth || height < _airplaneHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
// Конструктор
protected DrawningAirplane(int speed, double weight, Color bodyColor, int
width, int height, int airplaneWidth, int airplaneHeight)
{
if (width < _airplaneWidth || height < _airplaneHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
// Установка позиции
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _airplaneWidth) || _startPosY > (_pictureHeight - _airplaneHeight))
{
_startPosX = 50;
_startPosY = 50;
}
}
// Изменение направления перемещения
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityAirplane == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
_startPosX -= (int)EntityAirplane.Step;
break;
case DirectionType.Up:
_startPosY -= (int)EntityAirplane.Step;
break;
case DirectionType.Right:
_startPosX += (int)EntityAirplane.Step;
break;
case DirectionType.Down:
_startPosY += (int)EntityAirplane.Step;
break;
}
}
public bool CanMove(DirectionType direction)
{
if (EntityAirplane == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityAirplane.Step > 0,
DirectionType.Up => _startPosY - EntityAirplane.Step > 0,
DirectionType.Right => _startPosX + _airplaneWidth + EntityAirplane.Step < _pictureWidth,
DirectionType.Down => _startPosY + _airplaneHeight + EntityAirplane.Step < _pictureHeight,
_ => false,
};
}
// Прорисовка объекта
public virtual void DrawTransport(Graphics g)
{
if (EntityAirplane == null)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor);
//тело истребителя
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 40, 80, 15);
//нос
Point nose1 = new Point(_startPosX, _startPosY + 48);
Point nose2 = new Point(_startPosX + 10, _startPosY + 48 - 8);
Point nose3 = new Point(_startPosX + 10, _startPosY + 48 + 8);
Point[] nosePoints =
{
nose1,
nose2,
nose3
};
g.FillPolygon(bodyBrush, nosePoints);
//передние крылья
Point wingUp1 = new Point(_startPosX + 30, _startPosY + 40);
Point wingUp2 = new Point(_startPosX + 30, _startPosY);
Point wingUp3 = new Point(_startPosX + 40, _startPosY);
Point wingUp4 = new Point(_startPosX + 45, _startPosY + 40);
Point[] wingUpPoints =
{
wingUp1,
wingUp2,
wingUp3,
wingUp4
};
g.DrawPolygon(pen, wingUpPoints);
Point wingDown1 = new Point(_startPosX + 30, _startPosY + 55);
Point wingDown2 = new Point(_startPosX + 30, _startPosY + 95);
Point wingDown3 = new Point(_startPosX + 40, _startPosY + 95);
Point wingDown4 = new Point(_startPosX + 45, _startPosY + 55);
Point[] wingDownPoints =
{
wingDown1,
wingDown2,
wingDown3,
wingDown4
};
g.DrawPolygon(pen, wingDownPoints);
//хвост
Point tailUp1 = new Point(_startPosX + 75, _startPosY + 40);
Point tailUp2 = new Point(_startPosX + 75, _startPosY + 30);
Point tailUp3 = new Point(_startPosX + 90, _startPosY + 15);
Point tailUp4 = new Point(_startPosX + 90, _startPosY + 40);
Point[] tailUpPoints =
{
tailUp1,
tailUp2,
tailUp3,
tailUp4,
};
g.DrawPolygon(pen, tailUpPoints);
Point tailDown1 = new Point(_startPosX + 75, _startPosY + 55);
Point tailDown2 = new Point(_startPosX + 75, _startPosY + 65);
Point tailDown3 = new Point(_startPosX + 90, _startPosY + 80);
Point tailDown4 = new Point(_startPosX + 90, _startPosY + 55);
Point[] tailDownPoints =
{
tailDown1,
tailDown2,
tailDown3,
tailDown4,
};
g.DrawPolygon(pen, tailDownPoints);
}
}
}

View File

@ -0,0 +1,41 @@
using AirFighter.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.DrawningObjects;
namespace AirFighter.MovementStrategy
{
// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar
public class DrawningObjectAirplane : IMoveableObject
{
private readonly DrawningAirplane? _drawningAirplane = null;
public DrawningObjectAirplane(DrawningAirplane drawningCar)
{
_drawningAirplane = drawningCar;
}
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?.MoveTransport(direction);
}
}

View File

@ -4,19 +4,11 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
namespace AirFighter.Entities
{
public class EntityAirFighter
// Класс-сущность "истребитель"
public class EntityAirFighter : EntityAirplane
{
//Скорость
public int Speed { get; private set; }
//Вес
public double Weight { get; private set; }
//Основной цвет
public Color BodyColor { get; private set; }
//Дополнительный цвет (для опциональных элементов)
public Color AdditionalColor { get; private set; }
@ -25,17 +17,10 @@ namespace AirFighter
// Признак(опция) наличия крыльев
public bool Wings { get; private set; }
// Шаг перемещения
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool rockets, bool wings)
public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor, bool rockets, bool wings)
: base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Rockets = rockets;
Wings = wings;
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter.Entities
{
// Класс-сущность "самолет"
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;
}
}
}

View File

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

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter.MovementStrategy
{
// Стратегия перемещения объекта в центр экрана
internal 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,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter.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)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter.MovementStrategy
{
// Статус выполнения операции перемещения
public enum Status
{
NotInit,
InProgress,
Finish
}
}