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

This commit is contained in:
Daria 2024-04-08 12:34:46 +04:00
parent e5b5315924
commit 206af8dee0
7 changed files with 354 additions and 163 deletions

View File

@ -1,4 +1,4 @@
namespace Excavator;
namespace Excavator.Drawnings;
/// <summary>
/// Направление перемещения

View File

@ -0,0 +1,118 @@
using Excavator.Entities;
using System.Drawing.Drawing2D;
using System.Net.Sockets;
namespace Excavator.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningExcavator : DrawningTrackedVehicle
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bucket">Признак наличия ковша</param>
/// <param name="supports">Признак наличия опор</param>
public DrawningExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) : base(150,126)
{
EntityTrackedVehicle = new EntityExcavator(speed, weight, bodyColor, additionalColor, bucket, supports);
}
public override void DrawTransport(Graphics g)
{
if (EntityTrackedVehicle == null || EntityTrackedVehicle is not EntityExcavator excavator || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(excavator.AdditionalColor);
Brush brBlue = new SolidBrush(Color.LightBlue);
Brush brGray = new SolidBrush(Color.Gray);
Brush brRed = new SolidBrush(Color.Red);
Brush brYellow = new SolidBrush(Color.Yellow);
Brush brBlack = new SolidBrush(Color.Black);
int bodyHeight = 90;
int cabineHeight = 40;
int bucketWidth = 30;
int wheelsHeight = 40;
Pen bPen = new(Color.Black);
bPen.Width = 3;
_startPosX += 20;
base.DrawTransport(g);
_startPosX -= 20;
// ковш
if (excavator.Bucket)
{
PointF p1 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight);
PointF p2 = new Point(_startPosX.Value, _startPosY.Value + cabineHeight);
PointF p3 = new Point(_startPosX.Value + bucketWidth , _startPosY.Value + cabineHeight + bodyHeight / 2);
g.DrawLine(pen, p1, p2);
g.DrawLine(pen, p2, p3);
g.DrawLine(pen, p3, p1);
g.FillPolygon(additionalBrush, [p1, p2, p3]);
}
// опоры
if (excavator.Supports)
{
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight, _startPosY.Value + cabineHeight + 10,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10);
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight +15, _startPosY.Value + cabineHeight + 10,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight);
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight,
_startPosX.Value + bucketWidth + bodyHeight + 30, _startPosY.Value + bodyHeight + wheelsHeight);
}
}
}

View File

@ -1,17 +1,20 @@
using System.Drawing.Drawing2D;
using System.Net.Sockets;
using Excavator.Entities;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator;
namespace Excavator.Drawnings;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningExcavator
public class DrawningTrackedVehicle
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityExcavator? EntityExcavator { get; private set; }
public EntityTrackedVehicle? EntityTrackedVehicle { get; protected set; }
/// <summary>
/// Ширина окна
@ -26,50 +29,73 @@ public class DrawningExcavator
/// <summary>
/// Левая координата прорисовки эскаватора
/// </summary>
private int? _startPosX;
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки экскаватора
/// </summary>
private int? _startPosY;
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки экскаватора
/// </summary>
private readonly int _drawningExcavatorWidth = 145;
private readonly int _drawningExcavatorWidth = 110;
/// <summary>
/// Высота прорисовки экскаватора
/// </summary>
private readonly int _drawningExcavatorHeight = 125;
private readonly int _drawningExcavatorHeight = 126;
/// <summary>
/// Инициализация свойств
/// Пустой конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bucket">Признак наличия ковша</param>
/// <param name="supports">Признак наличия опор</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports)
private DrawningTrackedVehicle()
{
EntityExcavator = new EntityExcavator();
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bucket, supports);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningTrackedVehicle(int speed, double weight, Color bodyColor) : this()
{
EntityTrackedVehicle = new EntityTrackedVehicle(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningExcavatorWidth">Ширина прорисовки гусеничной машины</param>
/// <param name="drawningExcavatorHeight">Высота прорисовки гусеничной машины</param>
protected DrawningTrackedVehicle(int drawningExcavatorWidth, int drawningExcavatorHeight) : this()
{
_drawningExcavatorWidth = drawningExcavatorWidth;
_pictureHeight = drawningExcavatorHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
@ -149,7 +175,7 @@ public class DrawningExcavator
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
@ -159,35 +185,35 @@ public class DrawningExcavator
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityExcavator.Step > 0)
if (_startPosX.Value - EntityTrackedVehicle.Step > 0)
{
_startPosX -= (int)EntityExcavator.Step;
_startPosX -= (int)EntityTrackedVehicle.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityExcavator.Step > 0)
if (_startPosY.Value - EntityTrackedVehicle.Step > 0)
{
_startPosY -= (int)EntityExcavator.Step;
_startPosY -= (int)EntityTrackedVehicle.Step;
}
return true;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX.Value + _drawningExcavatorWidth + EntityExcavator.Step < _pictureWidth)
if (_startPosX.Value + _drawningExcavatorWidth + EntityTrackedVehicle.Step < _pictureWidth)
{
_startPosX += (int)EntityExcavator.Step;
_startPosX += (int)EntityTrackedVehicle.Step;
}
return true;
//вниз
case DirectionType.Down:
//TODO прописать логику сдвига в вниз
if (_startPosY.Value + _drawningExcavatorHeight + EntityExcavator.Step < _pictureHeight)
if (_startPosY.Value + _drawningExcavatorHeight + EntityTrackedVehicle.Step < _pictureHeight)
{
_startPosY += (int)EntityExcavator.Step;
_startPosY += (int)EntityTrackedVehicle.Step;
}
return true;
default:
@ -239,27 +265,27 @@ public class DrawningExcavator
/// <param name="g"></param>
///
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor);
Brush brBlue = new SolidBrush(Color.LightBlue);
Brush brGray = new SolidBrush(Color.Gray);
Brush brRed = new SolidBrush(Color.Red);
Brush brYellow = new SolidBrush(Color.Yellow);
Brush brBlack = new SolidBrush(Color.Black);
int bodyHeight = 90;
int cabineHeight = 40;
int pipeHeight = 35;
int bucketWidth = 30;
int bucketWidth = 10;
int pipeWidth = 7;
int pipeOffsetX = 25;
@ -281,7 +307,7 @@ public class DrawningExcavator
g.FillRectangle(brBlue, _startPosX.Value + bucketWidth + cabineOffsetX, _startPosY.Value, cabineHeight, cabineHeight);
//трак
g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value + wheelsOffsetX, _startPosY.Value + bodyHeight, wheelsOffsetX + bodyHeight, wheelsHeight), 15));
g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value , _startPosY.Value + bodyHeight, wheelsOffsetX + bodyHeight, wheelsHeight), 15));
Pen bPen = new(Color.Black);
bPen.Width = 3;
@ -315,33 +341,10 @@ public class DrawningExcavator
g.FillEllipse(brYellow, _startPosX.Value + bucketWidth + wheelsOffsetX + 13, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
g.FillEllipse(brYellow, _startPosX.Value + bucketWidth + wheelsOffsetX + 28, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
// ковш
if (EntityExcavator.Bucket)
{
PointF p1 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight);
PointF p2 = new Point(_startPosX.Value, _startPosY.Value + cabineHeight);
PointF p3 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight + bodyHeight / 2);
g.DrawLine(pen, p1, p2);
g.DrawLine(pen, p2, p3);
g.DrawLine(pen, p3, p1);
g.FillPolygon(additionalBrush, [p1, p2, p3]);
}
// опоры
if (EntityExcavator.Supports)
{
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight, _startPosY.Value + cabineHeight + 10,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10);
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight);
g.DrawLine(bPen,
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight,
_startPosX.Value + bucketWidth + bodyHeight + 30, _startPosY.Value + bodyHeight + wheelsHeight);
}
}
}

View File

@ -1,26 +1,10 @@
namespace Excavator;
namespace Excavator.Entities;
/// <summary>
/// Класс-сущность "Экскаватор"
/// </summary>
public class EntityExcavator
public class EntityExcavator : EntityTrackedVehicle
{
public int m;
/// <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>
/// Дополнительный цвет (для опциональных элементов)
@ -45,20 +29,17 @@ public class EntityExcavator
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// Инициализация полей объекта-класса экскаватора
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="weight">Вес экскаватора</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bucket">Признак наличия ковша</param>
/// <param name="supports">Признак наличия опор</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports)
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) : base(speed, weight, bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Bucket = bucket;
Supports = supports;

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.Entities;
/// <summary>
/// Класс-сущность "Гусеничная машина"
/// </summary>
public class EntityTrackedVehicle
{
public int m;
/// <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 EntityTrackedVehicle(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -29,11 +29,12 @@
private void InitializeComponent()
{
pictureBoxExcavator = new PictureBox();
buttonCreate = new Button();
buttonCreateExcavator = new Button();
buttonLeft = new Button();
buttonRight = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonCreateTrackedVehicle = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit();
SuspendLayout();
//
@ -45,18 +46,17 @@
pictureBoxExcavator.Size = new Size(824, 407);
pictureBoxExcavator.TabIndex = 0;
pictureBoxExcavator.TabStop = false;
//
// buttonCreate
// buttonCreateExcavator
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 366);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
buttonCreateExcavator.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateExcavator.Location = new Point(12, 366);
buttonCreateExcavator.Name = "buttonCreateExcavator";
buttonCreateExcavator.Size = new Size(154, 29);
buttonCreateExcavator.TabIndex = 1;
buttonCreateExcavator.Text = "Создать экскаватор";
buttonCreateExcavator.UseVisualStyleBackColor = true;
buttonCreateExcavator.Click += buttonCreateExcavator_Click;
//
// buttonLeft
//
@ -106,20 +106,31 @@
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// buttonCreateTrackedVehicle
//
buttonCreateTrackedVehicle.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTrackedVehicle.Location = new Point(183, 366);
buttonCreateTrackedVehicle.Name = "buttonCreateTrackedVehicle";
buttonCreateTrackedVehicle.Size = new Size(238, 29);
buttonCreateTrackedVehicle.TabIndex = 6;
buttonCreateTrackedVehicle.Text = "Создать гусеничную машину";
buttonCreateTrackedVehicle.UseVisualStyleBackColor = true;
buttonCreateTrackedVehicle.Click += buttonCreateTrackedVehicle_Click;
//
// FormExcavator
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(824, 407);
Controls.Add(buttonCreateTrackedVehicle);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateExcavator);
Controls.Add(pictureBoxExcavator);
Name = "FormExcavator";
Text = "Экскаватор";
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).EndInit();
ResumeLayout(false);
}
@ -127,10 +138,11 @@
#endregion
private PictureBox pictureBoxExcavator;
private Button buttonCreate;
private Button buttonCreateExcavator;
private Button buttonLeft;
private Button buttonRight;
private Button buttonUp;
private Button buttonDown;
private Button buttonCreateTrackedVehicle;
}
}

View File

@ -1,4 +1,6 @@
namespace Excavator;
using Excavator.Drawnings;
namespace Excavator;
/// <summary>
/// Форма работы с объектом "Экскаватор"
@ -8,7 +10,7 @@ public partial class FormExcavator : Form
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private DrawningExcavator? _drawningExcavator;
private DrawningTrackedVehicle? _drawningTrackedVehicle;
/// <summary>
@ -24,76 +26,101 @@ public partial class FormExcavator : Form
private void Draw()
{
if(_drawningExcavator == null)
{
return;
}
Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningExcavator.DrawTransport(gr);
pictureBoxExcavator.Image = bmp;
if (_drawningTrackedVehicle == null)
{
return;
}
Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningTrackedVehicle.DrawTransport(gr);
pictureBoxExcavator.Image = bmp;
}
/// <summary>
/// Создание объекта класса-перемещения
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
{
Random random = new();
switch (type)
{
case nameof(DrawningTrackedVehicle):
_drawningTrackedVehicle = new DrawningTrackedVehicle(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(DrawningExcavator):
_drawningTrackedVehicle = new DrawningExcavator(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;
}
_drawningTrackedVehicle.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
_drawningTrackedVehicle.SetPosition(random.Next(0, 100), random.Next(0, 100));
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Обработка нажатия кнопки "Создать экскаватор"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreateExcavator_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningExcavator));
/// <summary>
/// Обработка нажатия кнопки "Создать гусеничную машину"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateTrackedVehicle_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrackedVehicle));
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
Random random = new();
_drawningExcavator = new DrawningExcavator();
_drawningExcavator.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)));
_drawningExcavator.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
_drawningExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100));
if (_drawningTrackedVehicle == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningExcavator == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false;
switch (name)
{
case "buttonUp":
result = _drawningExcavator.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = _drawningExcavator.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = _drawningExcavator.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = _drawningExcavator.MoveTransport(DirectionType.Right);
break;
}
if (result)
{
Draw();
}
}
}