131 lines
5.0 KiB
C#
131 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using RoadTrain.Entities;
|
|
using RoadTrain.MovementStrategy;
|
|
|
|
namespace RoadTrain.DrawingObjects
|
|
{
|
|
public class DrawingRoadTrain
|
|
{
|
|
public EntityRoadTrain? EntityRoadTrain { get; protected set; }
|
|
public int _pictureWidth;
|
|
public int _pictureHeight;
|
|
protected int _startPosX;
|
|
protected int _startPosY;
|
|
protected readonly int _roadTrainWidth = 200;
|
|
protected readonly int _roadTrainHeight = 100;
|
|
public DrawingRoadTrain(int speed, double weight, Color bodyColor, int width, int height)
|
|
{
|
|
if (width < _roadTrainWidth || height < _roadTrainHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
EntityRoadTrain = new EntityRoadTrain(speed, weight, bodyColor);
|
|
}
|
|
protected DrawingRoadTrain(int speed, double weight, Color bodyColor, int width, int height, int roadTrainWidth, int roadTrainHeight)
|
|
{
|
|
if (width <= _roadTrainWidth || height <= _roadTrainHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
_roadTrainWidth = roadTrainWidth;
|
|
_roadTrainHeight = roadTrainHeight;
|
|
EntityRoadTrain = new EntityRoadTrain(speed, weight, bodyColor);
|
|
}
|
|
public void SetPosition(int x, int y)
|
|
{
|
|
if (x < 0 || x + _roadTrainWidth > _pictureWidth)
|
|
{
|
|
x = 0;
|
|
}
|
|
if (y < 0 || y + _roadTrainHeight > _pictureHeight)
|
|
{
|
|
y = 0;
|
|
}
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
}
|
|
public virtual void DrawTransport(Graphics g)
|
|
{
|
|
if (EntityRoadTrain == null)
|
|
{
|
|
return;
|
|
}
|
|
Pen pen = new(Color.Black, 2);
|
|
Pen anchor = new(Color.Black, 4);
|
|
Brush bodyBrush = new SolidBrush(EntityRoadTrain.BodyColor);
|
|
Brush whiteBrush = new SolidBrush(Color.White);
|
|
|
|
//машина
|
|
g.DrawRectangle(pen, _startPosX, _startPosY + 50, 160, 20);
|
|
g.FillRectangle(bodyBrush, _startPosX, _startPosY + 50, 160, 20);
|
|
g.DrawEllipse(pen, _startPosX + 5, _startPosY + 70, 30, 30); //колесо
|
|
g.FillEllipse(bodyBrush, _startPosX + 5, _startPosY + 70, 30, 30);
|
|
g.DrawEllipse(pen, _startPosX + 40, _startPosY + 70, 30, 30); //колесо
|
|
g.FillEllipse(bodyBrush, _startPosX + 40, _startPosY + 70, 30, 30);
|
|
g.DrawEllipse(pen, _startPosX + 120, _startPosY + 70, 30, 30); //колесо
|
|
g.FillEllipse(bodyBrush, _startPosX + 120, _startPosY + 70, 30, 30);
|
|
g.DrawRectangle(pen, _startPosX + 120, _startPosY + 10, 40, 40); //кабина
|
|
g.FillRectangle(bodyBrush, _startPosX + 120, _startPosY + 10, 40, 40);
|
|
g.DrawRectangle(pen, _startPosX + 130, _startPosY + 20, 30, 20); //окно
|
|
g.FillRectangle(whiteBrush, _startPosX + 130, _startPosY + 20, 30, 20);
|
|
|
|
}
|
|
public int GetPosX => _startPosX;
|
|
public int GetPosY => _startPosY;
|
|
public int GetWidth => _roadTrainWidth;
|
|
public int GetHeight => _roadTrainHeight;
|
|
public bool CanMove(DirectionType direction)
|
|
{
|
|
if (EntityRoadTrain == null)
|
|
{
|
|
return false;
|
|
}
|
|
return direction switch
|
|
{
|
|
DirectionType.Left => _startPosX - EntityRoadTrain.Step > 0,
|
|
DirectionType.Up => _startPosY - EntityRoadTrain.Step > 0,
|
|
DirectionType.Right => _startPosX + EntityRoadTrain.Step + _roadTrainWidth < _pictureWidth,
|
|
DirectionType.Down => _startPosY + EntityRoadTrain.Step + _roadTrainHeight < _pictureHeight,
|
|
_ => false,
|
|
};
|
|
}
|
|
public void MoveTransport(DirectionType direction)
|
|
{
|
|
if (!CanMove(direction) || EntityRoadTrain == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
case DirectionType.Left:
|
|
_startPosX -= (int)EntityRoadTrain.Step;
|
|
break;
|
|
case DirectionType.Up:
|
|
_startPosY -= (int)EntityRoadTrain.Step;
|
|
break;
|
|
case DirectionType.Right:
|
|
_startPosX += (int)EntityRoadTrain.Step;
|
|
break;
|
|
case DirectionType.Down:
|
|
_startPosY += (int)EntityRoadTrain.Step;
|
|
break;
|
|
}
|
|
}
|
|
public IMoveableObject GetMoveableObject => new DrawingObjectTrain(this);
|
|
public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight)
|
|
|
|
{
|
|
_pictureWidth = pictureBoxWidth;
|
|
_pictureHeight = pictureBoxHeight;
|
|
}
|
|
|
|
}
|
|
} |