This commit is contained in:
chtzsch ~ 2023-09-20 13:54:36 +03:00
parent c6a5960e15
commit 72e2df80a7
14 changed files with 706 additions and 92 deletions

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab.Drawings;
using SpeedBoatLab.Entity;
namespace speed_Boat.MovementStrategy
{
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMovementObject? _movementObject;
/// <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>
public void SetData(IMovementObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_movementObject = 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 => _movementObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _movementObject?.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 (_movementObject?.CheckCanMove(directionType) ?? false)
{
_movementObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpeedBoatLab
namespace SpeedBoatLab.Drawings
{
/// <summary>
/// Направление перемещения

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab.Entity;
using SpeedBoatLab.Drawings;
namespace speed_Boat.MovementStrategy
{
class DrawingObjectBoat : IMovementObject
{
private readonly DrawingBoat? _drawingBoat = null;
public DrawingObjectBoat(DrawingBoat drawingBoat)
{
_drawingBoat = drawingBoat;
}
public ObjectParameters? GetObjectPosition
{
get
{
if(_drawingBoat == null || _drawingBoat._entityBoat == null)
{
return null;
}
return new ObjectParameters(_drawingBoat.GetPoseX, _drawingBoat.GetPoseY, _drawingBoat.GetWidth, _drawingBoat.GetHeight);
}
}
public int GetStep => (int)(_drawingBoat?._entityBoat?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) => _drawingBoat?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) => _drawingBoat?.MoveBoat(direction);
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab.Entity;
using System.Drawing;
namespace SpeedBoatLab.Drawings
{
public class DrawingSpeedBoat : DrawingBoat
{
public DrawingSpeedBoat(int speed, double weight, Color mainColor, Color secondColor, bool _isMotor, bool _isProtectedGlass, int width, int height) :
base(speed, weight, mainColor, width, height, 100, 80)
{
if (_entityBoat != null)
{
_entityBoat = new EntitySpeedboat(speed, weight, mainColor, secondColor, _isMotor, _isProtectedGlass);
}
}
public override void DrawTransport(Graphics g)
{
if (_entityBoat is not EntitySpeedboat speedBoat)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(speedBoat.SecondColor);
#region Координаты защитного стекла
Point g1 = new Point(startXCoord + 70, startYCoord + 25);
Point g2 = new Point(startXCoord + 80, startYCoord + 20);
Point g3 = new Point(startXCoord + 80, startYCoord + 60);
Point g4 = new Point(startXCoord + 70, startYCoord + 55);
Point[] pointsGlass = { g1, g2, g3, g4 };
#endregion
//мотор
if (speedBoat.isMotor)
{
g.DrawRectangle(pen, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60);
g.FillRectangle(additionalBrush, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60);
}
//защитное стекло
if (speedBoat.isProtectedGlass)
{
g.DrawPolygon(pen, pointsGlass);
g.FillPolygon(new SolidBrush(Color.Aqua), pointsGlass);
}
base.DrawTransport(g);
}
}
}

View File

@ -5,9 +5,12 @@ using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace SpeedBoatLab
namespace SpeedBoatLab.Entity
{
public class SpeedBoat
/// <summary>
/// Катер
/// </summary>
public class EntityBoat
{
/// <summary>
/// Скорость катера
@ -19,39 +22,21 @@ namespace SpeedBoatLab
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Наличие мотора
/// </summary>
public bool isMotor { get; private set; }
/// <summary>
/// Наличие защитного стекла
/// </summary>
public bool isProtectedGlass { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color MainColor { get; private set; }
/// <summary>
/// Доп. цвет
/// </summary>
public Color SecondColor { get; private set; }
/// <summary>
/// Шаг перемещения катера
/// </summary>
public double Step => (double)Speed * 100 / (Weight);
public void Init(int speed, double weight, bool _isMotor, bool _isProtectedGlass, Color mainColor, Color secondColor)
public EntityBoat(int speed, double weight, Color mainColor)
{
Speed = speed;
Weight = weight;
isMotor = _isMotor;
isProtectedGlass = _isProtectedGlass;
MainColor = mainColor;
SecondColor = secondColor;
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace SpeedBoatLab.Entity
{
/// <summary>
/// Класс-сущность скоростного катера
/// </summary>
public class EntitySpeedboat : EntityBoat
{
/// <summary>
/// Наличие мотора
/// </summary>
public bool isMotor { get; private set; }
/// <summary>
/// Наличие защитного стекла
/// </summary>
public bool isProtectedGlass { get; private set; }
/// <summary>
/// Доп. цвет
/// </summary>
public Color SecondColor { get; private set; }
/// <summary>
/// Параметры катера
/// </summary>
public EntitySpeedboat(int speed, double weight, Color mainColor, Color secondColor, bool _isMotor, bool _isProtectedGlass) :
base(speed, weight, mainColor)
{
isMotor = _isMotor;
isProtectedGlass = _isProtectedGlass;
SecondColor = secondColor;
}
}
}

View File

@ -35,6 +35,9 @@ namespace SpeedBoatLab
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.StepButton = new System.Windows.Forms.Button();
this.buttonCreateSpeedBoat = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxSpeedBoat)).BeginInit();
this.SuspendLayout();
//
@ -56,7 +59,7 @@ namespace SpeedBoatLab
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(103, 51);
this.buttonCreate.TabIndex = 2;
this.buttonCreate.Text = "Создать обьект";
this.buttonCreate.Text = "Создать катер";
this.buttonCreate.UseVisualStyleBackColor = false;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
@ -112,11 +115,52 @@ namespace SpeedBoatLab
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"MoveToCenter",
"MoveToBorder"});
this.comboBox1.Location = new System.Drawing.Point(637, 12);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(151, 28);
this.comboBox1.TabIndex = 11;
//
// StepButton
//
this.StepButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.StepButton.BackColor = System.Drawing.Color.White;
this.StepButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.StepButton.Location = new System.Drawing.Point(731, 46);
this.StepButton.Name = "StepButton";
this.StepButton.Size = new System.Drawing.Size(57, 33);
this.StepButton.TabIndex = 12;
this.StepButton.Text = "Шаг";
this.StepButton.UseVisualStyleBackColor = false;
this.StepButton.Click += new System.EventHandler(this.StepButton_Click);
//
// buttonCreateSpeedBoat
//
this.buttonCreateSpeedBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateSpeedBoat.BackColor = System.Drawing.Color.White;
this.buttonCreateSpeedBoat.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonCreateSpeedBoat.Location = new System.Drawing.Point(121, 387);
this.buttonCreateSpeedBoat.Name = "buttonCreateSpeedBoat";
this.buttonCreateSpeedBoat.Size = new System.Drawing.Size(143, 51);
this.buttonCreateSpeedBoat.TabIndex = 13;
this.buttonCreateSpeedBoat.Text = "Создать скоростной катер";
this.buttonCreateSpeedBoat.UseVisualStyleBackColor = false;
this.buttonCreateSpeedBoat.Click += new System.EventHandler(this.buttonCreateSpeedBoat_Click);
//
// FormSpeedBoat
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonCreateSpeedBoat);
this.Controls.Add(this.StepButton);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonDown);
@ -138,6 +182,9 @@ namespace SpeedBoatLab
private System.Windows.Forms.Button buttonLeft;
private System.Windows.Forms.Button buttonDown;
private System.Windows.Forms.Button buttonRight;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button StepButton;
private System.Windows.Forms.Button buttonCreateSpeedBoat;
}
}

View File

@ -7,6 +7,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SpeedBoatLab.Drawings;
using SpeedBoatLab.Entity;
using speed_Boat.MovementStrategy;
namespace SpeedBoatLab
{
@ -15,25 +18,30 @@ namespace SpeedBoatLab
/// <summary>
/// Поле-объект для прорисовки объекта
/// </summary>
private SpeedBoatMovement? _tankerMovement;
private DrawingBoat? _boatMovement;
/// <summary>
/// Стратегия перемещения
/// </summary>
private AbstractStrategy? _abstractStrategy;
public FormSpeedBoat()
{
InitializeComponent();
}
/// <summary>
/// Метод прорисовки машины
/// Метод прорисовки лодки
/// </summary>
private void Draw()
{
if (_tankerMovement == null)
if (_boatMovement == null)
{
return;
}
Bitmap bmp = new(pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height);
Graphics gr = Graphics.FromImage(bmp);
_tankerMovement.DrawTransport(gr);
_boatMovement.DrawTransport(gr);
pictureBoxSpeedBoat.Image = bmp;
}
@ -43,18 +51,33 @@ namespace SpeedBoatLab
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_tankerMovement = new SpeedBoatMovement();
_tankerMovement.Init(random.Next(100, 300),
_boatMovement = new DrawingBoat(random.Next(100, 300),
random.Next(1000, 3000),
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
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)),
pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height);
//startXCoord and startYCoord
_tankerMovement.SetPosition(random.Next(10, 100), random.Next(10, 100));
_boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Обработка создания скоростного обьекта
/// </summary>
private void buttonCreateSpeedBoat_Click(object sender, EventArgs e)
{
Random random = new();
_boatMovement = new DrawingSpeedBoat(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)),
pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height);
//startXCoord and startYCoord
_boatMovement.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
@ -64,7 +87,7 @@ namespace SpeedBoatLab
/// </summary>
private void buttonMove_Click(object sender, EventArgs e)
{
if (_tankerMovement == null)
if (_boatMovement == null)
{
return;
}
@ -72,19 +95,56 @@ namespace SpeedBoatLab
switch (name)
{
case "buttonUp":
_tankerMovement.MoveTransport(DirectionType.Up);
_boatMovement.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_tankerMovement.MoveTransport(DirectionType.Down);
_boatMovement.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_tankerMovement.MoveTransport(DirectionType.Left);
_boatMovement.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_tankerMovement.MoveTransport(DirectionType.Right);
_boatMovement.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
private void StepButton_Click(object sender, EventArgs e)
{
if(_boatMovement == null)
{
return;
}
if(comboBox1.Enabled)
{
_abstractStrategy = comboBox1.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null
};
if(_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectBoat(_boatMovement), pictureBoxSpeedBoat.Width, pictureBoxSpeedBoat.Height);
comboBox1.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if(_abstractStrategy.GetStatus() == Status.Finish)
{
comboBox1.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab.Drawings;
namespace speed_Boat.MovementStrategy
{
public interface IMovementObject
{
//Получение координаты Х обьекта
ObjectParameters? GetObjectPosition { get; }
//шаг обьекта
int GetStep { get; }
//проверка на перемещение
bool CheckCanMove(DirectionType direction);
//изменение направления перемещения обьекта
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace speed_Boat.MovementStrategy
{
class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
//return objParams.RightBorder == FieldWidth && objParams.DownBorder == FieldHeight;
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 = FieldWidth - objParams.RightBorder;
if (Math.Abs(diffX) > GetStep())
{
MoveRight();
}
var diffY = FieldHeight - objParams.DownBorder;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab.Drawings;
using SpeedBoatLab.Entity;
namespace speed_Boat.MovementStrategy
{
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();
}
}
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace speed_Boat.MovementStrategy
{
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>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -5,16 +5,17 @@ using System.Text;
using System.Threading.Tasks;
using SpeedBoatLab;
using System.Drawing;
using SpeedBoatLab.Entity;
namespace SpeedBoatLab
namespace SpeedBoatLab.Drawings
{
class SpeedBoatMovement
public class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public SpeedBoat? _speedBoat { get; private set; }
public EntityBoat? _entityBoat { get; protected set; }
/// <summary>
/// Ширина окна
@ -28,27 +29,92 @@ namespace SpeedBoatLab
/// <summary>
/// Х-координата обьекта
/// </summary>
private int startXCoord;
protected int startXCoord;
/// <summary>
/// Y-координата обьекта
/// </summary>
private int startYCoord;
protected int startYCoord;
/// <summary>
/// Ширина обьекта
/// </summary>
private readonly int widthBoat = 100;
protected readonly int widthBoat = 100;
/// <summary>
/// Высота обьекта
/// </summary>
private readonly int heightBoat = 80;
protected readonly int heightBoat = 80;
public bool Init(int speed, double weight, bool _isMotor, bool _isProtectedGlass, Color mainColor, Color secondColor, int width, int height)
/// <summary>
/// Х-координата обьекта
/// </summary>
public int GetPoseX => startXCoord;
/// <summary>
/// Y-координата обьекта
/// </summary>
public int GetPoseY => startYCoord;
/// <summary>
/// Ширина обьекта
/// </summary>
public int GetWidth => widthBoat;
/// <summary>
/// Высота обьекта
/// </summary>
public int GetHeight => heightBoat;
public bool CanMove(DirectionType direction)
{
if (_entityBoat == null)
{
return false;
}
return direction switch
{
//Left
DirectionType.Left => startXCoord - _entityBoat.Step > 0,
//Up
DirectionType.Up => startYCoord - _entityBoat.Step > 0,
//Down
DirectionType.Down => startYCoord + _entityBoat.Step < screenHeight,
//Right
DirectionType.Right => startXCoord + _entityBoat.Step < screenWidth
};
}
//Изменение направления перемещения
public void MoveTransport(DirectionType direction)
{
if(!CanMove(direction) || _entityBoat == null)
{
return;
}
switch(direction)
{
case DirectionType.Left:
startXCoord -= (int)_entityBoat.Step;
break;
case DirectionType.Up:
startYCoord -= (int)_entityBoat.Step;
break;
case DirectionType.Right:
startXCoord += (int)_entityBoat.Step;
break;
case DirectionType.Down:
startYCoord += (int)_entityBoat.Step;
break;
}
}
/// <summary>
/// конструктор
/// </summary>
public DrawingBoat(int speed, double weight, Color mainColor, int width, int height)
{
screenWidth = width;
screenHeight = height;
_speedBoat = new SpeedBoat();
_speedBoat.Init(speed, weight, _isMotor, _isProtectedGlass, mainColor, secondColor);
_entityBoat = new EntityBoat(speed, weight, mainColor);
/// <summary>
/// Проверка на вместимость обьекта в рамки сцены
@ -56,13 +122,30 @@ namespace SpeedBoatLab
if ((widthBoat >= screenWidth) || (heightBoat >= screenHeight))
{
Console.WriteLine("проверка не пройдена, нельзя создать объект в этих размерах");
return false;
if(widthBoat >= screenWidth)
{
widthBoat = screenWidth - widthBoat;
}
if (heightBoat >= screenWidth)
{
heightBoat = screenWidth - heightBoat;
}
}
else
Console.WriteLine("объект создан");
return true;
}
/// <summary>
/// конструктор
/// </summary>
protected DrawingBoat(int speed, double weight, Color mainColor, int width, int height, int _widthBoat, int _heightBoat)
{
screenWidth = width;
screenHeight = height;
widthBoat = _widthBoat;
heightBoat = _heightBoat;
_entityBoat = new EntityBoat(speed, weight, mainColor);
}
/// <summary>
/// Установка позиции
/// </summary>
@ -83,9 +166,9 @@ namespace SpeedBoatLab
/// <summary>
/// Изменение направления перемещения
/// </summary>
public void MoveTransport(DirectionType direction)
public void MoveBoat(DirectionType direction)
{
if (_speedBoat == null)
if (_entityBoat == null)
{
return;
}
@ -94,30 +177,30 @@ namespace SpeedBoatLab
{
//влево
case DirectionType.Left:
if (startXCoord - _speedBoat.Step > 0)
if (startXCoord - _entityBoat.Step > 0)
{
startXCoord -= (int)_speedBoat.Step;
startXCoord -= (int)_entityBoat.Step;
}
break;
//вверх
case DirectionType.Up:
if (startYCoord - _speedBoat.Step > 0)
if (startYCoord - _entityBoat.Step > 0)
{
startYCoord -= (int)_speedBoat.Step;
startYCoord -= (int)_entityBoat.Step;
}
break;
// вправо
case DirectionType.Right:
if (startXCoord + _speedBoat.Step + widthBoat < screenWidth)
if (startXCoord + _entityBoat.Step + widthBoat < screenWidth)
{
startXCoord += (int)_speedBoat.Step;
startXCoord += (int)_entityBoat.Step;
}
break;
//вниз
case DirectionType.Down:
if (startYCoord + _speedBoat.Step + heightBoat < screenHeight)
if (startYCoord + _entityBoat.Step + heightBoat < screenHeight)
{
startYCoord += (int)_speedBoat.Step;
startYCoord += (int)_entityBoat.Step;
}
break;
}
@ -126,16 +209,15 @@ namespace SpeedBoatLab
/// <summary>
/// Прорисовка объекта
/// </summary>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_speedBoat == null)
if (_entityBoat == null)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(_speedBoat.SecondColor);
Brush mainBrush = new SolidBrush(_speedBoat.MainColor);
Brush mainBrush = new SolidBrush(_entityBoat.MainColor);
#region Координаты переда лодки
Point b1 = new Point(startXCoord + 80, startYCoord + 20);
@ -144,39 +226,14 @@ namespace SpeedBoatLab
Point[] pointsBoat = { b1, b2, b3 };
#endregion
#region Координаты защитного стекла
Point g1 = new Point(startXCoord + 70, startYCoord + 25);
Point g2 = new Point(startXCoord + 80, startYCoord + 20);
Point g3 = new Point(startXCoord + 80, startYCoord + 60);
Point g4 = new Point(startXCoord + 70, startYCoord + 55);
Point[] pointsGlass = { g1, g2, g3, g4 };
#endregion
//основа катера
g.DrawRectangle(pen, startXCoord + 20, startYCoord + 20, widthBoat - 40, heightBoat - 40);
g.DrawEllipse(pen, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50);
g.DrawPolygon(pen, pointsBoat);
g.FillRectangle(mainBrush, startXCoord + 20, startYCoord + 20, widthBoat - 40, heightBoat - 40);
g.FillEllipse(additionalBrush, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50);
g.FillEllipse(mainBrush, startXCoord + 25, startYCoord + 25, widthBoat - 50, heightBoat - 50);
g.FillPolygon(mainBrush, pointsBoat);
//мотор
if (_speedBoat.isMotor)
{
g.DrawRectangle(pen, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60);
g.FillRectangle(additionalBrush, startXCoord + 10, startYCoord + 30, widthBoat - 90, heightBoat - 60);
}
//защитное стекло
if (_speedBoat.isProtectedGlass)
{
g.DrawPolygon(pen, pointsGlass);
g.FillPolygon(new SolidBrush(Color.Aqua), pointsGlass);
}
}
}
}

View File

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