Created all classes, but without second moving method

This commit is contained in:
ekallin 2023-09-26 00:18:33 +04:00
parent 9f2f1b0686
commit 0497411365
10 changed files with 375 additions and 132 deletions

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectElectricLocomotive.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;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false -неудача)</returns>
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

@ -1,20 +1,17 @@
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.Entities;
using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
namespace ProjectElectricLocomotive.DrawingObjects
{
public class DrawingElectricLocomotive : DrawingLocomotive
{
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit,
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
bool horns, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 150, 50)
{
if (EntityLocomotive != null)
{
EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries);
@ -46,91 +43,10 @@ namespace ElectricLocomotive
if (electricLocomotive.SeifBatteries)
{
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 40, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 35),
new Point(_startPosX + 40, _startPosY + 35),
new Point(_startPosX + 40, _startPosY + 25),
}
);
g.FillRectangle(blackBrush, _startPosX + 80, _startPosY + 30, 5, 10);
}
/*
//окошки
g.FillPolygon(windows, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 10, _startPosY + 30),
new Point(_startPosX +15, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 25),
new Point(_startPosX + 20, _startPosY + 30),
new Point(_startPosX +10, _startPosY + 30),
}
);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
//обязательные колеса
//loco
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 5, 5);
//telega
g.FillEllipse(blackBrush, _startPosX + 95, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 140, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 130, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 105, _startPosY + 45, 5, 5);
//telejka
g.FillPolygon(additionalBrush, new Point[]
{
new Point(_startPosX + 90, _startPosY + 25),
new Point(_startPosX + 95, _startPosY + 20),
new Point(_startPosX + 145, _startPosY + 20),
new Point(_startPosX + 150, _startPosY + 25),
new Point(_startPosX + 150, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 25),
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX + 90, _startPosY + 25),
new Point(_startPosX + 95, _startPosY + 20),
new Point(_startPosX + 145, _startPosY + 20),
new Point(_startPosX + 150, _startPosY + 25),
new Point(_startPosX + 150, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 45),
new Point(_startPosX + 90, _startPosY + 25),
}
);
//телега окна
g.DrawLine(pen, _startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
g.FillRectangle(windows, _startPosX + 95, _startPosY + 30, 10, 5);
g.FillRectangle(windows, _startPosX + 115, _startPosY + 30, 10, 5);
g.FillRectangle(windows, _startPosX + 135, _startPosY + 30, 10, 5);*/
base.DrawTransport(g);
}
}
}

View File

@ -13,10 +13,10 @@ namespace ProjectElectricLocomotive.DrawingObjects
{
public EntityLocomotive? EntityLocomotive { get; protected set; }
private int _pictureHeight;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
@ -25,21 +25,29 @@ namespace ProjectElectricLocomotive.DrawingObjects
protected readonly int _locoHeight = 50;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _locoWidth;
public int GetHeight => _locoHeight;
public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
{
if(EntityLocomotive == null)
if (width < _locoWidth || heigth < _locoHeight)
{
return;
}
_locoWidth = width;
_locoHeight = heigth;
_pictureWidth = width;
_pictureHeight = heigth;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
int height, int locoWidth, int locoHeight)
{
if (EntityLocomotive == null)
if (width < _locoWidth || height < _locoHeight)
{
return;
}
@ -53,7 +61,7 @@ namespace ProjectElectricLocomotive.DrawingObjects
//Установка позиции
public void SetPosition(int x, int y)
{
if(x < 0 || x + _locoWidth > _pictureWidth)
if (x < 0 || x + _locoWidth > _pictureWidth)
{
x = _pictureWidth - _locoWidth;
}
@ -126,6 +134,7 @@ namespace ProjectElectricLocomotive.DrawingObjects
}
);
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
@ -170,6 +179,27 @@ namespace ProjectElectricLocomotive.DrawingObjects
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 5, 5);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 5, 5);
}
public bool CanMove(DirectionType direction)
{
if (EntityLocomotive == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityLocomotive.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityLocomotive.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityLocomotive.Speed < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityLocomotive.Speed < _pictureHeight,
};
}
}
}

View File

@ -0,0 +1,35 @@
using ElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.MovementStrategy
{
public class DrawingObjectLocomotive : IMoveableObject
{
private readonly DrawingLocomotive? _drawningLocomotive = null;
public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive)
{
_drawningLocomotive = drawningLocomotive;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive == null)
{
return null;
}
return new ObjectParameters(_drawningLocomotive.GetPosX,
_drawningLocomotive.GetPosY, _drawningLocomotive.GetWidth, _drawningLocomotive.GetHeight);
}
}
public int GetStep => (int)(_drawningLocomotive?.EntityLocomotive?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _drawningLocomotive?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _drawningLocomotive?.MoveTransport(direction);
}
}

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
this.pictureBoxElectricLocomotive = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonCreateElectricLocomotive = 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.buttonCreateLocomotive = new System.Windows.Forms.Button();
this.buttonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxElectricLocomotive)).BeginInit();
this.SuspendLayout();
//
@ -42,29 +45,29 @@
this.pictureBoxElectricLocomotive.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxElectricLocomotive.Location = new System.Drawing.Point(0, 0);
this.pictureBoxElectricLocomotive.Name = "pictureBoxElectricLocomotive";
this.pictureBoxElectricLocomotive.Size = new System.Drawing.Size(533, 465);
this.pictureBoxElectricLocomotive.Size = new System.Drawing.Size(1242, 512);
this.pictureBoxElectricLocomotive.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxElectricLocomotive.TabIndex = 0;
this.pictureBoxElectricLocomotive.TabStop = false;
//
// buttonCreate
// buttonCreateElectricLocomotive
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreate.Location = new System.Drawing.Point(16, 395);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(111, 52);
this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
this.buttonCreateElectricLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateElectricLocomotive.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreateElectricLocomotive.Location = new System.Drawing.Point(16, 442);
this.buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive";
this.buttonCreateElectricLocomotive.Size = new System.Drawing.Size(272, 52);
this.buttonCreateElectricLocomotive.TabIndex = 1;
this.buttonCreateElectricLocomotive.Text = "Создать электролокомотив";
this.buttonCreateElectricLocomotive.UseVisualStyleBackColor = true;
this.buttonCreateElectricLocomotive.Click += new System.EventHandler(this.buttonCreateElectricLocomotive_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(389, 417);
this.buttonLeft.Location = new System.Drawing.Point(1098, 464);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2;
@ -76,7 +79,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowUP;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(435, 368);
this.buttonUp.Location = new System.Drawing.Point(1144, 415);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 3;
@ -88,7 +91,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(479, 417);
this.buttonRight.Location = new System.Drawing.Point(1188, 464);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 4;
@ -100,23 +103,62 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::ProjectElectricLocomotive.Properties.Resources.arrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(435, 417);
this.buttonDown.Location = new System.Drawing.Point(1144, 464);
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[] {
"MoveToCenter"});
this.comboBoxStrategy.Location = new System.Drawing.Point(1058, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(160, 33);
this.comboBoxStrategy.TabIndex = 6;
//
// buttonCreateLocomotive
//
this.buttonCreateLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateLocomotive.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonCreateLocomotive.Location = new System.Drawing.Point(333, 442);
this.buttonCreateLocomotive.Name = "buttonCreateLocomotive";
this.buttonCreateLocomotive.Size = new System.Drawing.Size(272, 52);
this.buttonCreateLocomotive.TabIndex = 7;
this.buttonCreateLocomotive.Text = "Создать локомотив";
this.buttonCreateLocomotive.UseVisualStyleBackColor = true;
this.buttonCreateLocomotive.Click += new System.EventHandler(this.buttonCreateLocomotive_Click);
//
// buttonStep
//
this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonStep.Font = new System.Drawing.Font("Candara Light", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.buttonStep.Location = new System.Drawing.Point(1079, 65);
this.buttonStep.Name = "buttonStep";
this.buttonStep.Size = new System.Drawing.Size(125, 56);
this.buttonStep.TabIndex = 8;
this.buttonStep.Text = "Шаг";
this.buttonStep.UseVisualStyleBackColor = true;
this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click);
//
// ElectricLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(533, 465);
this.ClientSize = new System.Drawing.Size(1242, 512);
this.Controls.Add(this.buttonStep);
this.Controls.Add(this.buttonCreateLocomotive);
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.buttonCreate);
this.Controls.Add(this.buttonCreateElectricLocomotive);
this.Controls.Add(this.pictureBoxElectricLocomotive);
this.Name = "ElectricLocomotive";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
@ -130,10 +172,13 @@
#endregion
private PictureBox pictureBoxElectricLocomotive;
private Button buttonCreate;
private Button buttonCreateElectricLocomotive;
private Button buttonLeft;
private Button buttonUp;
private Button buttonRight;
private Button buttonDown;
private ComboBox comboBoxStrategy;
private Button buttonCreateLocomotive;
private Button buttonStep;
}
}

View File

@ -1,8 +1,17 @@
using ProjectElectricLocomotive;
using ProjectElectricLocomotive.DrawingObjects;
using ProjectElectricLocomotive.MovementStrategy;
using System;
namespace ElectricLocomotive
{
public partial class ElectricLocomotive : Form
{
private DrawingElectricLocomotive? _drawingElectricLocomotive;
private DrawingLocomotive? _drawingLocomotive;
private AbstractStrategy? _abstractStrategy;
public ElectricLocomotive()
{
InitializeComponent();
@ -10,32 +19,41 @@ namespace ElectricLocomotive
private void Draw()
{
if (_drawingElectricLocomotive == null)
if (_drawingLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawingElectricLocomotive.DrawTransport(gr);
_drawingLocomotive.DrawTransport(gr);
pictureBoxElectricLocomotive.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e)
{
Random random = new Random();
_drawingLocomotive = new DrawingElectricLocomotive(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)),
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawingLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
private void buttonCreateLocomotive_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_drawingElectricLocomotive = new DrawingElectricLocomotive();
_drawingElectricLocomotive.Init(rnd.Next(100, 300), rnd.Next(1000, 3000),
_drawingLocomotive = new DrawingLocomotive(rnd.Next(100, 300), rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height);
_drawingElectricLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
_drawingLocomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingElectricLocomotive == null)
if (_drawingLocomotive == null)
{
return;
}
@ -43,19 +61,55 @@ namespace ElectricLocomotive
switch (name)
{
case "buttonUp":
_drawingElectricLocomotive.MoveTransport(DirectionType.Up);
_drawingLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawingElectricLocomotive.MoveTransport(DirectionType.Down);
_drawingLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawingElectricLocomotive.MoveTransport(DirectionType.Left);
_drawingLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawingElectricLocomotive.MoveTransport(DirectionType.Right);
_drawingLocomotive.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingLocomotive == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
//1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectLocomotive(_drawingLocomotive), pictureBoxElectricLocomotive.Width,
pictureBoxElectricLocomotive.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -8,7 +8,7 @@ using ProjectElectricLocomotive.DrawingObjects;
namespace ProjectElectricLocomotive.MovementStrategy
{
internal interface IMoveableObject
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.MovementStrategy
{
internal 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

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace ProjectElectricLocomotive.MovementStrategy
{
internal class ObjectParameters
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;

View File

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