137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ContainerShip
|
|
{
|
|
internal class DrawingShip
|
|
{
|
|
public EntityShip Ship { get; protected set; }
|
|
protected float _startPosX;
|
|
protected float _startPosY;
|
|
private int? _pictureWidth = null;
|
|
private int? _pictureHeight = null;
|
|
|
|
private readonly int _shipWidth = 100;
|
|
private readonly int _shipHeight = 60;
|
|
public DrawingShip(int speed, float weight, Color bodyColor)
|
|
{
|
|
Ship = new EntityShip(speed, weight, bodyColor);
|
|
}
|
|
protected DrawingShip(int speed, float weight, Color bodyColor, int shipWidth,
|
|
int shipHeight) : this(speed, weight, bodyColor)
|
|
{
|
|
_shipWidth = shipWidth;
|
|
_shipHeight = shipHeight;
|
|
}
|
|
|
|
public void SetPosition(int x, int y, int width, int height)
|
|
{
|
|
if (x < 0 || y < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (x + _shipWidth > width || y + _shipHeight > height)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
}
|
|
|
|
public void MoveShip(Direction direction)
|
|
{
|
|
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
case Direction.Right:
|
|
if (_startPosX + _shipWidth + Ship.Step < _pictureWidth)
|
|
{
|
|
_startPosX += Ship.Step;
|
|
}
|
|
break;
|
|
case Direction.Left:
|
|
if (_startPosX - Ship.Step > 0)
|
|
{
|
|
_startPosX -= Ship.Step;
|
|
}
|
|
break;
|
|
case Direction.Up:
|
|
if (_startPosY - Ship.Step > 0)
|
|
{
|
|
_startPosY -= Ship.Step;
|
|
}
|
|
break;
|
|
case Direction.Down:
|
|
if (_startPosY + _shipHeight + Ship.Step < _pictureHeight)
|
|
{
|
|
_startPosY += Ship.Step;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public virtual void DrawTransport(Graphics g)
|
|
{
|
|
if (_startPosX < 0 || _startPosY < 0
|
|
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Pen pen = new(Color.Black);
|
|
Brush brRed = new SolidBrush(Color.Red);
|
|
Brush brMain = new SolidBrush(Ship?.BodyColor ?? Color.Blue);
|
|
|
|
//Границы основания корабля
|
|
PointF point1 = new PointF(_startPosX, _startPosY+30);
|
|
PointF point2 = new PointF(_startPosX+100, _startPosY+30);
|
|
PointF point3 = new PointF(_startPosX + 80, _startPosY + 60);
|
|
PointF point4 = new PointF(_startPosX + 20, _startPosY + 60);
|
|
PointF[] shipBorder = new PointF[4] {point1, point2, point3, point4};
|
|
g.DrawPolygon(pen, shipBorder);
|
|
//Границы верхней палубы
|
|
g.DrawRectangle(pen, _startPosX + 20, _startPosY, 60, 30);
|
|
//Заливка основания корабля
|
|
g.FillPolygon(brRed, shipBorder);
|
|
//Заливка верхней палубы
|
|
g.FillRectangle(brMain, _startPosX + 21, _startPosY + 1, 59, 29);
|
|
|
|
|
|
}
|
|
public void ChangeBorders(int width, int height)
|
|
{
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
|
|
{
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
return;
|
|
}
|
|
if (_startPosX + _shipWidth > _pictureWidth)
|
|
{
|
|
_startPosX = _pictureWidth.Value - _shipWidth;
|
|
}
|
|
if (_startPosY + _shipHeight > _pictureHeight)
|
|
{
|
|
_startPosY = _pictureHeight.Value - _shipHeight;
|
|
}
|
|
}
|
|
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
|
|
{
|
|
return (_startPosX, _startPosY, _startPosX + _shipWidth, _startPosY + _shipHeight);
|
|
}
|
|
}
|
|
}
|