PIbd-21. Anisin R.S. Lab work 02 #2

Closed
RuslanAnisin wants to merge 1 commits from lab2 into lab1
16 changed files with 913 additions and 341 deletions
Showing only changes of commit 5b001c5b2f - Show all commits

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace DumpTruck
{
public enum Direction
public enum Direction
{
/// Вверх
/// </summary>

View File

@ -1,156 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
{
public class DrawingDumpTruck
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityDumpTruck? EntityDumpTruck { get; private set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки самосвала
/// </summary>
private int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки самосвала
/// </summary>
private int _startPosY;
/// <summary>
/// Ширина прорисовки самосвала
/// </summary>
private readonly int _truckWidth = 160;
/// <summary>
/// Высота прорисовки самосвала
/// </summary>
private readonly int _truckHeight = 90;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет самосвала</param>
/// <param name="tent">Признак наличия тента</param>
/// <param name="dumpBox">Признак наличия кузова</param>
/// <param name="dumpBoxColor">Цвет кузова</param>
/// <param name="tentColor">Цвет тента</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _truckHeight || _pictureWidth < _truckWidth) return false;
EntityDumpTruck = new EntityDumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; }
if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; }
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityDumpTruck == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityDumpTruck.Step > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityDumpTruck.Step > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
}
break;
// вправо
case Direction.Right:
if (_startPosX + _truckWidth + EntityDumpTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _truckHeight + EntityDumpTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityDumpTruck == null)
{
return;
}
Brush brush = new SolidBrush(EntityDumpTruck.BodyColor);
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
if (EntityDumpTruck.DumpBox)
{
Brush brDumpBox = new SolidBrush(EntityDumpTruck.DumpBoxColor);
Point point1 = new Point(_startPosX + 20, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 100, _startPosY + 39);
Point point4 = new Point(_startPosX, _startPosY + 39);
Point[] dumpBoxPoints = { point1, point2, point3, point4};
g.FillPolygon(brDumpBox, dumpBoxPoints);
}
if (EntityDumpTruck.DumpBox && EntityDumpTruck.Tent)
{
Brush brTent = new SolidBrush(EntityDumpTruck.TentColor);
Point point1 = new Point(_startPosX + 15, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 115, _startPosY + 10);
Point point4 = new Point(_startPosX + 10, _startPosY + 10);
Point[] tentPoints = { point1, point2, point3, point4 };
g.FillPolygon(brTent, tentPoints);
}
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck.DrawingObjects
{
public class DrawingDumpTruck : DrawingTruck
{
public DrawingDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height)
: base(speed, weight, bodyColor, width, height, 160, 90)
{
if (EntityTruck != null)
{
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityTruck is not EntityDumpTruck dumpTruck)
{
return;
}
base.DrawTransport(g);
if (dumpTruck.DumpBox)
{
Brush brDumpBox = new SolidBrush(dumpTruck.DumpBoxColor);
Point point1 = new Point(_startPosX + 20, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 100, _startPosY + 39);
Point point4 = new Point(_startPosX, _startPosY + 39);
Point[] dumpBoxPoints = { point1, point2, point3, point4 };
g.FillPolygon(brDumpBox, dumpBoxPoints);
}
if (dumpTruck.DumpBox && dumpTruck.Tent)
{
Brush brTent = new SolidBrush(dumpTruck.TentColor);
Point point1 = new Point(_startPosX + 15, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 115, _startPosY + 10);
Point point4 = new Point(_startPosX + 10, _startPosY + 10);
Point[] tentPoints = { point1, point2, point3, point4 };
g.FillPolygon(brTent, tentPoints);
}
}
}
}

View File

@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.Entities;
namespace DumpTruck.DrawingObjects
{
public class DrawingTruck
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTruck? EntityTruck { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки грузовика
/// </summary>
protected int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки грузовика
/// </summary>
protected int _startPosY;
/// <summary>
/// Ширина прорисовки грузовика
/// </summary>
protected readonly int _truckWidth = 160;
/// <summary>
/// Высота прорисовки грузовика
/// </summary>
protected readonly int _truckHeight = 90;
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _truckWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _truckHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _truckWidth || height < _truckHeight) return;
_pictureWidth = width;
_pictureHeight = height;
EntityTruck = new EntityTruck(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="truckWidth">Ширина прорисовки грузовика</param>
/// <param name="truckWidth">Высота прорисовки грузовика</param>
protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight)
{
if (width < truckWidth || height < truckHeight) return;
_pictureWidth = width;
_pictureHeight = height;
_truckWidth = truckWidth;
_truckHeight = truckHeight;
EntityTruck = new EntityTruck(speed, weight, bodyColor);
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _truckWidth > _pictureWidth) x = 0;
if (y < 0 || y + _truckHeight > _pictureHeight) y = 0;
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public bool CanMove(Direction direction)
{
if (EntityTruck == null)
{
return false;
}
return direction switch
{
//влево
Direction.Left => _startPosX - EntityTruck.Step > 0,
//вверх
Direction.Up => _startPosY - EntityTruck.Step > 0,
// вправо
Direction.Right => _startPosX + _truckWidth + EntityTruck.Step < _pictureWidth,
//вниз
Direction.Down => _startPosY + _truckHeight + EntityTruck.Step < _pictureHeight,
_ => false,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityTruck == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityTruck.Step > 0)
{
_startPosX -= (int)EntityTruck.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityTruck.Step > 0)
{
_startPosY -= (int)EntityTruck.Step;
}
break;
// вправо
case Direction.Right:
if (_startPosX + _truckWidth + EntityTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityTruck.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _truckHeight + EntityTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityTruck == null)
{
return;
}
Brush brush = new SolidBrush(EntityTruck.BodyColor);
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.Entities
{
public class EntityDumpTruck : EntityTruck
{
/// <summary>
/// Признак (опция) наличия тента
/// </summary>
public bool Tent { get; private set; }
/// <summary>
/// Признак (опция) наличия кузова
/// </summary>
public bool DumpBox { get; private set; }
/// <summary>
/// Цвет кузова
/// </summary>
public Color DumpBoxColor { get; private set; }
/// <summary>
/// Цвет тента
/// </summary>
public Color TentColor { get; private set; }
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самосвала</param>
/// <param name="bodyColor">Основной цвет самосвала</param>
/// <param name="tent">Признак наличия тента</param>
/// <param name="dumpBox">Признак наличия кузова</param>
/// <param name="dumpBoxColor">Цвет кузова</param>
/// <param name="tentColor">Цвет тента</param>
public EntityDumpTruck(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor) : base(speed, weight, bodyColor)
{
Tent = tent;
DumpBox = dumpBox;
DumpBoxColor = dumpBoxColor;
TentColor = tentColor;
}
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.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 => (double)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

@ -1,66 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
{
public class EntityDumpTruck
{
/// <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 bool Tent { get; private set; }
/// <summary>
/// Признак (опция) наличия кузова
/// </summary>
public bool DumpBox { get; private set; }
/// <summary>
/// Цвет кузова
/// </summary>
public Color DumpBoxColor { get; private set; }
/// <summary>
/// Цвет тента
/// </summary>
public Color TentColor { get; private set; }
/// <summary>
/// Шаг
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса самосвала
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самосвала</param>
/// <param name="bodyColor">Основной цвет самосвала</param>
/// <param name="tent">Признак наличия тента</param>
/// <param name="dumpBox">Признак наличия кузова</param>
/// <param name="dumpBoxColor">Цвет кузова</param>
/// <param name="tentColor">Цвет тента</param>
public void Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Tent = tent;
DumpBox = dumpBox;
DumpBoxColor = dumpBoxColor;
TentColor = tentColor;
}
}
}

View File

@ -28,107 +28,152 @@
/// </summary>
private void InitializeComponent()
{
pictureBoxDumpTruck = new PictureBox();
Create = new Button();
buttonLeft = new Button();
buttonUp = new Button();
buttonRight = new Button();
buttonDown = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit();
SuspendLayout();
//
// pictureBoxDumpTruck
//
pictureBoxDumpTruck.Dock = DockStyle.Fill;
pictureBoxDumpTruck.Location = new Point(0, 0);
pictureBoxDumpTruck.Name = "pictureBoxDumpTruck";
pictureBoxDumpTruck.Size = new Size(800, 450);
pictureBoxDumpTruck.TabIndex = 0;
pictureBoxDumpTruck.TabStop = false;
//
// Create
//
Create.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
Create.Location = new Point(12, 415);
Create.Name = "Create";
Create.Size = new Size(75, 23);
Create.TabIndex = 1;
Create.Text = "Создать";
Create.UseVisualStyleBackColor = true;
Create.Click += Create_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(686, 408);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(722, 372);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(758, 408);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(722, 408);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
//
// FormDumpTruck
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonDown);
Controls.Add(buttonRight);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(Create);
Controls.Add(pictureBoxDumpTruck);
Name = "FormDumpTruck";
Text = "FormDumpTruck";
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit();
ResumeLayout(false);
this.pictureBoxDumpTruck = new System.Windows.Forms.PictureBox();
this.ButtonCreateDumpTruck = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.ButtonCreateTruck = new System.Windows.Forms.Button();
this.ButtonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit();
this.SuspendLayout();
//
// pictureBoxDumpTruck
//
this.pictureBoxDumpTruck.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxDumpTruck.Location = new System.Drawing.Point(0, 0);
this.pictureBoxDumpTruck.Name = "pictureBoxDumpTruck";
this.pictureBoxDumpTruck.Size = new System.Drawing.Size(800, 450);
this.pictureBoxDumpTruck.TabIndex = 0;
this.pictureBoxDumpTruck.TabStop = false;
//
// ButtonCreateDumpTruck
//
this.ButtonCreateDumpTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateDumpTruck.Location = new System.Drawing.Point(12, 408);
this.ButtonCreateDumpTruck.Name = "ButtonCreateDumpTruck";
this.ButtonCreateDumpTruck.Size = new System.Drawing.Size(130, 30);
this.ButtonCreateDumpTruck.TabIndex = 1;
this.ButtonCreateDumpTruck.Text = "Создать самосвал";
this.ButtonCreateDumpTruck.UseVisualStyleBackColor = true;
this.ButtonCreateDumpTruck.Click += new System.EventHandler(this.ButtonCreateDumpTruck_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::DumpTruck.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(686, 408);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::DumpTruck.Properties.Resources.up;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(722, 372);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::DumpTruck.Properties.Resources.right;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(758, 408);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 4;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::DumpTruck.Properties.Resources.down;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(722, 408);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 5;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// comboBoxStrategy
//
this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"0",
"1"});
this.comboBoxStrategy.Location = new System.Drawing.Point(667, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23);
this.comboBoxStrategy.TabIndex = 6;
//
// ButtonCreateTruck
//
this.ButtonCreateTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateTruck.Location = new System.Drawing.Point(148, 408);
this.ButtonCreateTruck.Name = "ButtonCreateTruck";
this.ButtonCreateTruck.Size = new System.Drawing.Size(130, 30);
this.ButtonCreateTruck.TabIndex = 7;
this.ButtonCreateTruck.Text = "Создать грузовик";
this.ButtonCreateTruck.UseVisualStyleBackColor = true;
this.ButtonCreateTruck.Click += new System.EventHandler(this.ButtonCreateTruck_Click);
//
// ButtonStep
//
this.ButtonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonStep.Location = new System.Drawing.Point(713, 41);
this.ButtonStep.Name = "ButtonStep";
this.ButtonStep.Size = new System.Drawing.Size(75, 23);
this.ButtonStep.TabIndex = 8;
this.ButtonStep.Text = "Шаг";
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// FormDumpTruck
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.ButtonCreateTruck);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.ButtonCreateDumpTruck);
this.Controls.Add(this.pictureBoxDumpTruck);
this.Name = "FormDumpTruck";
this.Text = "FormDumpTruck";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).EndInit();
this.ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxDumpTruck;
private Button Create;
private Button ButtonCreateDumpTruck;
private Button buttonLeft;
private Button buttonUp;
private Button buttonRight;
private Button buttonDown;
}
private ComboBox comboBoxStrategy;
private Button ButtonCreateTruck;
private Button ButtonStep;
}
}

View File

@ -1,3 +1,6 @@
using DumpTruck.DrawingObjects;
using DumpTruck.MovementStrategy;
namespace DumpTruck
{
public partial class FormDumpTruck : Form
@ -7,39 +10,26 @@ namespace DumpTruck
InitializeComponent();
}
private DrawingDumpTruck? _drawingDumpTruck;
private DrawingTruck? _drawingTruck;
private AbstractStrategy? _abstractStrategy;
private void Draw()
{
if (_drawingDumpTruck == null)
if (_drawingTruck == null)
{
return;
}
Bitmap bmp = new(pictureBoxDumpTruck.Width,
pictureBoxDumpTruck.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingDumpTruck.DrawTransport(gr);
_drawingTruck.DrawTransport(gr);
pictureBoxDumpTruck.Image = bmp;
}
private void Create_Click(object sender, EventArgs e)
{
Random random = new();
_drawingDumpTruck = new DrawingDumpTruck();
_drawingDumpTruck.Init(random.Next(100, 300), random.Next(1000, 3000),
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)),
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)),
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawingDumpTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingDumpTruck == null)
if (_drawingTruck == null)
{
return;
}
@ -47,19 +37,78 @@ namespace DumpTruck
switch (name)
{
case "buttonUp":
_drawingDumpTruck.MoveTransport(Direction.Up);
_drawingTruck.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawingDumpTruck.MoveTransport(Direction.Down);
_drawingTruck.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawingDumpTruck.MoveTransport(Direction.Left);
_drawingTruck.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawingDumpTruck.MoveTransport(Direction.Right);
_drawingTruck.MoveTransport(Direction.Right);
break;
}
Draw();
}
}
private void ButtonCreateDumpTruck_Click(object sender, EventArgs e)
{
Random random = new();
_drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
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)),
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)),
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonCreateTruck_Click(object sender, EventArgs e)
{
Random random = new();
_drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height);
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width,
pictureBoxDumpTruck.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(Direction.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(Direction.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(Direction.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveDown() => MoveTo(Direction.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DumpTruck.DrawingObjects;
namespace DumpTruck.MovementStrategy
{
public class DrawningObjectTruck : IMoveableObject
{
private readonly DrawingTruck? _drawingTruck = null;
public DrawningObjectTruck(DrawingTruck drawningCar)
{
_drawingTruck = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawingTruck == null || _drawingTruck.EntityTruck ==
null)
{
return null;
}
return new ObjectParameters(_drawingTruck.GetPosX,
_drawingTruck.GetPosY, _drawingTruck.GetWidth, _drawingTruck.GetHeight);
}
}
public int GetStep => (int)(_drawingTruck?.EntityTruck?.Step ?? 0);
public bool CheckCanMove(Direction direction) =>
_drawingTruck?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) =>
_drawingTruck?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public interface IMoveableObject
{
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(Direction direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX < 0)
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY < 0)
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder => _x;
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder => _y;
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder => _x + _width;
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder => _y + _height;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal => _x + _width / 2;
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical => _y + _height / 2;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}