134 lines
4.2 KiB
C#
134 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using ContainerShip.Entities;
|
|
|
|
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 _busWidth = 150;
|
|
|
|
protected readonly int _busHeight = 65;
|
|
|
|
public int GetPosX => _startPosX;
|
|
public int GetPosY => _startPosY;
|
|
public int GetWidth => _busWidth;
|
|
public int GetHeight => _busHeight;
|
|
|
|
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
|
|
{
|
|
if (width < _busWidth || height < _busHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
EntityShip = new EntityShip(speed, weight, bodyColor);
|
|
}
|
|
protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight)
|
|
{
|
|
if (width < _busWidth || height < _busHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
_busWidth = busWidth;
|
|
_busHeight = busHeight;
|
|
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 + _busWidth + EntityShip.Step < _pictureWidth)
|
|
{
|
|
_startPosX += (int)EntityShip.Step;
|
|
}
|
|
break;
|
|
|
|
case DirectionType.Down:
|
|
if (_startPosY + _busHeight + 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 + _busWidth + EntityShip.Step < _pictureWidth,
|
|
DirectionType.Down => _startPosY + _busHeight + EntityShip.Step < _pictureHeight,
|
|
_ => false,
|
|
};
|
|
}
|
|
}
|
|
}
|