197 lines
8.7 KiB
C#
197 lines
8.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AntiAircraftGun
|
||
{
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
internal class DrawingAntiAircraftGun
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityAntiAircraftGun AntiAircraftGun { protected set; get; }
|
||
/// <summary>
|
||
/// Левая координата отрисовки зенитного орудия
|
||
/// </summary>
|
||
protected float _startPosX;
|
||
/// <summary>
|
||
/// Верхняя кооридната отрисовки зенитного орудия
|
||
/// </summary>
|
||
protected float _startPosY;
|
||
/// <summary>
|
||
/// Ширина окна отрисовки
|
||
/// </summary>
|
||
private int? _pictureWidth = null;
|
||
/// <summary>
|
||
/// Высота окна отрисовки
|
||
/// </summary>
|
||
private int? _pictureHeight = null;
|
||
/// <summary>
|
||
/// Ширина отрисовки зенитного орудия
|
||
/// </summary>
|
||
private readonly int _antiAircrafGunWidth = 90;
|
||
/// <summary>
|
||
/// Высота отрисовки зенитного орудия
|
||
/// </summary>
|
||
private readonly int _antiAircrafGunHeight = 40;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес зенитного орудия</param>
|
||
/// <param name="bodyColor">Цвет корпуса</param>
|
||
public DrawingAntiAircraftGun(int speed, float weight, Color bodyColor)
|
||
{
|
||
AntiAircraftGun = new EntityAntiAircraftGun(speed, weight, bodyColor);
|
||
}
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес зенитного орудия</param>
|
||
/// <param name="bodyColor">Цвет корпуса</param>
|
||
/// <param name="_antiAircraftGunWidth">Ширина отрисовки зенитного орудия</param>
|
||
/// <param name="_antiAircraftGunHeight">Высота отрисовки зенитного орудия</param>
|
||
protected DrawingAntiAircraftGun(int speed, float weight, Color bodyColor, int antiAircrafGunWidth, int antiAircrafGunHeight) :
|
||
this(speed, weight, bodyColor)
|
||
{
|
||
_antiAircrafGunWidth = antiAircrafGunWidth;
|
||
_antiAircrafGunHeight = antiAircrafGunHeight;
|
||
}
|
||
/// <summary>
|
||
/// Установка позиции зенитного орудия
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
public void SetPosition(int x, int y, int width, int height)
|
||
{
|
||
_pictureHeight = height < _antiAircrafGunHeight ? null : height;
|
||
_pictureWidth = width < _antiAircrafGunWidth ? null : width;
|
||
if(_pictureHeight.HasValue && _pictureWidth.HasValue)
|
||
{
|
||
_startPosX = x < 0 || x > _pictureWidth - _antiAircrafGunWidth ? (int)_pictureWidth - _antiAircrafGunWidth : x;
|
||
_startPosY = y < 0 || y > _pictureHeight - _antiAircrafGunHeight ? (int)_pictureHeight - _antiAircrafGunHeight : y;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
public void MoveTransport(Direction direction)
|
||
{
|
||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
// вправо
|
||
case Direction.Right:
|
||
if (_startPosX + _antiAircrafGunWidth + AntiAircraftGun.Step < _pictureWidth)
|
||
{
|
||
_startPosX += AntiAircraftGun.Step;
|
||
}
|
||
break;
|
||
//влево
|
||
case Direction.Left:
|
||
if (_startPosX - AntiAircraftGun.Step >= 0)
|
||
{
|
||
_startPosX -= AntiAircraftGun.Step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case Direction.Up:
|
||
if (_startPosY - AntiAircraftGun.Step >= 0)
|
||
{
|
||
_startPosY -= AntiAircraftGun.Step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case Direction.Down:
|
||
if (_startPosY + _antiAircrafGunHeight + AntiAircraftGun.Step < _pictureHeight)
|
||
{
|
||
_startPosY += AntiAircraftGun.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Отрисовка зенитного орудия
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (_startPosX < 0 || _startPosY < 0
|
||
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
//границы зенитного орудия
|
||
g.DrawRectangle(pen, _startPosX + 25, _startPosY, 40, 10);
|
||
g.DrawRectangle(pen, _startPosX + 5, _startPosY + 10, 80, 10);
|
||
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 70, 20);
|
||
g.DrawEllipse(pen, _startPosX, _startPosY + 20, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX + 70, _startPosY + 20, 20, 20);
|
||
//гусеницы
|
||
Brush brGray = new SolidBrush(Color.Gray);
|
||
g.FillRectangle(brGray, _startPosX + 10, _startPosY + 20, 70, 20);
|
||
g.FillEllipse(brGray, _startPosX, _startPosY + 20, 20, 20);
|
||
g.FillEllipse(brGray, _startPosX + 70, _startPosY + 20, 20, 20);
|
||
//катки в гусеницах
|
||
Brush brBlack = new SolidBrush(Color.Black);
|
||
g.FillEllipse(brBlack, _startPosX +1, _startPosY + 21, 18, 18);
|
||
g.FillEllipse(brBlack, _startPosX + 69, _startPosY + 21, 18, 18);
|
||
g.FillEllipse(brBlack, _startPosX + 19, _startPosY + 30, 11, 11);
|
||
g.FillEllipse(brBlack, _startPosX + 58, _startPosY + 30, 11, 11);
|
||
g.FillEllipse(brBlack, _startPosX + 32, _startPosY + 30, 11, 11);
|
||
g.FillEllipse(brBlack, _startPosX + 45, _startPosY + 30, 11, 11);
|
||
//корпус
|
||
Brush br = new SolidBrush(AntiAircraftGun?.BodyColor ?? Color.Black);
|
||
g.FillRectangle(br, _startPosX + 25, _startPosY, 40, 10);
|
||
g.FillRectangle(br, _startPosX + 5, _startPosY + 10, 80, 10);
|
||
g.FillEllipse(br, _startPosX + 27, _startPosY + 16, 8, 8);
|
||
g.FillEllipse(br, _startPosX + 40, _startPosY + 16, 8, 8);
|
||
g.FillEllipse(br, _startPosX + 53, _startPosY + 16, 8, 8);
|
||
}
|
||
/// <summary>
|
||
/// Смена границ формы отрисовки
|
||
/// </summary>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
public void ChangeBorders(int width, int height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_pictureWidth <= _antiAircrafGunWidth || _pictureHeight <= _antiAircrafGunHeight)
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
return;
|
||
}
|
||
if (_startPosX + _antiAircrafGunWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth.Value - _antiAircrafGunWidth;
|
||
}
|
||
if (_startPosY + _antiAircrafGunHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight.Value - _antiAircrafGunHeight;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Получение текущей позиции объекта
|
||
/// </summary>
|
||
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
|
||
{
|
||
return (_startPosX, _startPosX + _antiAircrafGunWidth, _startPosY, _startPosY + _antiAircrafGunHeight);
|
||
}
|
||
}
|
||
}
|