PIbd-21 Sagirov M.M. LinerBase Lab2 #7
81
Liner/Drawing/DrawingBigLiner.cs
Normal file
81
Liner/Drawing/DrawingBigLiner.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using Liner.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.Drawing
|
||||
{
|
||||
public class DrawingBigLiner : DrawingLiner
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="swimmingPool">Признак наличия бассейна</param>
|
||||
/// <param name="deck">Признак наличия доп палубы</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public DrawingBigLiner(int speed, double weight, Color bodyColor, Color bottomColor, bool swimmingPool, bool deck, int width, int height)
|
||||
: base(speed,weight,bodyColor,bottomColor,width,height,100,80)
|
||||
{
|
||||
if(EntityLiner!= null)
|
||||
{
|
||||
EntityLiner = new EntityBigLiner(speed, weight, bodyColor, bottomColor, deck, swimmingPool);
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
// высота палубы - 20 пикселей
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLiner is not EntityBigLiner liner)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush poolBrush = new SolidBrush(Color.Blue);
|
||||
Brush bottomBrush = new SolidBrush(EntityLiner.BottomColor);
|
||||
Brush deckBrush = new SolidBrush(EntityLiner.BodyColor);
|
||||
// палуба
|
||||
Point[] firstDeck = {new Point(_startPosX + 30, _startPosY), new Point(_startPosX + 30 + 60, _startPosY),
|
||||
new Point(_startPosX + 30 + 60, _startPosY + 20), new Point(_startPosX + 30, _startPosY + 20)};
|
||||
int addY = 20;
|
||||
// нижний корпус
|
||||
Point[] bottom = {new Point(_startPosX, _startPosY), new Point(_startPosX + 100, _startPosY),
|
||||
new Point(_startPosX+80, _startPosY + 40), new Point(_startPosX + 20, _startPosY + 40)};
|
||||
// бассейн
|
||||
Point[] pool = {new Point(_startPosX + 10, _startPosY), new Point(_startPosX + 30, _startPosY),
|
||||
new Point(_startPosX+30, _startPosY + 10), new Point(_startPosX + 10, _startPosY + 10)};
|
||||
g.FillPolygon(deckBrush, firstDeck);
|
||||
g.DrawPolygon(pen, firstDeck);//рисуем палубу
|
||||
Point[] currentDeck = new Point[4]; //буффер для прорисовки доп палуб
|
||||
firstDeck.CopyTo(currentDeck, 0);
|
||||
if (liner.Deck) //рисуем вторую палубу
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
currentDeck[i].Y += addY;
|
||||
}
|
||||
g.FillPolygon(deckBrush, currentDeck);
|
||||
g.DrawPolygon(pen, currentDeck);
|
||||
addY += 20;
|
||||
}
|
||||
for (int i = 0; i < 4; i++) //сдвигаем элементы вниз на кол-во палуб
|
||||
{
|
||||
bottom[i].Y += addY;
|
||||
pool[i].Y += addY;
|
||||
}
|
||||
g.FillPolygon(bottomBrush, bottom);
|
||||
g.DrawPolygon(pen, bottom);//рисуем нижний корпус
|
||||
if (liner.SwimmingPool) { g.FillPolygon(poolBrush, pool); }//рисуем бассейн
|
||||
g.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -3,16 +3,16 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Liner.Entities;
|
||||
|
||||
namespace Liner
|
||||
namespace Liner.Drawing
|
||||
{
|
||||
public class DrawingLiner
|
||||
{
|
||||
private Brush poolBrush = new SolidBrush(Color.Blue);
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityLiner? EntityLiner { get; private set; }
|
||||
public EntityLiner? EntityLiner { get; protected set; }
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
@ -24,42 +24,66 @@ namespace Liner
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки лайнера
|
||||
/// </summary>
|
||||
private int _startPosX;
|
||||
protected int _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя координата прорисовки лайнера
|
||||
/// </summary>
|
||||
private int _startPosY;
|
||||
protected int _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина прорисовки лайнера
|
||||
/// </summary>
|
||||
private readonly int _linerWidth = 100;
|
||||
protected readonly int _linerWidth = 100;
|
||||
/// <summary>
|
||||
/// Высота прорисовки лайнера
|
||||
/// </summary>
|
||||
private readonly int _linerHeight = 80;
|
||||
protected readonly int _linerHeight = 60;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int GetPosX => _startPosX;
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int GetPosY => _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _linerWidth;
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _linerHeight;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="swimmingPool">Признак наличия бассейна</param>
|
||||
/// <param name="deck">Признак наличия доп палубы</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <returns>true - объект создан, false - проверка не пройдена,
|
||||
/// нельзя создать объект в этих размерах</returns>
|
||||
public bool Init(int speed, double weight, Color bodyColor,Color bottomColor, bool swimmingPool, bool deck, int width, int height)
|
||||
public DrawingLiner(int speed, double weight, Color bodyColor, Color bottomColor, int width, int height)
|
||||
{
|
||||
if(width < _linerWidth || height < _linerHeight)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityLiner = new EntityLiner();
|
||||
EntityLiner.Init(speed, weight, bodyColor,bottomColor, swimmingPool,deck);
|
||||
return true;
|
||||
EntityLiner = new EntityLiner(speed, weight, bodyColor, bottomColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
/// <param name="linerWidth">Ширина прорисовки лайнера</param>
|
||||
/// <param name="linerHeight">Высота прорисовки лайнера</param>
|
||||
protected DrawingLiner(int speed, double weight, Color bodyColor, Color bottomColor, int width, int height,int linerWidth,int linerHeight)
|
||||
{
|
||||
_linerHeight = linerHeight;
|
||||
_linerWidth = linerWidth;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityLiner = new EntityLiner(speed, weight, bodyColor, bottomColor);
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
@ -68,7 +92,7 @@ namespace Liner
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if(x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0)
|
||||
if (x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0)
|
||||
{
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
@ -76,54 +100,8 @@ namespace Liner
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - EntityLiner.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityLiner.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - EntityLiner.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityLiner.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + EntityLiner.Step < _pictureWidth - _linerWidth)
|
||||
{
|
||||
_startPosX += (int)EntityLiner.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + EntityLiner.Step < _pictureHeight - _linerHeight)
|
||||
{
|
||||
_startPosY += (int)EntityLiner.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
// высота палубы - 20 пикселей
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
{
|
||||
@ -135,36 +113,72 @@ namespace Liner
|
||||
// палуба
|
||||
Point[] firstDeck = {new Point(_startPosX + 30, _startPosY), new Point(_startPosX + 30 + 60, _startPosY),
|
||||
new Point(_startPosX + 30 + 60, _startPosY + 20), new Point(_startPosX + 30, _startPosY + 20)};
|
||||
int addY = 20;
|
||||
// нижний корпус
|
||||
Point[] bottom = {new Point(_startPosX, _startPosY), new Point(_startPosX + 100, _startPosY),
|
||||
new Point(_startPosX+80, _startPosY + 40), new Point(_startPosX + 20, _startPosY + 40)};
|
||||
// бассейн
|
||||
Point[] pool = {new Point(_startPosX + 10, _startPosY), new Point(_startPosX + 30, _startPosY),
|
||||
new Point(_startPosX+30, _startPosY + 10), new Point(_startPosX + 10, _startPosY + 10)};
|
||||
g.FillPolygon(deckBrush, firstDeck);
|
||||
g.DrawPolygon(pen, firstDeck);//рисуем палубу
|
||||
Point[] currentDeck = new Point[4]; //буффер для прорисовки доп палуб
|
||||
firstDeck.CopyTo(currentDeck, 0);
|
||||
if(EntityLiner.Deck) //рисуем вторую палубу
|
||||
for (int i = 0; i < 4; i++) //сдвигаем элементы вниз на кол-во палуб
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
{
|
||||
currentDeck[i].Y += addY;
|
||||
}
|
||||
g.FillPolygon(deckBrush, currentDeck);
|
||||
g.DrawPolygon(pen, currentDeck);
|
||||
addY += 20;
|
||||
}
|
||||
for(int i = 0; i < 4; i++) //сдвигаем элементы вниз на кол-во палуб
|
||||
{
|
||||
bottom[i].Y += addY;
|
||||
pool[i].Y += addY;
|
||||
bottom[i].Y += 20;
|
||||
}
|
||||
g.FillPolygon(bottomBrush, bottom);
|
||||
g.DrawPolygon(pen, bottom);//рисуем нижний корпус
|
||||
if (EntityLiner.SwimmingPool) { g.FillPolygon(poolBrush, pool); }//рисуем бассейн
|
||||
g.Dispose();
|
||||
}
|
||||
/// <summary>
|
||||
/// Проверка, что объект может переместится по указанному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityLiner == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
//влево
|
||||
DirectionType.Left => _startPosX - EntityLiner.Step > 0,
|
||||
//вверх
|
||||
DirectionType.Up => _startPosY - EntityLiner.Step > 0,
|
||||
//вправо
|
||||
DirectionType.Right => _startPosX + EntityLiner.Step + _linerWidth < _pictureWidth,
|
||||
//вниз
|
||||
DirectionType.Down => _startPosY + EntityLiner.Step + _linerHeight < _pictureHeight
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
_startPosX -= (int)EntityLiner.Step;
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
_startPosY -= (int)EntityLiner.Step;
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
_startPosX += (int)EntityLiner.Step;
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
_startPosY += (int)EntityLiner.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
Liner/Entities/EntityBigLiner.cs
Normal file
34
Liner/Entities/EntityBigLiner.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.Entities
|
||||
{
|
||||
public class EntityBigLiner : EntityLiner
|
||||
eegov
commented
Класс из первой части должен стать дочерним классом, а не родительским Класс из первой части должен стать дочерним классом, а не родительским
|
||||
{
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия бассейна
|
||||
/// </summary>
|
||||
public bool SwimmingPool { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия доп палубы
|
||||
/// </summary>
|
||||
public bool Deck { get; private set; }
|
||||
/// <summary>
|
||||
/// Конструктор с параметрами
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="bottomColor">Дополнительный цвет</param>
|
||||
/// <param name="deck">Признак наличия доп палубы</param>
|
||||
/// <param name="swimmingPool">Признак наличия бассейна</param>
|
||||
public EntityBigLiner(int speed, double weight, Color bodyColor, Color bottomColor,bool deck, bool swimmingPool) : base(speed, weight, bodyColor,bottomColor)
|
||||
{
|
||||
Deck = deck;
|
||||
SwimmingPool = swimmingPool;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner
|
||||
namespace Liner.Entities
|
||||
{
|
||||
public class EntityLiner
|
||||
{
|
||||
@ -26,34 +26,22 @@ namespace Liner
|
||||
/// </summary>
|
||||
public Color BottomColor { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия бассейна
|
||||
/// </summary>
|
||||
public bool SwimmingPool { get; private set; }
|
||||
/// <summary>
|
||||
/// Признак (опция) наличия доп палубы
|
||||
/// </summary>
|
||||
public bool Deck { get; private set; }
|
||||
/// <summary>
|
||||
/// Шаг
|
||||
/// </summary>
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса лайнера
|
||||
/// Конструктор с параметрами
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес лайнера</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="bottomColor">Цвет палубы</param>
|
||||
/// <param name="swimmingPool">Признак наличия бассейна</param>
|
||||
/// <param name="deck">Признак наличия доп палубы</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color bottomColor, bool swimmingPool, bool deck)
|
||||
/// <param name="bottomColor">Доп цвет</param>
|
||||
public EntityLiner(int speed, double weight, Color bodyColor, Color bottomColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
BottomColor = bottomColor;
|
||||
SwimmingPool = swimmingPool;
|
||||
Deck = deck;
|
||||
}
|
||||
}
|
||||
}
|
@ -8,6 +8,12 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="MovingStrategy\**" />
|
||||
<EmbeddedResource Remove="MovingStrategy\**" />
|
||||
<None Remove="MovingStrategy\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
|
64
Liner/MainScreen.Designer.cs
generated
64
Liner/MainScreen.Designer.cs
generated
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxLiner = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateLiner = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonCreateBigLiner = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -47,16 +50,16 @@
|
||||
pictureBoxLiner.TabIndex = 0;
|
||||
pictureBoxLiner.TabStop = false;
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateLiner
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(34, 584);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(87, 43);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Create Liner";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateLiner.Location = new Point(34, 584);
|
||||
buttonCreateLiner.Name = "buttonCreateLiner";
|
||||
buttonCreateLiner.Size = new Size(87, 43);
|
||||
buttonCreateLiner.TabIndex = 1;
|
||||
buttonCreateLiner.Text = "Create Liner";
|
||||
buttonCreateLiner.UseVisualStyleBackColor = true;
|
||||
buttonCreateLiner.Click += buttonCreateLiner_Click;
|
||||
//
|
||||
// buttonUp
|
||||
//
|
||||
@ -106,16 +109,50 @@
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += buttonMove_Click;
|
||||
//
|
||||
// buttonCreateBigLiner
|
||||
//
|
||||
buttonCreateBigLiner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateBigLiner.Location = new Point(127, 584);
|
||||
buttonCreateBigLiner.Name = "buttonCreateBigLiner";
|
||||
buttonCreateBigLiner.Size = new Size(87, 43);
|
||||
buttonCreateBigLiner.TabIndex = 6;
|
||||
buttonCreateBigLiner.Text = "Create BIG Liner";
|
||||
buttonCreateBigLiner.UseVisualStyleBackColor = true;
|
||||
buttonCreateBigLiner.Click += buttonCreateBigLiner_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
|
||||
comboBoxStrategy.Location = new Point(829, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(121, 23);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Location = new Point(856, 41);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(75, 23);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Step";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += buttonStep_Click;
|
||||
//
|
||||
// MainScreen
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(984, 661);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateBigLiner);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateLiner);
|
||||
Controls.Add(pictureBoxLiner);
|
||||
Name = "MainScreen";
|
||||
Text = "Liner";
|
||||
@ -127,10 +164,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxLiner;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateLiner;
|
||||
private Button buttonUp;
|
||||
private Button buttonDown;
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private Button buttonCreateBigLiner;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStep;
|
||||
}
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
using Liner.Drawing;
|
||||
using Liner.Entities;
|
||||
using Liner.MovingStrategies;
|
||||
|
||||
namespace Liner
|
||||
{
|
||||
public partial class MainScreen : Form
|
||||
@ -8,6 +12,10 @@ namespace Liner
|
||||
private DrawingLiner? _drawingLiner;
|
||||
private Bitmap bmp;
|
||||
/// <summary>
|
||||
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
|
||||
/// </summary>
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
/// <summary>
|
||||
/// Èíèöèàëèçàöèÿ ôîðìû
|
||||
/// </summary>
|
||||
public MainScreen()
|
||||
@ -30,18 +38,17 @@ namespace Liner
|
||||
pictureBoxLiner.Image = bmp;
|
||||
gr.Dispose();
|
||||
}
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void buttonCreateLiner_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingLiner = new DrawingLiner();
|
||||
_drawingLiner.Init(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)),
|
||||
_drawingLiner = new DrawingLiner(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)),
|
||||
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
_drawingLiner.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
_drawingLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
@ -68,7 +75,58 @@ namespace Liner
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonCreateBigLiner_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawingLiner = new DrawingBigLiner(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(1, 2)),
|
||||
pictureBoxLiner.Width, pictureBoxLiner.Height);
|
||||
_drawingLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void buttonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingLiner == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawingObjectLiner(_drawingLiner), pictureBoxLiner.Width,
|
||||
pictureBoxLiner.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
132
Liner/MovingStrategies/AbstractStrategy.cs
Normal file
132
Liner/MovingStrategies/AbstractStrategy.cs
Normal file
@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private Status _state = Status.NotInit;
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public Status GetStatus() { return _state; }
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(DirectionType.Left);
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(DirectionType.Right);
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(DirectionType.Up);
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(DirectionType.Down);
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
39
Liner/MovingStrategies/DrawingObjectLiner.cs
Normal file
39
Liner/MovingStrategies/DrawingObjectLiner.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Liner.Drawing;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса IMovableObject для работы с объектом DrawingLiner (паттерн Adapter)
|
||||
/// </summary>
|
||||
public class DrawingObjectLiner : IMoveableObject
|
||||
{
|
||||
private readonly DrawingLiner? _drawingLiner = null;
|
||||
public DrawingObjectLiner(DrawingLiner DrawingLiner)
|
||||
{
|
||||
_drawingLiner = DrawingLiner;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawingLiner == null || _drawingLiner.EntityLiner == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingLiner.GetPosX,
|
||||
_drawingLiner.GetPosY, _drawingLiner.GetWidth, _drawingLiner.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawingLiner?.EntityLiner?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) =>
|
||||
_drawingLiner?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) =>
|
||||
_drawingLiner?.MoveTransport(direction);
|
||||
}
|
||||
|
||||
}
|
34
Liner/MovingStrategies/IMoveableObject.cs
Normal file
34
Liner/MovingStrategies/IMoveableObject.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты X объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
/// <summary>
|
||||
/// Проверка, можно ли переместиться по нужному направлению
|
||||
/// </summary>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></returns>
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
/// <summary>
|
||||
/// Изменение направления пермещения объекта
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
59
Liner/MovingStrategies/MoveToBorder.cs
Normal file
59
Liner/MovingStrategies/MoveToBorder.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
var 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()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
59
Liner/MovingStrategies/MoveToCenter.cs
Normal file
59
Liner/MovingStrategies/MoveToCenter.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
public 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
Liner/MovingStrategies/ObjectParameters.cs
Normal file
57
Liner/MovingStrategies/ObjectParameters.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина</param>
|
||||
/// <param name="height">Высота</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
||||
}
|
18
Liner/MovingStrategies/Status.cs
Normal file
18
Liner/MovingStrategies/Status.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Liner.MovingStrategies
|
||||
{
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user
Класс из первой части должен стать дочерним классом, а не родительским