All lab 2 for push
This commit is contained in:
parent
0497411365
commit
17cb3342de
@ -14,11 +14,8 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
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)
|
||||
@ -57,7 +54,6 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
protected bool MoveDown() => MoveTo(DirectionType.Down);
|
||||
|
||||
/// Параметры объекта
|
||||
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
|
||||
protected int? GetStep()
|
||||
@ -68,17 +64,10 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
}
|
||||
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)
|
||||
@ -91,7 +80,6 @@ namespace ProjectElectricLocomotive.MovementStrategy
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,14 +10,13 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
public class DrawingElectricLocomotive : DrawingLocomotive
|
||||
{
|
||||
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)
|
||||
bool horns, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 85, 50)
|
||||
{
|
||||
if (EntityLocomotive != null)
|
||||
{
|
||||
EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries);
|
||||
}
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLocomotive is not EntityElectricLocomotive electricLocomotive)
|
||||
@ -44,7 +43,6 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
if (electricLocomotive.SeifBatteries)
|
||||
{
|
||||
g.FillRectangle(blackBrush, _startPosX + 80, _startPosY + 30, 5, 10);
|
||||
|
||||
}
|
||||
base.DrawTransport(g);
|
||||
}
|
||||
|
@ -13,24 +13,20 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
{
|
||||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
protected int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
protected int _pictureHeight;
|
||||
|
||||
protected int _startPosX;
|
||||
|
||||
protected int _startPosY;
|
||||
|
||||
protected readonly int _locoWidth = 150;
|
||||
protected readonly int _locoWidth = 85;
|
||||
|
||||
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)
|
||||
@ -134,7 +130,6 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
g.DrawPolygon(pen, new Point[]
|
||||
{
|
||||
new Point(_startPosX, _startPosY + 40),
|
||||
@ -179,10 +174,8 @@ 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)
|
||||
@ -196,9 +189,9 @@ namespace ProjectElectricLocomotive.DrawingObjects
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityLocomotive.Step > 0,
|
||||
// вправо
|
||||
DirectionType.Right => _startPosX + EntityLocomotive.Speed < _pictureWidth,
|
||||
DirectionType.Right => _startPosX + EntityLocomotive.Step < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityLocomotive.Speed < _pictureHeight,
|
||||
DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +116,8 @@
|
||||
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.comboBoxStrategy.FormattingEnabled = true;
|
||||
this.comboBoxStrategy.Items.AddRange(new object[] {
|
||||
"MoveToCenter"});
|
||||
"MoveToCenter",
|
||||
"MoveToRightCorner"});
|
||||
this.comboBoxStrategy.Location = new System.Drawing.Point(1058, 12);
|
||||
this.comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
this.comboBoxStrategy.Size = new System.Drawing.Size(160, 33);
|
||||
|
@ -87,7 +87,7 @@ namespace ElectricLocomotive
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
//1 => new MoveToBorder(),
|
||||
1 => new MoveToRightCorner(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
|
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
internal class MoveToCenter : AbstractStrategy
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
|
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectElectricLocomotive.MovementStrategy
|
||||
{
|
||||
public class MoveToRightCorner : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return false;
|
||||
|
||||
return objParams.RightBorder >= FieldWidth - GetStep() && objParams.DownBorder >= FieldHeight - GetStep();
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null) return;
|
||||
|
||||
if (objParams.RightBorder < FieldWidth - GetStep()) MoveRight();
|
||||
if (objParams.DownBorder < FieldHeight - GetStep()) MoveDown();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user