131 lines
4.7 KiB
C#
131 lines
4.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AirplaneWithRadar
|
||
{
|
||
internal class DrawingAirplane
|
||
{
|
||
public EntityAirplane Airplane { get; protected set; }
|
||
protected float _startPosX;
|
||
protected float _startPosY;
|
||
private int? _pictureWidth = null;
|
||
private int? _pictureHeight = null;
|
||
private readonly int _airplaneWidth = 50;
|
||
private readonly int _airplaneHeight = 27;
|
||
public DrawingAirplane(int speed, float weight, Color bodyColor)
|
||
{
|
||
Airplane = new EntityAirplane(speed, weight, bodyColor);
|
||
}
|
||
protected DrawingAirplane(int speed, float weight, Color bodyColor, int airplaneWidth, int airplaneHeight) :
|
||
this(speed, weight, bodyColor)
|
||
{
|
||
_airplaneWidth = airplaneWidth;
|
||
_airplaneHeight = airplaneHeight;
|
||
}
|
||
public void SetPosition(int x, int y, int width, int height)
|
||
{
|
||
if (x + _airplaneWidth <= width && y + _airplaneHeight <= height && x >= 0 && y >= 0)
|
||
{
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
}
|
||
}
|
||
public void MoveTransport(Direction direction)
|
||
{
|
||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
// вправо
|
||
case Direction.Right:
|
||
if (_startPosX + _airplaneWidth + Airplane.Step < _pictureWidth)
|
||
{
|
||
_startPosX += Airplane.Step;
|
||
}
|
||
break;
|
||
//влево
|
||
case Direction.Left:
|
||
if (_startPosX - Airplane.Step > 0)
|
||
{
|
||
_startPosX -= Airplane.Step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case Direction.Up:
|
||
if (_startPosY - Airplane.Step > 0)
|
||
{
|
||
_startPosY -= Airplane.Step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case Direction.Down:
|
||
if (_startPosY + _airplaneHeight + Airplane.Step + 30 < _pictureHeight)
|
||
{
|
||
_startPosY += Airplane.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
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, _startPosY + 10, 40, 10);
|
||
|
||
// Окно
|
||
Brush darkBrush = new SolidBrush(Airplane?.BodyColor ?? Color.Black);
|
||
g.FillRectangle(darkBrush, _startPosX + 12, _startPosY + 13, 20, 2);
|
||
|
||
// Хвост
|
||
darkBrush = new SolidBrush(Color.Black);
|
||
g.DrawLine(pen, _startPosX, _startPosY + 12, _startPosX, _startPosY);
|
||
g.DrawLine(pen, _startPosX, _startPosY, _startPosX + 10, _startPosY + 10);
|
||
|
||
// Заднее шасси
|
||
g.FillRectangle(darkBrush, _startPosX + 10, _startPosY + 21, 2, 2);
|
||
g.FillRectangle(darkBrush, _startPosX + 13, _startPosY + 23, 4, 4);
|
||
g.FillRectangle(darkBrush, _startPosX + 8, _startPosY + 23, 2, 4);
|
||
|
||
// Переднее шасси
|
||
g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 21, 2, 2);
|
||
g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 23, 4, 4);
|
||
|
||
// Нос
|
||
g.DrawLine(pen, _startPosX + 40, _startPosY + 10, _startPosX + 47, _startPosY + 15);
|
||
g.DrawLine(pen, _startPosX + 40, _startPosY + 20, _startPosX + 50, _startPosY + 15);
|
||
}
|
||
public void ChangeBorders(int width, int height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_pictureWidth <= _airplaneWidth || _pictureHeight <= _airplaneHeight)
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
return;
|
||
}
|
||
if (_startPosX + _airplaneWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth.Value - _airplaneWidth;
|
||
}
|
||
if (_startPosY + _airplaneHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight.Value - _airplaneHeight;
|
||
}
|
||
}
|
||
}
|
||
}
|