Лабораторная 2

vse cool
This commit is contained in:
Baryshev Dmitry 2024-03-15 00:32:50 +04:00
parent cd369bfe6e
commit 7402296ecd
8 changed files with 173 additions and 42 deletions

View File

@ -87,7 +87,6 @@ public class DrawningTruck
/// <param name="width">Ширина поля</param> /// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param> /// <param name="height">Высота поля</param>
public bool SetPictureSize(int width, int height) public bool SetPictureSize(int width, int height)
{ {
if (width >= _drawningTruckWidth && height >= _drawningTruckHeight) if (width >= _drawningTruckWidth && height >= _drawningTruckHeight)

View File

@ -28,29 +28,22 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxDumpTruck = new PictureBox();
buttonCreateDumpTruck = new Button(); buttonCreateDumpTruck = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonCreateTruck = new Button(); buttonCreateTruck = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).BeginInit(); comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
pictureBoxDumpTruck1 = new PictureBox();
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck1).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// pictureBoxDumpTruck
//
pictureBoxDumpTruck.Dock = DockStyle.Fill;
pictureBoxDumpTruck.Location = new Point(0, 0);
pictureBoxDumpTruck.Name = "pictureBoxDumpTruck";
pictureBoxDumpTruck.Size = new Size(892, 514);
pictureBoxDumpTruck.TabIndex = 0;
pictureBoxDumpTruck.TabStop = false;
//
// buttonCreateDumpTruck // buttonCreateDumpTruck
// //
buttonCreateDumpTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateDumpTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateDumpTruck.Location = new Point(12, 473); buttonCreateDumpTruck.Location = new Point(12, 486);
buttonCreateDumpTruck.Name = "buttonCreateDumpTruck"; buttonCreateDumpTruck.Name = "buttonCreateDumpTruck";
buttonCreateDumpTruck.Size = new Size(188, 29); buttonCreateDumpTruck.Size = new Size(188, 29);
buttonCreateDumpTruck.TabIndex = 1; buttonCreateDumpTruck.TabIndex = 1;
@ -63,7 +56,7 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.влево; buttonLeft.BackgroundImage = Properties.Resources.влево;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(738, 456); buttonLeft.Location = new Point(852, 469);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(40, 40); buttonLeft.Size = new Size(40, 40);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
@ -75,7 +68,7 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.вправо; buttonRight.BackgroundImage = Properties.Resources.вправо;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch; buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(830, 456); buttonRight.Location = new Point(944, 469);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(40, 40); buttonRight.Size = new Size(40, 40);
buttonRight.TabIndex = 3; buttonRight.TabIndex = 3;
@ -87,7 +80,7 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.вверх; buttonUp.BackgroundImage = Properties.Resources.вверх;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch; buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(784, 410); buttonUp.Location = new Point(898, 423);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(40, 40); buttonUp.Size = new Size(40, 40);
buttonUp.TabIndex = 4; buttonUp.TabIndex = 4;
@ -99,7 +92,7 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.вниз; buttonDown.BackgroundImage = Properties.Resources.вниз;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch; buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(784, 456); buttonDown.Location = new Point(898, 469);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(40, 40); buttonDown.Size = new Size(40, 40);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 5;
@ -109,7 +102,7 @@
// buttonCreateTruck // buttonCreateTruck
// //
buttonCreateTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateTruck.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateTruck.Location = new Point(206, 473); buttonCreateTruck.Location = new Point(206, 486);
buttonCreateTruck.Name = "buttonCreateTruck"; buttonCreateTruck.Name = "buttonCreateTruck";
buttonCreateTruck.Size = new Size(188, 29); buttonCreateTruck.Size = new Size(188, 29);
buttonCreateTruck.TabIndex = 6; buttonCreateTruck.TabIndex = 6;
@ -117,21 +110,53 @@
buttonCreateTruck.UseVisualStyleBackColor = true; buttonCreateTruck.UseVisualStyleBackColor = true;
buttonCreateTruck.Click += ButtonCreateTruck_Click; buttonCreateTruck.Click += ButtonCreateTruck_Click;
// //
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(833, 21);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(890, 55);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(94, 29);
buttonStrategyStep.TabIndex = 9;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// pictureBoxDumpTruck1
//
pictureBoxDumpTruck1.Dock = DockStyle.Fill;
pictureBoxDumpTruck1.Location = new Point(0, 0);
pictureBoxDumpTruck1.Name = "pictureBoxDumpTruck1";
pictureBoxDumpTruck1.Size = new Size(1006, 527);
pictureBoxDumpTruck1.TabIndex = 10;
pictureBoxDumpTruck1.TabStop = false;
//
// FormDumpTruck // FormDumpTruck
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(892, 514); ClientSize = new Size(1006, 527);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateTruck); Controls.Add(buttonCreateTruck);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonCreateDumpTruck); Controls.Add(buttonCreateDumpTruck);
Controls.Add(pictureBoxDumpTruck); Controls.Add(pictureBoxDumpTruck1);
Name = "FormDumpTruck"; Name = "FormDumpTruck";
Text = "Самосвал"; Text = "Самосвал";
((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxDumpTruck1).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -144,5 +169,8 @@
private Button buttonUp; private Button buttonUp;
private Button buttonDown; private Button buttonDown;
private Button buttonCreateTruck; private Button buttonCreateTruck;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
private PictureBox pictureBoxDumpTruck1;
} }
} }

View File

@ -1,5 +1,6 @@
 using System.Diagnostics;
using ProjectDumpTruck.Drawnings; using ProjectDumpTruck.Drawnings;
using ProjectDumpTruck.MovementStrategy;
namespace ProjectDumpTruck namespace ProjectDumpTruck
{ {
@ -8,20 +9,26 @@ namespace ProjectDumpTruck
{ {
private DrawningTruck? _drawningTruck; private DrawningTruck? _drawningTruck;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
public FormDumpTruck() public FormDumpTruck()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
private void Draw() private void Draw()
{ {
if (_drawningTruck == null) return; if (_drawningTruck == null) return;
Bitmap bmp = new(pictureBoxDumpTruck.Width, Bitmap bmp = new(pictureBoxDumpTruck1.Width,
pictureBoxDumpTruck.Height); pictureBoxDumpTruck1.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningTruck.DrawTransport(gr); _drawningTruck.DrawTransport(gr);
pictureBoxDumpTruck.Image = bmp; pictureBoxDumpTruck1.Image = bmp;
} }
@ -50,8 +57,10 @@ namespace ProjectDumpTruck
default: default:
return; return;
} }
_drawningTruck.SetPictureSize(pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawningTruck.SetPictureSize(pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height);
_drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawningTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
//comboBoxStrategy.Enabled = true;
Draw(); Draw();
} }
@ -69,7 +78,7 @@ namespace ProjectDumpTruck
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck)); private void ButtonCreateTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck));
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawningTruck == null) return; if (_drawningTruck == null) return;
@ -91,6 +100,42 @@ namespace ProjectDumpTruck
if (result) Draw(); if (result) Draw();
} }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningTruck == null) return;
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null) return;
_strategy.SetData(new MoveableTruck(_drawningTruck), pictureBoxDumpTruck1.Width, pictureBoxDumpTruck1.Height);
}
if (_strategy == null) return;
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
} }
} }

View File

@ -10,7 +10,7 @@ public abstract class AbstractStrategy
/// <summary> /// <summary>
/// Перемещаемый объект /// Перемещаемый объект
/// </summary> /// </summary>
private IMoveableObject _moveableObject; private IMoveableObject? _moveableObject;
/// <summary> /// <summary>
/// Статус перемещения /// Статус перемещения
@ -84,7 +84,7 @@ public abstract class AbstractStrategy
public void MakeStep() public void MakeStep()
{ {
if (_state == StrategyStatus.InProgress) return; if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestination()) if (IsTargetDestination())
{ {
@ -118,7 +118,7 @@ public abstract class AbstractStrategy
/// Перемещение к цели /// Перемещение к цели
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
protected abstract bool MoveToTarget(); protected abstract void MoveToTarget();
/// <summary> /// <summary>
/// Достигнута ли цель /// Достигнута ли цель

View File

@ -1,5 +1,4 @@
 
namespace ProjectDumpTruck.MovementStrategy; namespace ProjectDumpTruck.MovementStrategy;
/// <summary> /// <summary>
@ -9,11 +8,34 @@ public class MoveToBorder : AbstractStrategy
{ {
protected override bool IsTargetDestination() protected override bool IsTargetDestination()
{ {
throw new NotImplementedException(); ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.LeftBorder - GetStep() <= 0 || objParams.RightBorder + GetStep() >= FieldWidth ||
objParams.TopBorder - GetStep() <= 0 || objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
} }
protected override bool MoveToTarget() protected override void MoveToTarget()
{ {
throw new NotImplementedException(); ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
int diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
MoveRight();
}
int diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
} }
} }

View File

@ -8,11 +8,48 @@ public class MoveToCenter : AbstractStrategy
{ {
protected override bool IsTargetDestination() protected override bool IsTargetDestination()
{ {
throw new NotImplementedException(); ObjectParameters? objParams = GetObjectParameters;
if (objParams == null) return false;
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2
&& objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2
&& objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
} }
protected override bool MoveToTarget() protected override void MoveToTarget()
{ {
throw new NotImplementedException(); ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return ;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
} }
} }

View File

@ -53,6 +53,6 @@ public class MoveableTruck : IMoveableObject
{ {
if (_truck == null || _truck.EntityTruck == null) return false; if (_truck == null || _truck.EntityTruck == null) return false;
return _truck.MoveTransport(GetDirectionType(direction)) return _truck.MoveTransport(GetDirectionType(direction));
} }
} }

View File

@ -46,12 +46,12 @@ public class ObjectParameters
/// <summary> /// <summary>
/// Середина объекта /// Середина объекта
/// </summary> /// </summary>
public int ObjectMiddleHprizontal => _x+_width/2; public int ObjectMiddleHorizontal => _x+_width/2;
/// <summary> /// <summary>
/// Середина объекта /// Середина объекта
/// </summary> /// </summary>
public int ObjectMiddleVertical => _x + _height / 2; public int ObjectMiddleVertical => _y + _height / 2;
/// <summary> /// <summary>
/// Конструктор /// Конструктор