PIbd-23-Kondratev-N.D.-Gaso.../GasolineTanker/ProjectGasolineTanker/MovementStratg/AbstractStrategy.cs

95 lines
2.3 KiB
C#
Raw Permalink Normal View History

2024-10-03 01:40:53 +04:00
using System;
2024-10-03 01:16:11 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
2024-10-03 01:40:53 +04:00
using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.Entities;
2024-10-03 01:16:11 +04:00
2024-10-03 01:40:53 +04:00
namespace ProjectGasolineTanker.MovementStratg
2024-10-03 01:16:11 +04:00
{
2024-10-03 01:45:35 +04:00
2024-10-03 01:16:11 +04:00
public abstract class AbstractStrategy
{
2024-10-03 01:43:12 +04:00
2024-10-03 01:16:11 +04:00
private IMoveableObject? _moveableObject;
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
private Status _state = Status.NotInit;
2024-10-03 01:45:35 +04:00
2024-10-03 01:16:11 +04:00
protected int FieldWidth { get; private set; }
2024-10-03 01:46:41 +04:00
2024-10-03 01:16:11 +04:00
protected int FieldHeight { get; private set; }
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
public Status GetStatus() { return _state; }
2024-10-03 01:44:06 +04:00
2024-10-03 01:16:11 +04:00
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;
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected bool MoveLeft() => MoveTo(DirectionType.Left);
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected bool MoveRight() => MoveTo(DirectionType.Right);
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected bool MoveUp() => MoveTo(DirectionType.Up);
2024-10-03 01:46:41 +04:00
2024-10-03 01:16:11 +04:00
protected bool MoveDown() => MoveTo(DirectionType.Down);
2024-10-03 01:43:12 +04:00
2024-10-03 01:16:11 +04:00
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected abstract void MoveToTarget();
2024-10-03 01:40:53 +04:00
2024-10-03 01:16:11 +04:00
protected abstract bool IsTargetDestinaion();
2024-10-03 01:42:04 +04:00
2024-10-03 01:16:11 +04:00
private bool MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
2024-10-03 01:40:53 +04:00
}