LabWork02
This commit is contained in:
parent
8271110f01
commit
b3e0208941
@ -1,171 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat
|
||||
{
|
||||
public class DrawningMotorboat
|
||||
{
|
||||
public EntityMotorboat? EntityMotorboat { get; private set; }
|
||||
|
||||
private int? _pictureWidth;
|
||||
|
||||
private int? _pictureHeight;
|
||||
|
||||
private int? _startPosX;
|
||||
|
||||
private int? _startPosY;
|
||||
private readonly int _drawningMotorboatWidth = 160;
|
||||
|
||||
private readonly int _drawningMotorboatHeight = 60;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool motor, bool glass)
|
||||
{
|
||||
EntityMotorboat = new EntityMotorboat();
|
||||
EntityMotorboat.Init(speed, weight, bodyColor, additionalColor,
|
||||
motor, glass);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
if (_drawningMotorboatWidth < width && _drawningMotorboatHeight < height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||||
{
|
||||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (x + _drawningMotorboatWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningMotorboatWidth;
|
||||
}
|
||||
else if (x < 0)
|
||||
{
|
||||
_startPosX = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_startPosX = x;
|
||||
}
|
||||
if (y + _drawningMotorboatHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningMotorboatHeight;
|
||||
}
|
||||
else if (y < 0)
|
||||
{
|
||||
_startPosY = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityMotorboat == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityMotorboat.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityMotorboat.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityMotorboat.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityMotorboat.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + EntityMotorboat.Step + _drawningMotorboatWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityMotorboat.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + EntityMotorboat.Step + _drawningMotorboatHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityMotorboat.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityMotorboat == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{ return; }
|
||||
Pen pen = new(Color.Black, 1);
|
||||
Brush mainBrush = new SolidBrush(EntityMotorboat.BodyColor);
|
||||
|
||||
// корпус
|
||||
Point[] hull = new Point[]
|
||||
{
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 0),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 0),
|
||||
new Point(_startPosX.Value + 160, _startPosY.Value + 35),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 70),
|
||||
};
|
||||
g.FillPolygon(mainBrush, hull);
|
||||
g.DrawPolygon(pen, hull);
|
||||
|
||||
// стекло впереди
|
||||
if (EntityMotorboat.Glass) {
|
||||
Brush glassBrush = new SolidBrush(Color.LightBlue);
|
||||
g.FillEllipse(glassBrush, _startPosX.Value + 20, _startPosY.Value + 15, 100, 40);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 15, 100, 40);
|
||||
}
|
||||
|
||||
// основная часть
|
||||
Brush blockBrush = new SolidBrush(EntityMotorboat.AdditionalColor);
|
||||
g.FillRectangle(blockBrush, _startPosX.Value + 20, _startPosY.Value + 15, 80, 40);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 15, 80, 40);
|
||||
|
||||
// двигатель
|
||||
if (EntityMotorboat.Motor)
|
||||
{
|
||||
Brush engineBrush = new
|
||||
SolidBrush(EntityMotorboat.AdditionalColor);
|
||||
g.FillRectangle(engineBrush, _startPosX.Value + 0, _startPosY.Value + 10, 5, 50);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value + 10, 5, 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,10 +4,11 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat
|
||||
namespace ProjectMotorboat.Drownings
|
||||
{
|
||||
public enum DirectionType
|
||||
{
|
||||
Unknow = -1,
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
170
ProjectMotorboat/ProjectMotorboat/Drownings/DrawningBoat.cs
Normal file
170
ProjectMotorboat/ProjectMotorboat/Drownings/DrawningBoat.cs
Normal file
@ -0,0 +1,170 @@
|
||||
using ProjectMotorboat.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.Drownings;
|
||||
|
||||
public class DrawningBoat
|
||||
{
|
||||
public EntityBoat? EntityBoat { get; protected set; }
|
||||
|
||||
private int? _pictureWidth;
|
||||
|
||||
private int? _pictureHeight;
|
||||
|
||||
protected int? _startPosX;
|
||||
|
||||
protected int? _startPosY;
|
||||
private readonly int _drawningBoatWidth = 160;
|
||||
|
||||
private readonly int _drawningBoatHeight = 60;
|
||||
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
|
||||
public int GetWidth => _drawningBoatWidth;
|
||||
|
||||
|
||||
public int GetHeight => _drawningBoatHeight;
|
||||
private DrawningBoat()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
public DrawningBoat(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
protected DrawningBoat(int drawningBoatWidth, int drawningBoatHeight) : this()
|
||||
{
|
||||
_drawningBoatWidth = drawningBoatWidth;
|
||||
_drawningBoatHeight = drawningBoatHeight;
|
||||
|
||||
}
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
if (_drawningBoatWidth < width && _drawningBoatHeight < height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||||
{
|
||||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (x + _drawningBoatWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningBoatWidth;
|
||||
}
|
||||
else if (x < 0)
|
||||
{
|
||||
_startPosX = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_startPosX = x;
|
||||
}
|
||||
if (y + _drawningBoatHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningBoatHeight;
|
||||
}
|
||||
else if (y < 0)
|
||||
{
|
||||
_startPosY = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityBoat == null || !_startPosX.HasValue ||
|
||||
!_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityBoat.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityBoat.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityBoat.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityBoat.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + EntityBoat.Step + _drawningBoatWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityBoat.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + EntityBoat.Step + _drawningBoatHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityBoat.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{ return; }
|
||||
Pen pen = new(Color.Black);
|
||||
Brush mainBrush = new SolidBrush(EntityBoat.BodyColor);
|
||||
|
||||
// корпус
|
||||
Point[] hull = new Point[]
|
||||
{
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 0),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 0),
|
||||
new Point(_startPosX.Value + 160, _startPosY.Value + 35),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 5, _startPosY.Value + 70),
|
||||
};
|
||||
g.FillPolygon(mainBrush, hull);
|
||||
g.DrawPolygon(pen, hull);
|
||||
|
||||
// основная часть
|
||||
Brush blockBrush = new SolidBrush(EntityBoat.BodyColor);
|
||||
g.FillRectangle(blockBrush, _startPosX.Value + 20, _startPosY.Value + 15, 80, 40);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 15, 80, 40);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
using ProjectMotorboat.Entities;
|
||||
|
||||
namespace ProjectMotorboat.Drownings;
|
||||
|
||||
public class DrawningMotorboat : DrawningBoat
|
||||
{
|
||||
public DrawningMotorboat(int speed, double weight, Color bodyColor, Color additionalColor, bool motor, bool glass) : base (160, 60)
|
||||
{
|
||||
EntityBoat = new EntityMotorboat(speed, weight, bodyColor, additionalColor, motor, glass);
|
||||
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBoat == null || EntityBoat is not EntityMotorboat motorboat|| !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(motorboat.AdditionalColor);
|
||||
|
||||
base.DrawTransport(g);
|
||||
|
||||
// стекло впереди
|
||||
if (motorboat.Glass)
|
||||
{
|
||||
Brush glassBrush = new SolidBrush(Color.LightBlue);
|
||||
g.FillEllipse(glassBrush, _startPosX.Value + 20, _startPosY.Value + 15, 100, 40);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 15, 100, 40);
|
||||
}
|
||||
// двигатель
|
||||
if (motorboat.Motor)
|
||||
{
|
||||
Brush engineBrush = new
|
||||
SolidBrush(motorboat.AdditionalColor);
|
||||
g.FillRectangle(engineBrush, _startPosX.Value + 0, _startPosY.Value + 10, 5, 50);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value + 10, 5, 50);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
24
ProjectMotorboat/ProjectMotorboat/Entities/EntityBoat.cs
Normal file
24
ProjectMotorboat/ProjectMotorboat/Entities/EntityBoat.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.Entities;
|
||||
|
||||
public class EntityBoat
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
|
||||
public double Weight { get; private set; }
|
||||
|
||||
public Color BodyColor { get; private set; }
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
public EntityBoat(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
namespace ProjectMotorboat.Entities;
|
||||
|
||||
public class EntityMotorboat : EntityBoat
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public bool Motor { get; private set; }
|
||||
|
||||
public bool Glass { get; private set; }
|
||||
|
||||
public EntityMotorboat(int speed, double weight, Color bodyColor, Color additionalColor, bool motor, bool glass) : base (speed, weight, bodyColor)
|
||||
{
|
||||
|
||||
AdditionalColor = additionalColor;
|
||||
Motor = motor;
|
||||
Glass = glass;
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat;
|
||||
|
||||
public class EntityMotorboat
|
||||
{
|
||||
public int Speed { get; private set; }
|
||||
|
||||
public double Weight { get; private set; }
|
||||
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public bool Motor { get; private set; }
|
||||
|
||||
public bool Glass { get; private set; }
|
||||
|
||||
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, bool motor, bool glass )
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Motor = motor;
|
||||
Glass = glass;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -34,6 +34,9 @@
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreateBoat = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxMotorboat).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -51,11 +54,11 @@
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 409);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(94, 29);
|
||||
buttonCreate.Size = new Size(240, 29);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.Text = "Создать моторную лодку";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreate.Click += ButtonCreate_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
@ -106,11 +109,45 @@
|
||||
buttonUp.Click += ButtonMove_Click;
|
||||
buttonUp.MouseClick += ButtonMove_Click;
|
||||
//
|
||||
// buttonCreateBoat
|
||||
//
|
||||
buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateBoat.Location = new Point(275, 409);
|
||||
buttonCreateBoat.Name = "buttonCreateBoat";
|
||||
buttonCreateBoat.Size = new Size(240, 29);
|
||||
buttonCreateBoat.TabIndex = 6;
|
||||
buttonCreateBoat.Text = "Создать лодку";
|
||||
buttonCreateBoat.UseVisualStyleBackColor = true;
|
||||
buttonCreateBoat.Click += ButtonBoat_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
comboBoxStrategy.Location = new Point(604, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(184, 28);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(686, 46);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(93, 28);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += ButtonStrategyStep_Click;
|
||||
//
|
||||
// FormMotorboat
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateBoat);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
@ -131,5 +168,8 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreateBoat;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,85 +1,126 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using ProjectMotorboat.Drownings;
|
||||
using ProjectMotorboat.MovementStrategy;
|
||||
|
||||
namespace ProjectMotorboat
|
||||
namespace ProjectMotorboat;
|
||||
|
||||
public partial class FormMotorboat : Form
|
||||
{
|
||||
public partial class FormMotorboat : Form
|
||||
private DrawningBoat? _drawningBoat;
|
||||
private AbstractStrategy? _strategy;
|
||||
public FormMotorboat()
|
||||
{
|
||||
private DrawningMotorboat? _drawningMotorboat;
|
||||
public FormMotorboat()
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningBoat == null)
|
||||
{
|
||||
InitializeComponent();
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxMotorboat.Width, pictureBoxMotorboat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningBoat.DrawTransport(gr);
|
||||
pictureBoxMotorboat.Image = bmp;
|
||||
}
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawningBoat):
|
||||
_drawningBoat = new DrawningBoat(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||
break;
|
||||
case nameof(DrawningMotorboat):
|
||||
_drawningBoat = new DrawningMotorboat(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)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningMotorboat = new DrawningMotorboat();
|
||||
_drawningMotorboat.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)));
|
||||
_drawningMotorboat.SetPictureSize(pictureBoxMotorboat.Width, pictureBoxMotorboat.Height);
|
||||
_drawningMotorboat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_drawningBoat.SetPictureSize(pictureBoxMotorboat.Width, pictureBoxMotorboat.Height);
|
||||
_drawningBoat.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningMotorboat));
|
||||
|
||||
Bitmap bmp = new(pictureBoxMotorboat.Width, pictureBoxMotorboat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningMotorboat.DrawTransport(gr);
|
||||
pictureBoxMotorboat.Image = bmp;
|
||||
|
||||
private void ButtonBoat_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBoat));
|
||||
|
||||
|
||||
|
||||
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningBoat == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
if (_drawningMotorboat == null)
|
||||
case "buttonUp":
|
||||
result = _drawningBoat.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningBoat.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningBoat.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningBoat.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningBoat == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxMotorboat.Width,
|
||||
pictureBoxMotorboat.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningMotorboat.DrawTransport(gr);
|
||||
pictureBoxMotorboat.Image = bmp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningMotorboat == null)
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableBoat(_drawningBoat), pictureBoxMotorboat.Width, pictureBoxMotorboat.Height);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
|
||||
comboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningMotorboat.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningMotorboat.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningMotorboat.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningMotorboat.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
|
||||
private IMoveableObject? _moveableObject;
|
||||
|
||||
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
|
||||
|
||||
protected int FieldWidth { get; private set; }
|
||||
|
||||
|
||||
protected int FieldHeight { get; private set; }
|
||||
|
||||
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
|
||||
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
|
||||
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
|
||||
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
|
||||
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
|
||||
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
|
||||
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
public interface IMoveableObject
|
||||
{
|
||||
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
int GetStep { get; }
|
||||
|
||||
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
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.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
int diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
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 (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
using ProjectMotorboat.Drownings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
public class MoveableBoat : IMoveableObject
|
||||
{
|
||||
private readonly DrawningBoat? _boat = null;
|
||||
public MoveableBoat(DrawningBoat boat)
|
||||
{
|
||||
_boat = boat;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_boat == null || _boat.EntityBoat == null || !_boat.GetPosX.HasValue || !_boat.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_boat.GetPosX.Value, _boat.GetPosY.Value, _boat.GetWidth, _boat.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_boat?.EntityBoat?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_boat == null || _boat.EntityBoat == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _boat.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
|
||||
Up = 1,
|
||||
|
||||
|
||||
Down = 2,
|
||||
|
||||
|
||||
Left = 3,
|
||||
|
||||
|
||||
Right = 4
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
|
||||
private readonly int _x;
|
||||
|
||||
|
||||
private readonly int _y;
|
||||
|
||||
|
||||
private readonly int _width;
|
||||
|
||||
|
||||
private readonly int _height;
|
||||
|
||||
|
||||
public int LeftBorder => _x;
|
||||
|
||||
|
||||
public int TopBorder => _y;
|
||||
|
||||
|
||||
public int RightBorder => _x + _width;
|
||||
|
||||
|
||||
public int DownBorder => _y + _height;
|
||||
|
||||
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
|
||||
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectMotorboat.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
NotInit,
|
||||
|
||||
|
||||
InProgress,
|
||||
|
||||
|
||||
Finish
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user