PIbd22_NikiforovaMV_Contain.../ContainerShip/DrawningShip.cs
2023-12-16 11:14:14 +04:00

124 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.Entities;
using ContainerShip.MovementStrategy;
namespace ContainerShip.DrawningObjects
{
public class DrawningShip
{
public EntityShip? EntityShip { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _shipWidth = 150;
protected readonly int _shipHeight = 65;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _shipWidth;
public int GetHeight => _shipHeight;
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight)
{
if (width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_shipWidth = shipWidth;
_shipHeight = shipHeight;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x > _pictureWidth || y > _pictureHeight)
{
return;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityShip == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityShip.Step > 0)
{
_startPosX -= (int)EntityShip.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityShip.Step > 0)
{
_startPosY -= (int)EntityShip.Step;
}
break;
case DirectionType.Right:
if (_startPosX + _shipWidth + EntityShip.Step < _pictureWidth)
{
_startPosX += (int)EntityShip.Step;
}
break;
case DirectionType.Down:
if (_startPosY + _shipHeight + EntityShip.Step < _pictureHeight)
{
_startPosY += (int)EntityShip.Step;
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityShip.BodyColor);
Point point1 = new Point(_startPosX, _startPosY + 52);
Point point2 = new Point(_startPosX + 137, _startPosY + 52);
Point point3 = new Point(_startPosX + 137 - 20, _startPosY + 52 + 46 - 10);
Point point4 = new Point(_startPosX + 20, _startPosY + 52 + 46 - 10);
Point[] p = { point1, point2, point3, point4 };
g.FillPolygon(bodyBrush, p);
g.FillRectangle(bodyBrush, _startPosX + 15, _startPosY + 32, 80, 20);
}
public bool CanMove(DirectionType direction)
{
if (EntityShip == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityShip.Step > 0,
DirectionType.Up => _startPosY - EntityShip.Step > 0,
DirectionType.Right => _startPosX + _shipWidth + EntityShip.Step < _pictureWidth,
DirectionType.Down => _startPosY + _shipHeight + EntityShip.Step < _pictureHeight,
_ => false,
};
}
public IMoveableObject GetMoveableObject => new DrawingObjectShip(this);
}
}