перенос 2 лабы
This commit is contained in:
parent
a7f1fc2910
commit
0b3396d17f
141
Tank/Tank/AbstractStrategy.cs
Normal file
141
Tank/Tank/AbstractStrategy.cs
Normal file
@ -0,0 +1,141 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
// using Tank.Drawings;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private Status _state = Status.NotInit;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public Status GetStatus() { return _state; }
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false -
|
||||
// неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(Direction.Left);
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,
|
||||
// false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(Direction.Right);
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,
|
||||
// false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(Direction.Up);
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться,
|
||||
// false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(Direction.Down);
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters =>
|
||||
_moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
225
Tank/Tank/DrawingArmoredCar.cs
Normal file
225
Tank/Tank/DrawingArmoredCar.cs
Normal file
@ -0,0 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tank.Entites;
|
||||
|
||||
namespace Tank.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
|
||||
public class DrawingArmoredCar
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityArmoredCar? EntityArmoredCar { get; protected set; }
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _ArmoredcarWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _ArmoredcarHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному
|
||||
/// направлению</returns>
|
||||
public bool CanMove(Direction direction)
|
||||
{
|
||||
if (EntityArmoredCar == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
Direction.Left => _startPosX - EntityArmoredCar.Step > 0,
|
||||
//вверх
|
||||
Direction.Up => _startPosY - EntityArmoredCar.Step > 0,
|
||||
// вправо
|
||||
Direction.Right => false,// TODO: Продумать логику
|
||||
//вниз
|
||||
Direction.Down => false,// TODO: Продумать логику
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityArmoredCar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case Direction.Left:
|
||||
_startPosX -= (int)EntityArmoredCar.Step;
|
||||
break;
|
||||
//вверх
|
||||
case Direction.Up:
|
||||
_startPosY -= (int)EntityArmoredCar.Step;
|
||||
break;
|
||||
// вправо
|
||||
case Direction.Right:
|
||||
_startPosX += (int)EntityArmoredCar.Step;
|
||||
break;
|
||||
//вниз
|
||||
case Direction.Down:
|
||||
_startPosY += (int)EntityArmoredCar.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected readonly int _ArmoredcarWidth = 100;
|
||||
/// <summary>
|
||||
/// Высота прорисовки автомобиля
|
||||
/// </summary>
|
||||
protected readonly int _ArmoredcarHeight = 55;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public DrawningArmoredCar(int speed, double weight, Color bodyColor, int
|
||||
width, int height)
|
||||
{
|
||||
// TODO: Продумать проверки
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityArmoredCar = new EntityArmoredCar(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <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 DrawningArmoredCar(int speed, double weight, Color bodyColor, int
|
||||
width, int height, int carWidth, int carHeight)
|
||||
{
|
||||
// TODO: Продумать проверки
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_ArmoredcarWidth = carWidth;
|
||||
_ArmoredcarHeight = carHeight;
|
||||
EntityArmoredCar = new EntityArmoredCar(speed, weight, bodyColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
// TODO: Изменение x, y, если при установке объект выходит за
|
||||
// границы
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
//public void MoveTransport(Direction direction)
|
||||
//{
|
||||
// if (EntityArmoredCar == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
// switch (direction)
|
||||
// {
|
||||
// //влево
|
||||
// case Direction.Left:
|
||||
// if (_startPosX - EntityArmoredCar.Step > 0)
|
||||
// {
|
||||
// _startPosX -= (int)EntityArmoredCar.Step;
|
||||
// }
|
||||
// break;
|
||||
// //вверх
|
||||
// case Direction.Up:
|
||||
// if (_startPosY - EntityArmoredCar.Step > 0)
|
||||
// {
|
||||
// _startPosY -= (int)EntityArmoredCar.Step;
|
||||
// }
|
||||
// break;
|
||||
// // вправо
|
||||
// case Direction.Right:
|
||||
// if (_startPosX + EntityArmoredCar.Step + _ArmoredcarWidth < _pictureWidth)
|
||||
// {
|
||||
// _startPosX += (int)EntityArmoredCar.Step;
|
||||
// }
|
||||
// break;
|
||||
// //вниз
|
||||
// case Direction.Down:
|
||||
// if (_startPosY + EntityArmoredCar.Step + _ArmoredcarHeight < _pictureHeight)
|
||||
// {
|
||||
// _startPosY += (int)EntityArmoredCar.Step;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityArmoredCar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//кузов
|
||||
Brush br = new SolidBrush(EntityArmoredCar.BodyColor);
|
||||
g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
40
Tank/Tank/DrawingObjectArmoredCar.cs
Normal file
40
Tank/Tank/DrawingObjectArmoredCar.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tank.DrawingObjects;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar
|
||||
/// (паттерн Adapter)
|
||||
/// </summary>
|
||||
public class DrawningObjectCar : IMoveableObject
|
||||
{
|
||||
private readonly DrawningArmoredCar? _drawningArmoredCar = null;
|
||||
public DrawningObjectCar(DrawningArmoredCar drawningArmoredCar)
|
||||
{
|
||||
_drawningArmoredCar = drawningArmoredCar;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningArmoredCar == null || _drawningArmoredCar.EntityArmoredCar ==
|
||||
null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningArmoredCar.GetPosX,
|
||||
_drawningArmoredCar.GetPosY, _drawningArmoredCar.GetWidth, _drawningArmoredCar.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningArmoredCar?.EntityCar?.Step ?? 0);
|
||||
public bool CheckCanMove(Direction direction) =>
|
||||
_drawningArmoredCar?.CanMove(direction) ?? false;
|
||||
public void MoveObject(Direction direction) =>
|
||||
_drawningArmoredCar?.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -4,220 +4,236 @@ using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tank.DrawingObjects;
|
||||
using Tank.Entites;
|
||||
|
||||
namespace Tank
|
||||
namespace Tank.DrawingObjects
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningTank
|
||||
public class DrawningTank : DrawingArmoredCar
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityTank? EntityTank { get; private set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int _pictureWidth = 900;
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int _pictureHeight = 500;
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки автомобиля
|
||||
/// </summary>
|
||||
private int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки автомобиля
|
||||
/// </summary>
|
||||
private int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки автомобиля
|
||||
/// </summary>
|
||||
private readonly int _tankWidth = 110;
|
||||
/// <summary>
|
||||
/// Высота прорисовки автомобиля
|
||||
/// </summary>
|
||||
private readonly int _tankHeight = 60;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Цвет кузова</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bodyKit">Признак наличия обвеса</param>
|
||||
/// <param name="trunk">Признак наличия антикрыла</param>
|
||||
/// <param name="wing">Признак наличия антикрыла</param>
|
||||
/// <param name="sportLine">Признак наличия гоночной полосы</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <returns>true - объект создан, false - проверка не пройдена,
|
||||
/// нельзя создать объект в этих размерах</returns>
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool bodyKit, bool trunk, bool sportLine, int width, int height)
|
||||
public DrawningTank(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) :
|
||||
base(speed, weight, bodyColor, width, height, 110, 60)
|
||||
{
|
||||
// TODO: Продумать проверки
|
||||
if (width > _tankWidth && height > _tankHeight && speed > 0 && weight > 0)
|
||||
if (EntityArmoredCar != null)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityTank = new EntityTank();
|
||||
EntityTank.Init(speed, weight, bodyColor, additionalColor,
|
||||
bodyKit, trunk, sportLine);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
// проверки
|
||||
if (x >= 0 && x + _tankWidth <= _pictureWidth &&
|
||||
y >= 0 && y + _tankHeight <= _pictureHeight)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
EntityArmoredCar = new EntityTank(speed, weight, bodyColor,
|
||||
additionalColor, bodyKit, wing, sportLine);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(Direction direction)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTank == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case Direction.Left:
|
||||
if (_startPosX - EntityTank.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityTank.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Direction.Up:
|
||||
if (_startPosY - EntityTank.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityTank.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case Direction.Right:
|
||||
if (_startPosX + EntityTank.Step + _tankWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityTank.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Direction.Down:
|
||||
if (_startPosY + EntityTank.Step + _tankHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityTank.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTank == null)
|
||||
if (EntityArmoredCar is not EntityTank sportCar)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new
|
||||
SolidBrush(EntityTank.AdditionalColor);
|
||||
// гусеница
|
||||
Rectangle Caterpillar = new Rectangle(_startPosX + 2, _startPosY + 35, 16, 22);
|
||||
float startAngle_Caterpillar = 90;
|
||||
float sweepAngle_Caterpillar = 180;
|
||||
g.DrawArc(pen, Caterpillar, startAngle_Caterpillar, sweepAngle_Caterpillar);
|
||||
Rectangle Caterpillar2 = new Rectangle(_startPosX + 102, _startPosY + 35, 16, 22);
|
||||
float startAngle_Caterpillar2 = 270;
|
||||
float sweepAngle_Caterpillar2 = 180;
|
||||
g.DrawArc(pen, Caterpillar2, startAngle_Caterpillar2, sweepAngle_Caterpillar2);
|
||||
Point startPoint = new Point(_startPosX + 10, _startPosY + 58);
|
||||
Point endPoint = new Point(_startPosX + 110, _startPosY + 58);
|
||||
g.DrawLine(pen, startPoint, endPoint);
|
||||
// колеса
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
g.FillEllipse(brBlack, _startPosX + 95, _startPosY + 35, 20, 20);
|
||||
g.FillEllipse(brBlack, _startPosX + 5, _startPosY + 35, 20, 20);
|
||||
// колеса снизу поменьше
|
||||
g.FillEllipse(brBlack, _startPosX + 25, _startPosY + 47, 10, 10);
|
||||
g.FillEllipse(brBlack, _startPosX + 45, _startPosY + 47, 10, 10);
|
||||
g.FillEllipse(brBlack, _startPosX + 65, _startPosY + 47, 10, 10);
|
||||
g.FillEllipse(brBlack, _startPosX + 85, _startPosY + 47, 10, 10);
|
||||
// колеса сверху
|
||||
g.FillEllipse(brBlack, _startPosX + 35, _startPosY + 32, 10, 10);
|
||||
g.FillEllipse(brBlack, _startPosX + 55, _startPosY + 32, 10, 10);
|
||||
g.FillEllipse(brBlack, _startPosX + 75, _startPosY + 32, 10, 10);
|
||||
|
||||
//кузов
|
||||
Brush br = new SolidBrush(EntityTank.BodyColor);
|
||||
g.FillRectangle(br, _startPosX + 5, _startPosY + 17, 110, 18);
|
||||
g.FillRectangle(br, _startPosX + 30, _startPosY, 50, 15);
|
||||
|
||||
// пулемет
|
||||
g.FillRectangle(br, _startPosX + 80, _startPosY + 3, 25, 5);
|
||||
|
||||
// зенитный пулемет на башне
|
||||
g.FillRectangle(br, _startPosX + 40, _startPosY - 10, 20, 5);
|
||||
|
||||
g.FillRectangle(brBlack, _startPosX + 50, _startPosY - 5, 5, 5);
|
||||
g.FillRectangle(brBlack, _startPosX + 55, _startPosY - 10, 5, 10);
|
||||
g.FillRectangle(brBlack, _startPosX + 52, _startPosY - 7, 3, 2);
|
||||
|
||||
//выделяем рамкой весь танк
|
||||
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 17, 110, 18);
|
||||
g.DrawRectangle(pen, _startPosX + 30, _startPosY, 50, 15);
|
||||
g.DrawRectangle(pen, _startPosX + 80, _startPosY + 3, 25, 5);
|
||||
g.DrawRectangle(pen, _startPosX + 40, _startPosY - 10, 20, 5);
|
||||
|
||||
SolidBrush(sportCar.AdditionalColor);
|
||||
// обвесы
|
||||
if (EntityTank.BodyKit)
|
||||
{
|
||||
Brush brAdd = new SolidBrush(EntityTank.AdditionalColor);
|
||||
|
||||
g.FillRectangle(brAdd, _startPosX + 5, _startPosY + 17, 20, 18);
|
||||
g.FillRectangle(brAdd, _startPosX + 95, _startPosY + 17, 20, 18);
|
||||
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 17, 20, 18);
|
||||
g.DrawRectangle(pen, _startPosX + 95, _startPosY + 17, 20, 18);
|
||||
|
||||
g.FillRectangle(brAdd, _startPosX + 100, _startPosY + 4, 5, 4);
|
||||
|
||||
}
|
||||
|
||||
//// спортивная линия
|
||||
//if (EntityTank.SportLine)
|
||||
//{
|
||||
// g.FillRectangle(additionalBrush, _startPosX + 75,
|
||||
// _startPosY + 23, 25, 15);
|
||||
// g.FillRectangle(additionalBrush, _startPosX + 35,
|
||||
// _startPosY + 23, 35, 15);
|
||||
// g.FillRectangle(additionalBrush, _startPosX + 10,
|
||||
// _startPosY + 23, 20, 15);
|
||||
//}
|
||||
// багажник
|
||||
if (EntityTank.Trunk)
|
||||
if (EntityArmoredCar.Trunk)
|
||||
{
|
||||
g.FillRectangle(additionalBrush, _startPosX + 23, _startPosY + 4, 8, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 23, _startPosY + 4 , 8, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 23, _startPosY + 4, 8, 10);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
// /// Инициализация свойств
|
||||
// /// </summary>
|
||||
// /// <param name="speed">Скорость</param>
|
||||
// /// <param name="weight">Вес</param>
|
||||
// /// <param name="bodyColor">Цвет кузова</param>
|
||||
// /// <param name="additionalColor">Дополнительный цвет</param>
|
||||
// /// <param name="bodyKit">Признак наличия обвеса</param>
|
||||
// /// <param name="trunk">Признак наличия антикрыла</param>
|
||||
// /// <param name="sportLine">Признак наличия гоночной полосы</param>
|
||||
// /// <param name="width">Ширина картинки</param>
|
||||
// /// <param name="height">Высота картинки</param>
|
||||
// /// <returns>true - объект создан, false - проверка не пройдена,
|
||||
// /// нельзя создать объект в этих размерах</returns>
|
||||
// public bool Init(int speed, double weight, Color bodyColor, Color
|
||||
//additionalColor, bool bodyKit, bool trunk, bool sportLine, int width, int height)
|
||||
//{
|
||||
// // TODO: Продумать проверки
|
||||
// if (width > _tankWidth && height > _tankHeight && speed > 0 && weight > 0)
|
||||
// {
|
||||
// _pictureWidth = width;
|
||||
// _pictureHeight = height;
|
||||
// EntityTank = new EntityTank();
|
||||
// EntityTank.Init(speed, weight, bodyColor, additionalColor,
|
||||
// bodyKit, trunk, sportLine);
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
//}
|
||||
///// <summary>
|
||||
///// Установка позиции
|
||||
///// </summary>
|
||||
///// <param name="x">Координата X</param>
|
||||
///// <param name="y">Координата Y</param>
|
||||
//public void SetPosition(int x, int y)
|
||||
//{
|
||||
// // проверки
|
||||
// if (x >= 0 && x + _tankWidth <= _pictureWidth &&
|
||||
// y >= 0 && y + _tankHeight <= _pictureHeight)
|
||||
// {
|
||||
// _startPosX = x;
|
||||
// _startPosY = y;
|
||||
// }
|
||||
//}
|
||||
///// <summary>
|
||||
///// Изменение направления перемещения
|
||||
///// </summary>
|
||||
///// <param name="direction">Направление</param>
|
||||
//public void MoveTransport(Direction direction)
|
||||
//{
|
||||
// if (EntityTank == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
// switch (direction)
|
||||
// {
|
||||
// //влево
|
||||
// case Direction.Left:
|
||||
// if (_startPosX - EntityTank.Step > 0)
|
||||
// {
|
||||
// _startPosX -= (int)EntityTank.Step;
|
||||
// }
|
||||
// break;
|
||||
// //вверх
|
||||
// case Direction.Up:
|
||||
// if (_startPosY - EntityTank.Step > 0)
|
||||
// {
|
||||
// _startPosY -= (int)EntityTank.Step;
|
||||
// }
|
||||
// break;
|
||||
// // вправо
|
||||
// case Direction.Right:
|
||||
// if (_startPosX + EntityTank.Step + _tankWidth < _pictureWidth)
|
||||
// {
|
||||
// _startPosX += (int)EntityTank.Step;
|
||||
// }
|
||||
// break;
|
||||
// //вниз
|
||||
// case Direction.Down:
|
||||
// if (_startPosY + EntityTank.Step + _tankHeight < _pictureHeight)
|
||||
// {
|
||||
// _startPosY += (int)EntityTank.Step;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
//public void DrawTransport(Graphics g)
|
||||
//{
|
||||
// if (EntityTank == null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
// Pen pen = new(Color.Black);
|
||||
// Brush additionalBrush = new
|
||||
// SolidBrush(EntityTank.AdditionalColor);
|
||||
// // гусеница
|
||||
// Rectangle Caterpillar = new Rectangle(_startPosX + 2, _startPosY + 35, 16, 22);
|
||||
// float startAngle_Caterpillar = 90;
|
||||
// float sweepAngle_Caterpillar = 180;
|
||||
// g.DrawArc(pen, Caterpillar, startAngle_Caterpillar, sweepAngle_Caterpillar);
|
||||
// Rectangle Caterpillar2 = new Rectangle(_startPosX + 102, _startPosY + 35, 16, 22);
|
||||
// float startAngle_Caterpillar2 = 270;
|
||||
// float sweepAngle_Caterpillar2 = 180;
|
||||
// g.DrawArc(pen, Caterpillar2, startAngle_Caterpillar2, sweepAngle_Caterpillar2);
|
||||
// Point startPoint = new Point(_startPosX + 10, _startPosY + 58);
|
||||
// Point endPoint = new Point(_startPosX + 110, _startPosY + 58);
|
||||
// g.DrawLine(pen, startPoint, endPoint);
|
||||
// // колеса
|
||||
// Brush brBlack = new SolidBrush(Color.Black);
|
||||
// g.FillEllipse(brBlack, _startPosX + 95, _startPosY + 35, 20, 20);
|
||||
// g.FillEllipse(brBlack, _startPosX + 5, _startPosY + 35, 20, 20);
|
||||
// // колеса снизу поменьше
|
||||
// g.FillEllipse(brBlack, _startPosX + 25, _startPosY + 47, 10, 10);
|
||||
// g.FillEllipse(brBlack, _startPosX + 45, _startPosY + 47, 10, 10);
|
||||
// g.FillEllipse(brBlack, _startPosX + 65, _startPosY + 47, 10, 10);
|
||||
// g.FillEllipse(brBlack, _startPosX + 85, _startPosY + 47, 10, 10);
|
||||
// // колеса сверху
|
||||
// g.FillEllipse(brBlack, _startPosX + 35, _startPosY + 32, 10, 10);
|
||||
// g.FillEllipse(brBlack, _startPosX + 55, _startPosY + 32, 10, 10);
|
||||
// g.FillEllipse(brBlack, _startPosX + 75, _startPosY + 32, 10, 10);
|
||||
|
||||
// //кузов
|
||||
// Brush br = new SolidBrush(EntityTank.BodyColor);
|
||||
// g.FillRectangle(br, _startPosX + 5, _startPosY + 17, 110, 18);
|
||||
// g.FillRectangle(br, _startPosX + 30, _startPosY, 50, 15);
|
||||
|
||||
// // пулемет
|
||||
// g.FillRectangle(br, _startPosX + 80, _startPosY + 3, 25, 5);
|
||||
|
||||
// // зенитный пулемет на башне
|
||||
// g.FillRectangle(br, _startPosX + 40, _startPosY - 10, 20, 5);
|
||||
|
||||
// g.FillRectangle(brBlack, _startPosX + 50, _startPosY - 5, 5, 5);
|
||||
// g.FillRectangle(brBlack, _startPosX + 55, _startPosY - 10, 5, 10);
|
||||
// g.FillRectangle(brBlack, _startPosX + 52, _startPosY - 7, 3, 2);
|
||||
|
||||
// //выделяем рамкой весь танк
|
||||
// g.DrawRectangle(pen, _startPosX + 5, _startPosY + 17, 110, 18);
|
||||
// g.DrawRectangle(pen, _startPosX + 30, _startPosY, 50, 15);
|
||||
// g.DrawRectangle(pen, _startPosX + 80, _startPosY + 3, 25, 5);
|
||||
// g.DrawRectangle(pen, _startPosX + 40, _startPosY - 10, 20, 5);
|
||||
|
||||
// // обвесы
|
||||
// if (EntityTank.BodyKit)
|
||||
// {
|
||||
// Brush brAdd = new SolidBrush(EntityTank.AdditionalColor);
|
||||
|
||||
// g.FillRectangle(brAdd, _startPosX + 5, _startPosY + 17, 20, 18);
|
||||
// g.FillRectangle(brAdd, _startPosX + 95, _startPosY + 17, 20, 18);
|
||||
// g.DrawRectangle(pen, _startPosX + 5, _startPosY + 17, 20, 18);
|
||||
// g.DrawRectangle(pen, _startPosX + 95, _startPosY + 17, 20, 18);
|
||||
|
||||
// g.FillRectangle(brAdd, _startPosX + 100, _startPosY + 4, 5, 4);
|
||||
|
||||
// }
|
||||
|
||||
// //// спортивная линия
|
||||
// //if (EntityTank.SportLine)
|
||||
// //{
|
||||
// // g.FillRectangle(additionalBrush, _startPosX + 75,
|
||||
// // _startPosY + 23, 25, 15);
|
||||
// // g.FillRectangle(additionalBrush, _startPosX + 35,
|
||||
// // _startPosY + 23, 35, 15);
|
||||
// // g.FillRectangle(additionalBrush, _startPosX + 10,
|
||||
// // _startPosY + 23, 20, 15);
|
||||
// //}
|
||||
// // багажник
|
||||
// if (EntityTank.Trunk)
|
||||
// {
|
||||
// g.FillRectangle(additionalBrush, _startPosX + 23, _startPosY + 4, 8, 10);
|
||||
// g.DrawRectangle(pen, _startPosX + 23, _startPosY + 4 , 8, 10);
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
44
Tank/Tank/EntityArmoredCar.cs
Normal file
44
Tank/Tank/EntityArmoredCar.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tank.Entites
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность "Бронированная машина"
|
||||
/// </summary>
|
||||
public class EntityArmoredCar
|
||||
{
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; set; }
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; set; }
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; set; }
|
||||
/// <summary>
|
||||
/// Шаг перемещения автомобиля
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 300 / Weight;
|
||||
/// <summary>
|
||||
/// Конструктор с параметрами
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public EntityArmoredCar(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -3,26 +3,27 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Tank.Entites;
|
||||
|
||||
namespace Tank
|
||||
namespace Tank.Entites
|
||||
{
|
||||
public class EntityTank
|
||||
public class EntityTank : EntityArmoredCar
|
||||
{
|
||||
/// <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>
|
||||
///// <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; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия обвеса
|
||||
@ -39,7 +40,7 @@ namespace Tank
|
||||
/// <summary>
|
||||
/// Шаг перемещения танка
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 200 / Weight;
|
||||
// public double Step => (double)Speed * 200 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса спортивного автомобиля
|
||||
/// </summary>
|
||||
@ -50,7 +51,7 @@ namespace Tank
|
||||
/// <param name="bodyKit">Признак наличия обвеса</param>
|
||||
/// <param name="trunk">Признак наличия багажника</param>
|
||||
/// <param name="sportLine">Признак наличия гоночной полосы</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
public EntityTank(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool bodyKit, bool trunk, bool sportLine)
|
||||
{
|
||||
Speed = speed;
|
||||
|
29
Tank/Tank/FormTank.Designer.cs
generated
29
Tank/Tank/FormTank.Designer.cs
generated
@ -34,6 +34,8 @@
|
||||
this.buttonLeft = new System.Windows.Forms.Button();
|
||||
this.buttonDown = new System.Windows.Forms.Button();
|
||||
this.buttonRight = new System.Windows.Forms.Button();
|
||||
this.buttonCreateArmoredCar = new System.Windows.Forms.Button();
|
||||
this.buttonStep = new System.Windows.Forms.Button();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTank)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
@ -52,9 +54,9 @@
|
||||
//
|
||||
this.buttonCreate.Location = new System.Drawing.Point(26, 417);
|
||||
this.buttonCreate.Name = "buttonCreate";
|
||||
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
|
||||
this.buttonCreate.Size = new System.Drawing.Size(97, 23);
|
||||
this.buttonCreate.TabIndex = 1;
|
||||
this.buttonCreate.Text = "Создать";
|
||||
this.buttonCreate.Text = "Создать Танк";
|
||||
this.buttonCreate.UseVisualStyleBackColor = true;
|
||||
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
|
||||
//
|
||||
@ -102,11 +104,32 @@
|
||||
this.buttonRight.UseVisualStyleBackColor = true;
|
||||
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
|
||||
//
|
||||
// buttonCreateArmoredCar
|
||||
//
|
||||
this.buttonCreateArmoredCar.Location = new System.Drawing.Point(129, 417);
|
||||
this.buttonCreateArmoredCar.Name = "buttonCreateArmoredCar";
|
||||
this.buttonCreateArmoredCar.Size = new System.Drawing.Size(210, 23);
|
||||
this.buttonCreateArmoredCar.TabIndex = 7;
|
||||
this.buttonCreateArmoredCar.Text = "Создать Бронированную машину\r\n";
|
||||
this.buttonCreateArmoredCar.UseVisualStyleBackColor = true;
|
||||
this.buttonCreateArmoredCar.Click += new System.EventHandler(this.buttonCreateArmoredCar_Click);
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
this.buttonStep.Location = new System.Drawing.Point(682, 48);
|
||||
this.buttonStep.Name = "buttonStep";
|
||||
this.buttonStep.Size = new System.Drawing.Size(75, 23);
|
||||
this.buttonStep.TabIndex = 9;
|
||||
this.buttonStep.Text = "buttonStep";
|
||||
this.buttonStep.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// FormTank
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(884, 461);
|
||||
this.Controls.Add(this.buttonStep);
|
||||
this.Controls.Add(this.buttonCreateArmoredCar);
|
||||
this.Controls.Add(this.buttonRight);
|
||||
this.Controls.Add(this.buttonDown);
|
||||
this.Controls.Add(this.buttonLeft);
|
||||
@ -131,5 +154,7 @@
|
||||
private Button buttonLeft;
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonCreateArmoredCar;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
using Tank.DrawingObjects;
|
||||
using Tank.MovementStrategy;
|
||||
|
||||
namespace Tank
|
||||
{
|
||||
/// <summary>
|
||||
@ -8,7 +11,13 @@ namespace Tank
|
||||
/// <summary>
|
||||
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
|
||||
/// </summary>
|
||||
//private DrawningArmoredCar? _drawningArmoredCar;
|
||||
private DrawningTank? _drawningTank;
|
||||
/// <summary>
|
||||
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Èíèöèàëèçàöèÿ ôîðìû
|
||||
/// </summary>
|
||||
@ -21,7 +30,7 @@ namespace Tank
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningTank == null)
|
||||
if (_drawningTank == null) // _drawingArmoredCar
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -32,7 +41,7 @@ namespace Tank
|
||||
pictureBoxTank.Image = bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
|
||||
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü Òàíê"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
@ -50,7 +59,8 @@ namespace Tank
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningTank = new DrawningTank();
|
||||
_drawningArmoredCar = new DrawningTank(random.Next(100, 300),
|
||||
random.Next(1000, 3000);
|
||||
_drawningTank.Init(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
@ -94,6 +104,24 @@ namespace Tank
|
||||
}
|
||||
Draw();
|
||||
|
||||
}
|
||||
// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü Áðîíèðîâàííóþ ìàøèíó"
|
||||
private void buttonCreateArmoredCar_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningArmoredCar = new DrawningArmoredCar(random.Next(100, 300),
|
||||
random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
|
||||
random.Next(0, 256)),
|
||||
pictureBoxTank.Width, pictureBoxTank.Height);
|
||||
_drawningArmoredCar.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
37
Tank/Tank/IMoveableObject.cs
Normal file
37
Tank/Tank/IMoveableObject.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
// using Tank.Drawnings;
|
||||
using Tank.DrawingObjects;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты X объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
/// <summary>
|
||||
/// Проверка, можно ли переместиться по нужному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></returns>
|
||||
bool CheckCanMove(Direction direction);
|
||||
/// <summary>
|
||||
/// Изменение направления пермещения объекта
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
void MoveObject(Direction direction);
|
||||
|
||||
}
|
||||
}
|
59
Tank/Tank/MoveToCenter.cs
Normal file
59
Tank/Tank/MoveToCenter.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
59
Tank/Tank/ObjectParameters.cs
Normal file
59
Tank/Tank/ObjectParameters.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
19
Tank/Tank/Status.cs
Normal file
19
Tank/Tank/Status.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tank.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
|
||||
}
|
||||
}
|
@ -23,4 +23,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Drawings\" />
|
||||
<Folder Include="Entites\" />
|
||||
<Folder Include="MovementStrategy\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user