Создание и наполнение классов EntityLocomotive, EntityElectricLocomotive, DrawningLocomotive, DrawingElectricLocomotive.

This commit is contained in:
Андрей Байгулов 2023-10-06 21:00:34 +04:00
parent 84a371c0cf
commit 5e1c2a5559
7 changed files with 221 additions and 157 deletions

View File

@ -3,96 +3,24 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ProjectElectricLocomotive.DrawningObjects;
using ProjectElectricLocomotive.Entities;
namespace ElectricLocomotive namespace ProjectElectricLocomotive.DrawingObjects
{ {
public class DrawningElectricLocomotive public class DrawingElectricLocomotive : DrawningLocomotive
{ {
public EntityElectricLocomotive? EntityElectricLocomotive { get; private set; } public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
private int _pictureWidth; bool pantograph, bool compartment, int width, int height) : base(speed, weight, bodyColor, width, height, 80, 52)
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _locomWidth = 80;
private readonly int _locomHeight = 52;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pantograph">Признак наличия токоприемника</param>
/// <param name="compartment">Признак наличия отсеков под электрические батареи</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, bool compartment, int width, int height)
{ {
if (width < _locomWidth || height < _locomHeight) if (EntityLocomotive != null)
{ {
return false; EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, pantograph, compartment);
}
_pictureWidth = width;
_pictureHeight = height;
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, pantograph, compartment);
return true;
}
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locomWidth > _pictureWidth)
{
x = _pictureWidth - _locomWidth;
}
if (y < 0 || y + _locomHeight > _pictureHeight)
{
y = _pictureHeight - _locomHeight;
}
_startPosX = x;
_startPosY = y;
}
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityElectricLocomotive == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityElectricLocomotive.Step > 0)
{
_startPosX -= (int)EntityElectricLocomotive.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityElectricLocomotive.Step > 0)
{
_startPosY -= (int)EntityElectricLocomotive.Step;
}
break;
//вправо
case Direction.Right:
if (_startPosX + EntityElectricLocomotive.Step + _locomWidth < _pictureWidth)
{
_startPosX += (int)EntityElectricLocomotive.Step;
}
break;
case Direction.Down:
if (_startPosY + EntityElectricLocomotive.Step + _locomHeight < _pictureHeight)
{
_startPosY += (int)EntityElectricLocomotive.Step;
}
break;
} }
} }
/// <param name="g"></param> public override void DrawTransport(Graphics g)
public void DrawTransport(Graphics g)
{ {
if (EntityElectricLocomotive == null) if (EntityLocomotive is not EntityElectricLocomotive electricLocomotive)
{ {
return; return;
} }
@ -100,33 +28,17 @@ namespace ElectricLocomotive
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black); Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue); Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(EntityElectricLocomotive.BodyColor); Brush bodyColor = new SolidBrush(electricLocomotive.BodyColor);
Brush additionalBrush = new SolidBrush(EntityElectricLocomotive.AdditionalColor); Brush additionalBrush = new SolidBrush(electricLocomotive.AdditionalColor);
if (EntityElectricLocomotive.Pantograph) if (electricLocomotive.Pantograph)
{ {
// Токоприемники
g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5); g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5);
g.DrawLine(pen, _startPosX + 30, _startPosY + 15, _startPosX + 50, _startPosY + 2); g.DrawLine(pen, _startPosX + 30, _startPosY + 15, _startPosX + 50, _startPosY + 2);
g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 60, _startPosY + 2); g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 60, _startPosY + 2);
} }
// Локомотив if (electricLocomotive.Compartment)
g.FillPolygon(bodyColor, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
if (EntityElectricLocomotive.Compartment)
{ {
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11); g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11);
g.FillPolygon(additionalBrush, new Point[] g.FillPolygon(additionalBrush, new Point[]
@ -139,35 +51,7 @@ namespace ElectricLocomotive
} }
); );
} }
base.DrawTransport(g);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 40),
new Point(_startPosX + 75, _startPosY + 45),
new Point(_startPosX + 5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
// Окна
g.FillEllipse(windows, _startPosX + 10, _startPosY + 24, 10, 10);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 25, 10, 10);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
// Колёса
// Локомотив
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10);
} }
} }
} }

View File

@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive;
using ProjectElectricLocomotive.Entities;
using ProjectElectricLocomotive.Properties;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectElectricLocomotive.DrawningObjects
{
public class DrawningLocomotive
{
public EntityLocomotive? EntityLocomotive { get; protected set; }
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _locomWidth = 80;
protected readonly int _locomHeight = 52;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="heigth">Высота картинки</param>
public DrawningLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
{
if (width < _locomWidth || heigth < _locomHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="locomWidth">Ширина картинки</param>
/// <param name="locomHeight">Высота картинки</param>
protected DrawningLocomotive(int speed, double weight, Color bodyColor, int width,
int height, int locomWidth, int locomHeight)
{
if (width < _locomWidth || height < _locomHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_locomWidth = locomWidth;
_locomHeight = locomHeight;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locomWidth > _pictureWidth)
{
x = _pictureWidth - _locomWidth;
}
if (y < 0 || y + _locomHeight > _pictureHeight)
{
y = _pictureHeight - _locomHeight;
}
_startPosX = x;
_startPosY = y;
}
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityLocomotive == null)
{
return;
}
switch (direction)
{
case Direction.Left:
if (_startPosX - EntityLocomotive.Step > 0)
{
_startPosX -= (int)EntityLocomotive.Step;
}
break;
case Direction.Up:
if (_startPosY - EntityLocomotive.Step > 0)
{
_startPosY -= (int)EntityLocomotive.Step;
}
break;
case Direction.Right:
if (_startPosX + EntityLocomotive.Step + _locomWidth < _pictureWidth)
{
_startPosX += (int)EntityLocomotive.Step;
}
break;
case Direction.Down:
if (_startPosY + EntityLocomotive.Step + _locomHeight < _pictureHeight)
{
_startPosY += (int)EntityLocomotive.Step;
}
break;
}
}
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
{
if (EntityLocomotive == null) return;
}
Pen pen = new(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(EntityLocomotive.BodyColor);
g.FillPolygon(bodyColor, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 40),
new Point(_startPosX + 75, _startPosY + 45),
new Point(_startPosX + 5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 40),
new Point(_startPosX + 75, _startPosY + 45),
new Point(_startPosX + 5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
g.FillEllipse(windows, _startPosX + 10, _startPosY + 24, 10, 10);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 25, 10, 10);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10);
}
}
}

View File

@ -1,31 +1,24 @@
using System; using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ElectricLocomotive namespace ProjectElectricLocomotive.Entities
{ {
public class EntityElectricLocomotive public class EntityElectricLocomotive : EntityLocomotive
{ {
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; } public Color AdditionalColor { get; private set; }
public bool Pantograph { get; private set; } public bool Pantograph { get; private set; }
public bool Compartment { get; private set; } public bool Compartment { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес локомотива</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param> /// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pantograph">Признак наличия токоприемника</param> /// <param name="pantograph">Признак наличия токоприемника</param>
/// <param name="compartment">Признак наличия отсеков под электрические батареи</param> /// <param name="compartment">Признак наличия отсеков под электрические батареи</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, bool compartment) public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph,
bool compartment) : base(speed, weight, bodyColor)
{ {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor; AdditionalColor = additionalColor;
Pantograph = pantograph; Pantograph = pantograph;
Compartment = compartment; Compartment = compartment;

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Entities
{
public class EntityLocomotive
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес локомотива</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityLocomotive (int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -8,7 +8,7 @@
// </auto-generated> // </auto-generated>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
namespace ElectricLocomotive.Properties { namespace ProjectElectricLocomotive.Properties {
using System; using System;
@ -39,7 +39,7 @@ namespace ElectricLocomotive.Properties {
internal static global::System.Resources.ResourceManager ResourceManager { internal static global::System.Resources.ResourceManager ResourceManager {
get { get {
if (object.ReferenceEquals(resourceMan, null)) { if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ElectricLocomotive.Properties.Resources", typeof(Resources).Assembly); global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProjectElectricLocomotive.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp; resourceMan = temp;
} }
return resourceMan; return resourceMan;

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279 VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElectricLocomotive", "ElectricLocomotive\ElectricLocomotive.csproj", "{A7A62158-ECC2-48DA-81F7-565BB5CE1E0D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectElectricLocomotive", "ElectricLocomotive\ProjectElectricLocomotive.csproj", "{A7A62158-ECC2-48DA-81F7-565BB5CE1E0D}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution