PIbd22_NikiforovaMV_Contain.../ContainerShip/DrawningShip.cs

130 lines
4.4 KiB
C#
Raw Permalink Normal View History

2023-11-25 12:44:21 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContainerShip.Entities;
2023-12-16 11:10:10 +04:00
using ContainerShip.MovementStrategy;
2023-11-25 12:44:21 +04:00
namespace ContainerShip.DrawningObjects
{
public class DrawningShip
{
public EntityShip? EntityShip { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
2023-12-16 11:14:14 +04:00
protected readonly int _shipWidth = 150;
2023-12-16 11:10:10 +04:00
protected readonly int _shipHeight = 65;
2023-11-25 12:44:21 +04:00
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
2023-12-16 11:10:10 +04:00
public int GetWidth => _shipWidth;
public int GetHeight => _shipHeight;
2023-11-25 12:44:21 +04:00
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
{
2023-12-16 11:10:10 +04:00
if (width < _shipWidth || height < _shipHeight)
2023-11-25 12:44:21 +04:00
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
2023-12-16 11:10:10 +04:00
protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight)
2023-11-25 12:44:21 +04:00
{
2023-12-16 11:10:10 +04:00
if (width < _shipWidth || height < _shipHeight)
2023-11-25 12:44:21 +04:00
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
2023-12-16 11:10:10 +04:00
_shipWidth = shipWidth;
_shipHeight = shipHeight;
2023-11-25 12:44:21 +04:00
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:
2023-12-16 11:10:10 +04:00
if (_startPosX + _shipWidth + EntityShip.Step < _pictureWidth)
2023-11-25 12:44:21 +04:00
{
_startPosX += (int)EntityShip.Step;
}
break;
case DirectionType.Down:
2023-12-16 11:10:10 +04:00
if (_startPosY + _shipHeight + EntityShip.Step < _pictureHeight)
2023-11-25 12:44:21 +04:00
{
_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 };
2023-12-16 11:10:10 +04:00
g.FillPolygon(bodyBrush, p);
2023-11-25 12:44:21 +04:00
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,
2023-12-16 11:10:10 +04:00
DirectionType.Right => _startPosX + _shipWidth + EntityShip.Step < _pictureWidth,
DirectionType.Down => _startPosY + _shipHeight + EntityShip.Step < _pictureHeight,
2023-11-25 12:44:21 +04:00
_ => false,
};
}
2023-12-16 11:10:10 +04:00
public IMoveableObject GetMoveableObject => new DrawingObjectShip(this);
2023-12-16 11:16:37 +04:00
public void ChangeBordersPicture(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
}
2023-11-25 12:44:21 +04:00
}
}