Добавление родитилей и ввод консрукторов

This commit is contained in:
Samecoins 2024-04-01 09:54:35 +04:00
parent 8b3ca3dcd7
commit 33d663adfe
7 changed files with 224 additions and 121 deletions

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck;
namespace ProjectDumpTruck.Drawnings;
/// <summary>
/// Направление перемещения

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectDumpTruck.Entities;
namespace ProjectDumpTruck.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningDumpTruck : DrawningTruck
{
/// <summary>
/// Консруктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodywork">Признак наличия кузова</param>
/// <param name="awning">Признак наличия тента</param>
public DrawningDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodywork, bool awning) : base(130, 90)
{
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, bodywork, awning);
}
public override void DrawTransport(Graphics g)
{
if (EntityTruck == null || EntityTruck is not EntityDumpTruck dumpTruck || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(dumpTruck.AdditionalColor);
Brush border = new SolidBrush(Color.Black);
//Отрисовка кузова
if (dumpTruck.Bodywork)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value, 90, 35);
}
_startPosX += 10;
_startPosY += 20;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 20;
//Отрисовка тента
if (dumpTruck.Bodywork & dumpTruck.Awning)
{
g.FillRectangle(border, _startPosX.Value, _startPosY.Value, 95, 10);
g.FillRectangle(border, _startPosX.Value, _startPosY.Value, 95, 3);
g.FillRectangle(border, _startPosX.Value + 30, _startPosY.Value, 3, 40);
g.FillRectangle(border, _startPosX.Value + 70, _startPosY.Value, 3, 40);
}
}
}

View File

@ -1,20 +1,18 @@
using System;
using ProjectDumpTruck.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck;
namespace ProjectDumpTruck.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningDumpTruck
public class DrawningTruck
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityDumpTruck? EntityDumpTruck { get; private set; }
public EntityTruck? EntityTruck { get; protected set; }
/// <summary>
/// Ширина окна
@ -29,48 +27,58 @@ public class DrawningDumpTruck
/// <summary>
/// Левая координата прорисовки самосвала
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки самосвала
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки самосвала
/// </summary>
private readonly int _drawningDumpTruckWidth = 130;
private readonly int _drawningDumpTruckWidth = 90;
/// <summary>
/// Высота прорисовки самосвала
/// </summary>
private readonly int _drawningDumpTruckHeight = 90;
private readonly int _drawningDumpTruckHeight = 50;
/// <summary>
/// Инициализация свойств
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodywork">Признак наличия кузова</param>
/// <param name="awning">Признак наличия тента</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodywork, bool awning)
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningTruck()
{
EntityDumpTruck = new EntityDumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, bodywork, awning);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(int speed, double weight, Color bodyColor) : this()
{
EntityTruck = new EntityTruck(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningDumpTruckWidth">Ширина прорисовки самосвала</param>
/// <param name="drawningDumpTruckHeight">Высота прорисовки самосвала</param>
protected DrawningTruck(int drawningDumpTruckWidth, int drawningDumpTruckHeight) : this()
{
_drawningDumpTruckWidth = drawningDumpTruckWidth;
_pictureHeight = drawningDumpTruckHeight;
}
/// <summary>
/// Установка границ поля
@ -90,7 +98,7 @@ public class DrawningDumpTruck
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue && _startPosX.Value + _drawningDumpTruckWidth > _pictureWidth)
if (_startPosX.HasValue && _startPosX.Value + _drawningDumpTruckWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningDumpTruckWidth;
}
@ -145,7 +153,7 @@ public class DrawningDumpTruck
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityDumpTruck == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTruck == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -154,33 +162,33 @@ public class DrawningDumpTruck
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityDumpTruck.Step > 0)
if (_startPosX.Value - EntityTruck.Step > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
_startPosX -= (int)EntityTruck.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityDumpTruck.Step > 0)
if (_startPosY.Value - EntityTruck.Step > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
_startPosY -= (int)EntityTruck.Step;
}
return true;
//вправо
case DirectionType.Right:
if (_startPosX.Value + EntityDumpTruck.Step + _drawningDumpTruckWidth < _pictureWidth)
if (_startPosX.Value + EntityTruck.Step + _drawningDumpTruckWidth < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
_startPosX += (int)EntityTruck.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityDumpTruck.Step + _drawningDumpTruckHeight < _pictureHeight)
if (_startPosY.Value + EntityTruck.Step + _drawningDumpTruckHeight < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
_startPosY += (int)EntityTruck.Step;
}
return true;
default:
@ -189,49 +197,33 @@ public class DrawningDumpTruck
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityDumpTruck == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTruck == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityDumpTruck.AdditionalColor);
//Отрисовка основы (кабины водителя и днища)
Brush body = new SolidBrush(EntityDumpTruck.BodyColor);
g.FillRectangle(body, _startPosX.Value + 100, _startPosY.Value, 30, 35);
g.FillRectangle(body, _startPosX.Value, _startPosY.Value + 40, 130, 20);
Brush body = new SolidBrush(EntityTruck.BodyColor);
g.FillRectangle(body, _startPosX.Value + 60, _startPosY.Value + 5, 20, 20); // Уменьшенная кабина
g.FillRectangle(body, _startPosX.Value, _startPosY.Value + 25, 90, 15); // Уменьшенное днище
//Отрисовка колёс
Brush wheels = new SolidBrush(Color.Gray);
g.FillEllipse(wheels, _startPosX.Value, _startPosY.Value + 60, 30, 30);
g.FillEllipse(wheels, _startPosX.Value + 30, _startPosY.Value + 60, 30, 30);
g.FillEllipse(wheels, _startPosX.Value + 100, _startPosY.Value + 60, 30, 30);
g.FillEllipse(wheels, _startPosX.Value + 5, _startPosY.Value + 35, 20, 20); // Уменьшенные колеса
g.FillEllipse(wheels, _startPosX.Value + 35, _startPosY.Value + 35, 20, 20);
g.FillEllipse(wheels, _startPosX.Value + 65, _startPosY.Value + 35, 20, 20);
//Отрисовка границ
Brush border = new SolidBrush(Color.Black);
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value, 30, 35);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 40, 130, 20);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 60, 30, 30);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 60, 30, 30);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 60, 30, 30);
//Отрисовка кузова
if (EntityDumpTruck.Bodywork)
{
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value, 90, 35);
}
//Отрисовка тента
if (EntityDumpTruck.Bodywork & EntityDumpTruck.Awning)
{
g.FillRectangle(border, _startPosX.Value, _startPosY.Value, 95, 10);
g.FillRectangle(border, _startPosX.Value, _startPosY.Value, 95, 3);
g.FillRectangle(border, _startPosX.Value + 30, _startPosY.Value, 3, 40);
g.FillRectangle(border, _startPosX.Value + 70, _startPosY.Value, 3, 40);
}
g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value + 5, 20, 20); // Граница кабины
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 25, 90, 15); // Граница днища
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 35, 20, 20); // Границы колёс
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 35, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 35, 20, 20);
}
}
}

View File

@ -1,31 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck;
namespace ProjectDumpTruck.Entities;
/// <summary>
/// Класс-сущность "Самосвал"
/// </summary>
public class EntityDumpTruck
public class EntityDumpTruck : EntityTruck
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
@ -41,11 +20,6 @@ public class EntityDumpTruck
/// </summary>
public bool Awning { get; private set; }
/// <summary>
/// Шаг перемещения самосвала
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса самосвала
/// </summary>
@ -56,15 +30,10 @@ public class EntityDumpTruck
/// <param name="bodywork">Признак наличия кузова</param>
/// <param name="awning">Признак наличия тента</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodywork, bool awning)
public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, bool bodywork, bool awning) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Bodywork = bodywork;
Awning = awning;
}
}

View File

@ -0,0 +1,41 @@
/// <summary>
/// Класс-сущность "Тележка"
/// </summary>
namespace ProjectDumpTruck.Entities;
public class EntityTruck
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения самосвала
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Конструктор сущности
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityTruck(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -34,6 +34,7 @@
buttonDown = new Button();
buttonRight = new Button();
buttonCreateDumpTruck = new Button();
buttonCreateTruck = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit();
SuspendLayout();
//
@ -99,17 +100,29 @@
buttonCreateDumpTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateDumpTruck.Location = new Point(12, 498);
buttonCreateDumpTruck.Name = "buttonCreateDumpTruck";
buttonCreateDumpTruck.Size = new Size(75, 23);
buttonCreateDumpTruck.Size = new Size(117, 23);
buttonCreateDumpTruck.TabIndex = 5;
buttonCreateDumpTruck.Text = "Создать";
buttonCreateDumpTruck.Text = "Создать самосвал";
buttonCreateDumpTruck.UseVisualStyleBackColor = true;
buttonCreateDumpTruck.Click += ButtonCreateDumpTruck_Click;
//
// buttonCreateTruck
//
buttonCreateTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTruck.Location = new Point(135, 498);
buttonCreateTruck.Name = "buttonCreateTruck";
buttonCreateTruck.Size = new Size(117, 23);
buttonCreateTruck.TabIndex = 6;
buttonCreateTruck.Text = "Создать тележка";
buttonCreateTruck.UseVisualStyleBackColor = true;
buttonCreateTruck.Click += ButtonCreateTruck_Click;
//
// FormTransport
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1017, 533);
Controls.Add(buttonCreateTruck);
Controls.Add(buttonCreateDumpTruck);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
@ -130,5 +143,6 @@
private Button buttonDown;
private Button buttonRight;
private Button buttonCreateDumpTruck;
private Button buttonCreateTruck;
}
}

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectDumpTruck.Drawnings;
/// <summary>
/// Форма работы с объектом "Самосвал"
@ -18,7 +19,7 @@ namespace ProjectDumpTruck
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningDumpTruck? _drawningDumpTruck;
private DrawningTruck? _drawningTruck;
/// <summary>
/// Конструктор формы
@ -33,36 +34,58 @@ namespace ProjectDumpTruck
/// </summary>
private void Draw()
{
if (_drawningDumpTruck == null)
if (_drawningTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningDumpTruck.DrawTransport(gr);
_drawningTruck.DrawTransport(gr);
pictureBoxDumpTruck.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningTruck):
_drawningTruck = new DrawningTruck(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawningDumpTruck):
_drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
_drawningTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать самосвал"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e)
{
Random random = new();
_drawningDumpTruck = new DrawningDumpTruck();
_drawningDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck));
_drawningDumpTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawningDumpTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать тележка"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck));
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
@ -71,7 +94,7 @@ namespace ProjectDumpTruck
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningDumpTruck == null)
if (_drawningTruck == null)
{
return;
}
@ -82,16 +105,16 @@ namespace ProjectDumpTruck
switch (name)
{
case "buttonUp":
result = _drawningDumpTruck.MoveTransport(DirectionType.Up);
result = _drawningTruck.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningDumpTruck.MoveTransport(DirectionType.Down);
result = _drawningTruck.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningDumpTruck.MoveTransport(DirectionType.Left);
result = _drawningTruck.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningDumpTruck.MoveTransport(DirectionType.Right);
result = _drawningTruck.MoveTransport(DirectionType.Right);
break;
}
if (result)
@ -100,5 +123,6 @@ namespace ProjectDumpTruck
}
}
}
}