119 lines
4.3 KiB
C#
119 lines
4.3 KiB
C#
using ProjectTank.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing.Drawing2D;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ProjectTank.MovementStrategy;
|
|
|
|
namespace ProjectTank.DrawningObjects
|
|
{
|
|
public class DrawningTankBase
|
|
{
|
|
public EntityTankBase? EntityTankBase { get; protected set; }
|
|
private int pictureWidth;
|
|
private int pictureHeight;
|
|
protected int startPosX;
|
|
protected int startPosY;
|
|
protected readonly int tankWidth = 145;
|
|
protected readonly int tankHeight = 95;
|
|
|
|
public int GetPosX => startPosX;
|
|
public int GetPosY => startPosY;
|
|
public int GetWidth => tankWidth;
|
|
public int GetHeight => tankHeight;
|
|
public IMoveableObject GetMoveableObject => new DrawningObjectTank(this);
|
|
public DrawningTankBase(int speed, int weight, Color bodyColor, int width, int height)
|
|
{
|
|
if (width <= tankWidth || height <= tankHeight) return;
|
|
|
|
pictureWidth = width;
|
|
pictureHeight = height;
|
|
EntityTankBase = new EntityTankBase(speed, weight, bodyColor);
|
|
}
|
|
public bool CanMove(DirectionType direction)
|
|
{
|
|
if (EntityTankBase == null) return false;
|
|
|
|
return direction switch
|
|
{
|
|
DirectionType.Left => startPosX - EntityTankBase.Step > 0,
|
|
DirectionType.Up => startPosY - EntityTankBase.Step > 0,
|
|
DirectionType.Right => startPosX + tankWidth + EntityTankBase.Step < pictureWidth,
|
|
DirectionType.Down => startPosY + tankHeight + EntityTankBase.Step < pictureHeight,
|
|
_ => false,
|
|
};
|
|
}
|
|
public void SetPosition(int x, int y)
|
|
{
|
|
if (EntityTankBase == null) return;
|
|
startPosX = x;
|
|
startPosY = y;
|
|
|
|
if (x + tankWidth > pictureWidth || y + tankHeight > pictureHeight)
|
|
{
|
|
startPosX = 1;
|
|
startPosY = 1;
|
|
}
|
|
}
|
|
public void MoveTransport(DirectionType direction)
|
|
{
|
|
if (!CanMove(direction) || EntityTankBase == null) return;
|
|
|
|
switch (direction)
|
|
{
|
|
//влево
|
|
case DirectionType.Left:
|
|
startPosX -= (int)EntityTankBase.Step;
|
|
break;
|
|
//вверх
|
|
case DirectionType.Up:
|
|
startPosY -= (int)EntityTankBase.Step;
|
|
break;
|
|
// вправо
|
|
case DirectionType.Right:
|
|
startPosX += (int)EntityTankBase.Step;
|
|
break;
|
|
//вниз
|
|
case DirectionType.Down:
|
|
startPosY += (int)EntityTankBase.Step;
|
|
break;
|
|
}
|
|
}
|
|
public virtual void DrawTransport(Graphics g)
|
|
{
|
|
if (EntityTankBase == null) return;
|
|
|
|
Pen pen = new(EntityTankBase.BodyColor);
|
|
|
|
// гусеница
|
|
GraphicsPath path = new GraphicsPath();
|
|
|
|
int cornerRadius = 20;
|
|
|
|
path.AddArc(startPosX + 1, startPosY + 65, cornerRadius, cornerRadius, 180, 90);
|
|
path.AddArc(startPosX + tankWidth - cornerRadius, startPosY + 65, cornerRadius, cornerRadius, 270, 90);
|
|
path.AddArc(startPosX + tankWidth - cornerRadius, startPosY + tankHeight - cornerRadius, cornerRadius, cornerRadius, 0, 90);
|
|
path.AddArc(startPosX + 1, startPosY + tankHeight - cornerRadius, cornerRadius, cornerRadius, 90, 90);
|
|
|
|
path.CloseFigure();
|
|
g.DrawPath(pen, path);
|
|
|
|
// колеса
|
|
g.DrawEllipse(pen, startPosX + 1, startPosY + tankHeight - 3 - 25, 25, 25);
|
|
for (int i = 1; i < 5; i++)
|
|
{
|
|
g.DrawEllipse(pen, startPosX + 30 + 5 * i + 15 * (i - 1), startPosY + tankHeight - 5 - 15, 15, 15);
|
|
}
|
|
g.DrawEllipse(pen, startPosX + tankWidth - 25 - 1, startPosY + tankHeight - 3 - 25, 25, 25);
|
|
|
|
// башня
|
|
SolidBrush brush = new SolidBrush(EntityTankBase.BodyColor);
|
|
g.FillRectangle(brush, startPosX + 45, startPosY + 30, 60, 26);
|
|
|
|
g.FillRectangle(brush, startPosX + 5, startPosY + 55 + 1, tankWidth - 10, 9);
|
|
}
|
|
}
|
|
} |