PIbd22_Kuznetsov_A.V._lab2 #3

Closed
Kuznetsov_Anatoliy wants to merge 2 commits from lab2 into lab1
14 changed files with 704 additions and 90 deletions

View File

@@ -0,0 +1,75 @@
using Excavator.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Excavator.DrawingObjects;
namespace Excavator.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
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;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(DirectionType.Left);
protected bool MoveRight() => MoveTo(DirectionType.Right);
protected bool MoveUp() => MoveTo(DirectionType.Up);
protected bool MoveDown() => MoveTo(DirectionType.Down);
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator
{
public enum DirectionType
Review

Это перечисление должно было быть прописано в рамках первой лабораторной

Это перечисление должно было быть прописано в рамках первой лабораторной
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}
}

View File

@@ -0,0 +1,49 @@
using Excavator.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excavator.Entities;
namespace Excavator
{
public class DrawingExcavator : DrawingMash
Review

Этот класс не должен быть новым

Этот класс не должен быть новым
{
public DrawingExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool Bucket, bool Supports, int width, int height) : base(speed, weight, bodyColor, width, height, 215, 95)
{
if (EntityMash != null)
{
EntityMash = new EntityExcavator(speed, weight, bodyColor, additionalColor, Bucket, Supports);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityMash is not EntityExcavator Excavator)
{
return;
}
Pen pen = new(Color.Black);
Pen additionalPen = new(Excavator.AdditionalColor);
Brush additionalBrush = new SolidBrush(Excavator.AdditionalColor);
base.DrawTransport(g);
if (Excavator.Bucket)
{
Brush additionalBrush2 = new
SolidBrush(EntityMash.BodyColor);
g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 42, 20, 5);
g.FillRectangle(additionalBrush, _startPosX, _startPosY + 37, 15, 50);
}
if (Excavator.Supports)
{
g.FillRectangle(additionalBrush, _startPosX + 170, _startPosY + 47, 35, 5);
g.FillRectangle(additionalBrush, _startPosX + 190, _startPosY + 27, 15, 45);
g.FillRectangle(additionalBrush, _startPosX + 185, _startPosY + 72, 30, 5);
}
}
}
}

138
Excavator/DrawingMahs.cs Normal file
View File

@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excavator.Entities;
namespace Excavator.DrawingObjects
{
public class DrawingMash
{
public EntityMash? EntityMash { get; protected set; }
public int _pictureWidth;
public int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _mashWidth = 150;
protected readonly int _mashHeight = 95;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _mashWidth;
public int GetHeight => _mashHeight;
public DrawingMash(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _mashWidth || height < _mashHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityMash = new EntityMash(speed, weight, bodyColor);
}
protected DrawingMash(int speed, double weight, Color bodyColor, int width, int height, int mashWidth, int mashHeight)
{
if (width < _mashWidth || height < _mashHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_mashWidth = mashWidth;
_mashHeight = mashHeight;
EntityMash = new EntityMash(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x > _pictureWidth || y > _pictureHeight)
{
return;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityMash == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityMash.Step > -50)
{
_startPosX -= (int)EntityMash.Step;
}
break;
case DirectionType.Up:
if (_startPosY - EntityMash.Step > -35)
{
_startPosY -= (int)EntityMash.Step;
}
break;
case DirectionType.Right:
if (_startPosX + _mashWidth + EntityMash.Step < _pictureWidth + 14)
{
_startPosX += (int)EntityMash.Step;
}
break;
case DirectionType.Down:
if (_startPosY + _mashHeight + EntityMash.Step < _pictureHeight)
{
_startPosY += (int)EntityMash.Step;
}
break;
}
}
public virtual void DrawTransport(Graphics g)
{
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityMash.BodyColor);
g.FillRectangle(additionalBrush, _startPosX + 30, _startPosY + 31, 140, 35);
g.FillRectangle(additionalBrush, _startPosX + 60, _startPosY + 2, 15, 35);
g.FillRectangle(additionalBrush, _startPosX + 125, _startPosY + 2, 40, 40);
Brush additionalBrush1 = new
SolidBrush(EntityMash.BodyColor);
Brush gr = new SolidBrush(Color.Gray);
g.FillEllipse(additionalBrush1, _startPosX + 20, _startPosY + 72, 25, 25);
g.FillEllipse(additionalBrush1, _startPosX + 145, _startPosY + 72, 25, 25);
g.FillEllipse(gr, _startPosX + 55, _startPosY + 81, 17, 17);
g.FillEllipse(gr, _startPosX + 85, _startPosY + 81, 17, 17);
g.FillEllipse(gr, _startPosX + 110, _startPosY + 81, 17, 17);
g.FillEllipse(gr, _startPosX + 68, _startPosY + 72, 8, 8);
g.FillEllipse(gr, _startPosX + 95, _startPosY + 72, 8, 8);
Pen blackPen = new Pen(Color.Black, 3);
Rectangle rect1 = new Rectangle(_startPosX + 15, _startPosY + 69, 30, 30);
g.DrawArc(blackPen, rect1, 90, 180);
Rectangle rect2 = new Rectangle(_startPosX + 145, _startPosY + 69, 30, 30);
g.DrawArc(blackPen, rect2, -90, 180);
g.DrawLine(blackPen, _startPosX + 27, _startPosY + 69, _startPosX + 165, _startPosY + 69);
g.DrawLine(blackPen, _startPosX + 27, _startPosY + 99, _startPosX + 165, _startPosY + 99);
}
public bool CanMove(DirectionType direction)
{
if (EntityMash == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityMash.Step > 0,
DirectionType.Up => _startPosY - EntityMash.Step > 0,
DirectionType.Right => _startPosX + _mashWidth + EntityMash.Step < _pictureWidth,
DirectionType.Down => _startPosY + _mashHeight + EntityMash.Step < _pictureHeight,
_ => false,
};
}
}
}

View File

@@ -0,0 +1,36 @@
using Excavator.MovementStrategy;
using Excavator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Excavator.DrawingObjects;
namespace Excavator.MovementStrategy
{
public class DrawingObjectMash : IMoveableObject
{
private readonly DrawingMash? _DrawingMash = null;
public DrawingObjectMash(DrawingMash DrawingMash)
{
_DrawingMash = DrawingMash;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_DrawingMash == null || _DrawingMash.EntityMash == null)
{
return null;
}
return new ObjectParameters(_DrawingMash.GetPosX,_DrawingMash.GetPosY, _DrawingMash.GetWidth, _DrawingMash.GetHeight);
}
}
public int GetStep => (int)(_DrawingMash?.EntityMash?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _DrawingMash?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _DrawingMash?.MoveTransport(direction);
}
}

View File

@@ -0,0 +1,22 @@
using Excavator.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.Entities
{
public class EntityExcavator : EntityMash
{
public Color AdditionalColor { get; private set; }
public bool Bucket { get; private set; }
public bool Supports { get; private set; }
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Bucket = bucket;
Supports = supports;
}
}
}

22
Excavator/EntityMash.cs Normal file
View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.Entities
{
public class EntityMash
{
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;
public EntityMash(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@@ -2,15 +2,7 @@
{ {
partial class FormExcavator partial class FormExcavator
{ {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (disposing && (components != null)) if (disposing && (components != null))
@@ -28,111 +20,153 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormExcavator)); this.pictureBoxExcavator = new System.Windows.Forms.PictureBox();
this.pictureBoxExc = new System.Windows.Forms.PictureBox(); this.buttonCreateExcavator = new System.Windows.Forms.Button();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxExc)).BeginInit(); this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.buttonStep = new System.Windows.Forms.Button();
this.buttonCreateMash = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxExcavator)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxBus // pictureBoxExcavator
// //
this.pictureBoxExc.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxExcavator.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxExc.Location = new System.Drawing.Point(0, 0); this.pictureBoxExcavator.Location = new System.Drawing.Point(0, 0);
this.pictureBoxExc.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.pictureBoxExcavator.Name = "pictureBoxExcavator";
this.pictureBoxExc.Name = "pictureBoxBus"; this.pictureBoxExcavator.Size = new System.Drawing.Size(882, 453);
this.pictureBoxExc.Size = new System.Drawing.Size(1010, 615); this.pictureBoxExcavator.TabIndex = 5;
this.pictureBoxExc.TabIndex = 1; this.pictureBoxExcavator.TabStop = false;
this.pictureBoxExc.TabStop = false;
// //
// buttonCreate // buttonCreateExcavator
// //
this.buttonCreate.Location = new System.Drawing.Point(14, 541); this.buttonCreateExcavator.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonCreateExcavator.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreateExcavator.Location = new System.Drawing.Point(11, 365);
this.buttonCreate.Size = new System.Drawing.Size(95, 57); this.buttonCreateExcavator.Name = "buttonCreateExcavator";
this.buttonCreate.TabIndex = 2; this.buttonCreateExcavator.Size = new System.Drawing.Size(120, 73);
this.buttonCreate.Text = "Создать"; this.buttonCreateExcavator.TabIndex = 6;
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreateExcavator.Text = "Создать Экскаватор";
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); this.buttonCreateExcavator.UseVisualStyleBackColor = true;
this.buttonCreateExcavator.Click += new System.EventHandler(this.buttonCreateExcavator_Click);
// //
// buttonUp // buttonUp
// //
this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage"))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Excavator.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(896, 487); this.buttonUp.Location = new System.Drawing.Point(811, 377);
this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(46, 53); this.buttonUp.Size = new System.Drawing.Size(30, 29);
this.buttonUp.TabIndex = 3; this.buttonUp.TabIndex = 7;
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
//
// buttonDown
//
this.buttonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonDown.BackgroundImage")));
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(896, 548);
this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(46, 53);
this.buttonDown.TabIndex = 4;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// buttonLeft // buttonLeft
// //
this.buttonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonLeft.BackgroundImage"))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Excavator.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(843, 547); this.buttonLeft.Location = new System.Drawing.Point(776, 413);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(46, 53); this.buttonLeft.Size = new System.Drawing.Size(30, 29);
this.buttonLeft.TabIndex = 5; this.buttonLeft.TabIndex = 8;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonLeft.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::Excavator.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(811, 413);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 29);
this.buttonDown.TabIndex = 9;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
// //
// buttonRight // buttonRight
// //
this.buttonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRight.BackgroundImage"))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Excavator.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(949, 547); this.buttonRight.Location = new System.Drawing.Point(848, 413);
this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(46, 53); this.buttonRight.Size = new System.Drawing.Size(30, 29);
this.buttonRight.TabIndex = 6; this.buttonRight.TabIndex = 10;
this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonRight.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[] {
"В центр",
"В правый нижний угол"});
this.comboBoxStrategy.Location = new System.Drawing.Point(719, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28);
this.comboBoxStrategy.TabIndex = 11;
//
// 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(747, 45);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(94, 29);
this.buttonStep.TabIndex = 12;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// buttonCreateMash
//
this.buttonCreateMash.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateMash.Location = new System.Drawing.Point(138, 365);
this.buttonCreateMash.Name = "buttonCreateMash";
this.buttonCreateMash.Size = new System.Drawing.Size(120, 74);
this.buttonCreateMash.TabIndex = 13;
this.buttonCreateMash.Text = "Создать Гусеничную машину";
this.buttonCreateMash.UseVisualStyleBackColor = true;
this.buttonCreateMash.Click += new System.EventHandler(this.buttonCreateMash_Click);
// //
// FormExcavator // FormExcavator
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1010, 615); this.ClientSize = new System.Drawing.Size(882, 453);
this.Controls.Add(this.buttonCreateMash);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonDown); this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreateExcavator);
this.Controls.Add(this.pictureBoxExc); this.Controls.Add(this.pictureBoxExcavator);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormExcavator"; this.Name = "FormExcavator";
this.Text = "Bus"; this.Text = "Excavator";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxExc)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxExcavator)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion #endregion
private PictureBox pictureBoxExc; private PictureBox pictureBoxExcavator;
private Button buttonCreate; private Button buttonCreateExcavator;
private Button buttonUp; private Button buttonUp;
private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private ComboBox comboBoxStrategy;
private Button buttonStep;
private Button buttonCreateMash;
} }
} }

View File

@@ -1,43 +1,56 @@
using Excavator.DrawingObjects;
using Excavator.MovementStrategy;
namespace Excavator namespace Excavator
{ {
public partial class FormExcavator : Form public partial class FormExcavator : Form
{ {
private DrawingExc? _drawingExc; private DrawingMash? _DrawingMash;
private AbstractStrategy? _abstractStrategy;
public FormExcavator() public FormExcavator()
{ {
InitializeComponent(); InitializeComponent();
} }
private void Draw() private void Draw()
{ {
if (_drawingExc == null) if (_DrawingMash == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxExc.Width, pictureBoxExc.Height); Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingExc.DrawTransport(gr); _DrawingMash.DrawTransport(gr);
pictureBoxExc.Image = bmp; pictureBoxExcavator.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreateExcavator_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawingExc = new DrawingExc(); _DrawingMash = new DrawingExcavator(random.Next(100, 300),
EntityExcavator bus = new EntityExcavator();
bus.Init(random.Next(100, 300),
random.Next(1000, 3000), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); true,
_drawingExc.Init(bus, pictureBoxExc.Width, pictureBoxExc.Height); Convert.ToBoolean(random.Next(0, 2)),
_drawingExc.SetPosition(random.Next(10, 100), pictureBoxExcavator.Width, pictureBoxExcavator.Height);
random.Next(10, 100)); _DrawingMash.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw(); Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void buttonCreateMash_Click(object sender, EventArgs e)
{ {
if (_drawingExc == null) Random random = new();
_DrawingMash = new DrawingMash(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxExcavator.Width, pictureBoxExcavator.Height);
_DrawingMash.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_DrawingMash == null)
{ {
return; return;
} }
@@ -45,19 +58,53 @@ namespace Excavator
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawingExc.MoveTransport(Direction.Up); _DrawingMash.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawingExc.MoveTransport(Direction.Down); _DrawingMash.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawingExc.MoveTransport(Direction.Left); _DrawingMash.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawingExc.MoveTransport(Direction.Right); _DrawingMash.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); Draw();
} }
private void buttonStep_Click(object sender, EventArgs e)
{
if (_DrawingMash == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectMash(_DrawingMash), pictureBoxExcavator.Width, pictureBoxExcavator.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,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excavator.DrawingObjects;
namespace Excavator.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

56
Excavator/MoveToBorder.cs Normal file
View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.MovementStrategy
{
internal 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)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

56
Excavator/MoveToCenter.cs Normal file
View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.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,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Excavator.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
public int LeftBorder => _x;
public int TopBorder => _y;
public int RightBorder => _x + _width;
public int DownBorder => _y + _height;
public int ObjectMiddleHorizontal => _x + _width / 2;
public int ObjectMiddleVertical => _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

15
Excavator/Status.cs Normal file
View File

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