Осталось исправить ошибки

This commit is contained in:
Shtyrkin_Egor 2024-04-15 10:32:35 +04:00
parent 9e1d64b87e
commit f3f138ff62
9 changed files with 350 additions and 31 deletions

View File

@ -5,6 +5,11 @@
/// </summary>
public enum DirectionType
{
/// <summary>
/// Неизвестное направление
/// </summary>
Unknow = -1,
/// <summary>
/// Вверх
/// </summary>

View File

@ -7,6 +7,9 @@ using System.Threading.Tasks;
namespace ProjectAircraftCarrier.Drawnings;
/// <summary>
/// Класс отвечающий за прорисовку и перемещение базового объекта-сущности
/// </summary>
public class DrawningWarship
{
/// <summary>
@ -44,6 +47,26 @@ public class DrawningWarship
/// </summary>
private readonly int _drawningWarshipHeight = 50;
/// <summary>
/// Координата X объекта
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningWarshipWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawningWarshipHeight;
/// <summary>
/// Пустой конструктор
/// </summary>

View File

@ -20,11 +20,6 @@ public class EntityAircraftCarrier : EntityWarship
/// </summary>
public bool ControlRoom { get; private set; }
/// <summary>
/// Шаг перемещения авианосца
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса авианосца
/// </summary>
@ -34,7 +29,7 @@ public class EntityAircraftCarrier : EntityWarship
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="aircraftDeck">Признак наличия взлётно-посадочной полосы</param>
/// <param name="controlRoom">Признак наличия рубки управления</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom)
public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool aircraftDeck, bool controlRoom) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
AircraftDeck = aircraftDeck;

View File

@ -35,6 +35,8 @@
buttonDown = new Button();
buttonLeft = new Button();
buttonCreateWarship = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAircraftCarrier).BeginInit();
SuspendLayout();
//
@ -42,9 +44,8 @@
//
pictureBoxAircraftCarrier.Dock = DockStyle.Fill;
pictureBoxAircraftCarrier.Location = new Point(0, 0);
pictureBoxAircraftCarrier.Margin = new Padding(3, 4, 3, 4);
pictureBoxAircraftCarrier.Name = "pictureBoxAircraftCarrier";
pictureBoxAircraftCarrier.Size = new Size(918, 628);
pictureBoxAircraftCarrier.Size = new Size(803, 471);
pictureBoxAircraftCarrier.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxAircraftCarrier.TabIndex = 0;
pictureBoxAircraftCarrier.TabStop = false;
@ -52,24 +53,22 @@
// buttonCreateAircraftCarrier
//
buttonCreateAircraftCarrier.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateAircraftCarrier.Location = new Point(14, 581);
buttonCreateAircraftCarrier.Margin = new Padding(3, 4, 3, 4);
buttonCreateAircraftCarrier.Location = new Point(12, 436);
buttonCreateAircraftCarrier.Name = "buttonCreateAircraftCarrier";
buttonCreateAircraftCarrier.Size = new Size(188, 31);
buttonCreateAircraftCarrier.Size = new Size(164, 23);
buttonCreateAircraftCarrier.TabIndex = 1;
buttonCreateAircraftCarrier.Text = "Создать авианосец";
buttonCreateAircraftCarrier.UseVisualStyleBackColor = true;
buttonCreateAircraftCarrier.Click += buttonCreate_Click;
buttonCreateAircraftCarrier.Click += ButtonCreateAircraftCarrier_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(876, 585);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Location = new Point(766, 439);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30);
buttonRight.Size = new Size(26, 22);
buttonRight.TabIndex = 2;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
@ -79,10 +78,9 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(840, 547);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Location = new Point(735, 410);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30);
buttonUp.Size = new Size(26, 22);
buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click;
@ -92,10 +90,9 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(840, 585);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Location = new Point(735, 439);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30);
buttonDown.Size = new Size(26, 22);
buttonDown.TabIndex = 4;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click;
@ -105,10 +102,9 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(804, 585);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Location = new Point(704, 439);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30);
buttonLeft.Size = new Size(26, 22);
buttonLeft.TabIndex = 5;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click;
@ -116,20 +112,41 @@
// buttonCreateWarship
//
buttonCreateWarship.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreateWarship.Location = new Point(208, 581);
buttonCreateWarship.Margin = new Padding(3, 4, 3, 4);
buttonCreateWarship.Location = new Point(182, 436);
buttonCreateWarship.Name = "buttonCreateWarship";
buttonCreateWarship.Size = new Size(208, 31);
buttonCreateWarship.Size = new Size(182, 23);
buttonCreateWarship.TabIndex = 6;
buttonCreateWarship.Text = "Создать военный корабль";
buttonCreateWarship.UseVisualStyleBackColor = true;
buttonCreateWarship.Click += ButtonCreateWarship_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(670, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// buttonStrategyStep
//
buttonStrategyStep.Location = new Point(716, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 8;
buttonStrategyStep.Text = "Шаг";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += ButtonStrategyStep_Click;
//
// FormAircraftCarrier
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(918, 628);
ClientSize = new Size(803, 471);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateWarship);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
@ -137,7 +154,6 @@
Controls.Add(buttonRight);
Controls.Add(buttonCreateAircraftCarrier);
Controls.Add(pictureBoxAircraftCarrier);
Margin = new Padding(3, 4, 3, 4);
Name = "FormAircraftCarrier";
StartPosition = FormStartPosition.CenterScreen;
Text = "Авианосец";
@ -155,5 +171,7 @@
private Button buttonDown;
private Button buttonLeft;
private Button buttonCreateWarship;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
}
}

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectAircraftCarrier.Drawnings;
using ProjectAircraftCarrier.MovementStrategy;
namespace ProjectAircraftCarrier;
@ -21,12 +22,18 @@ public partial class FormAircraftCarrier : Form
/// </summary>
private DrawningWarship? _drawningWarship;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Конструктор формы
/// </summary>
public FormAircraftCarrier()
{
InitializeComponent();
_strategy = null;
}
/// <summary>
@ -66,6 +73,8 @@ public partial class FormAircraftCarrier : Form
_drawningWarship.SetPictureSize(pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
_drawningWarship.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
@ -82,7 +91,7 @@ public partial class FormAircraftCarrier : Form
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateWarship_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarship));
/// <summary>
/// Перемещение объекта по форме (нажатие кнопок навигации)
/// </summary>
@ -118,4 +127,47 @@ public partial class FormAircraftCarrier : Form
Draw();
}
}
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawningWarship == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToCenter(),
_ => null,
};
if(_strategy == null)
{
return;
}
_strategy.SetData(new MoveableWarship(_drawningWarship), pictureBoxAircraftCarrier.Width, pictureBoxAircraftCarrier.Height);
}
if(_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if(_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}

View File

@ -104,5 +104,42 @@ public abstract class AbstractStrategy
/// </summary>
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if(_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestination();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="movementDirection">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(MovementDirection movementDirection)
{
if(_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier.MovementStrategy;
public class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParameters? 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()
{
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier.MovementStrategy;
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
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 void MoveToTarget()
{
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 (diffX > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,71 @@
using ProjectAircraftCarrier.Drawnings;
using ProjectAircraftCarrier.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAircraftCarrier.MovementStrategy;
/// <summary>
/// Класс реализация IMoveableObject с использованием DrawningWarship
/// </summary>
public class MoveableWarship : IMoveableObject
{
/// <summary>
/// Поле-объект класса DrawningWarship или его наследника
/// </summary>
private readonly DrawningWarship? _warship = null;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="warship">Объект класса DrawningWarship</param>
public MoveableWarship(DrawningWarship warship)
{
_warship = warship;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_warship == null || _warship.EntityWarship == null || !_warship.GetPosX.HasValue || !_warship.GetPosY.HasValue)
{
return null;
}
return new ObjectParameters(_warship.GetPosX.Value, _warship.GetPosY.Value, _warship.GetWidth, _warship.GetHeight);
}
}
public int GetStep => (int)(_warship?.EntityWarship?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction)
{
if(_warship == null || _warship.EntityWarship == null)
{
return false;
}
return _warship.MoveTransport(GetDirectionType(direction));
}
/// <summary>
/// Конвертация из MovementDirection в DirectionType
/// </summary>
/// <param name="direction">MovementDirection</param>
/// <returns>DirectionType</returns>
private static DirectionType GetDirectionType(MovementDirection direction)
{
return direction switch
{
MovementDirection.Left => DirectionType.Left,
MovementDirection.Right => DirectionType.Right,
MovementDirection.Up => DirectionType.Up,
MovementDirection.Down => DirectionType.Down,
_ => DirectionType.Unknow,
};
}
}