lab-2 done

This commit is contained in:
strwbrry1 2024-03-06 16:13:49 +04:00
parent f633bde48a
commit 70f40c1f99
16 changed files with 642 additions and 135 deletions

View File

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
public class EntityCatamaran
{
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 LeftBobber { get; private set; }
public bool RightBobber { get; private set; }
public bool Sail { get; private set; }
public double Step => Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
LeftBobber = leftBobber;
RightBobber = rightBobber;
Sail = sail;
}
}
}

View File

@ -4,10 +4,11 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Catamaran namespace Catamaran.Drawings
{ {
internal enum DirectionType public enum DirectionType
{ {
Unknown = -1,
Up = 1, Up = 1,
Down = 2, Down = 2,
Left = 3, Left = 3,

View File

@ -1,40 +1,58 @@
using System; using Catamaran.Entities;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Catamaran namespace Catamaran.Drawings
{ {
internal class DrawingCatamaran public class DrawingBoat
{ {
public EntityCatamaran? EntityCatamaran { get; private set; } public EntityBoat? EntityBoat { get; protected set; }
private int? _pictureWidth; private int? _pictureWidth;
private int? _pictureHeight; private int? _pictureHeight;
private int? _startPosX; protected int? _startPosX;
private int? _startPosY; protected int? _startPosY;
private readonly int _drawingCatamaranWidth = 100; private readonly int _drawingCatamaranWidth = 100;
private readonly int _drawingCatamaranHeight = 60; private readonly int _drawingCatamaranHeight = 40;
public int? GetPosX => _startPosX;
public int? GetPosY => _startPosY;
public int GetWidth => _drawingCatamaranWidth;
public int GetHeight => _drawingCatamaranHeight;
//public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail)
public void Init(EntityCatamaran entityCatamaran) private DrawingBoat()
{ {
EntityCatamaran = entityCatamaran;
//EntityCatamaran.Init(speed, weight, bodyColor, additionalColor, leftBobber, rightBobber, sail);
_pictureHeight = null; _pictureHeight = null;
_pictureWidth = null; _pictureWidth = null;
_startPosX = null; _startPosX = null;
_startPosY = null; _startPosY = null;
} }
public DrawingBoat(int speed, double weight, Color bodyColor) : this()
{
EntityBoat = new EntityBoat(speed, weight, bodyColor);
}
protected DrawingBoat(int drawingCatamaranWidth, int drawingCatamaranHeight) : this()
{
_drawingCatamaranWidth = drawingCatamaranWidth;
_drawingCatamaranHeight = drawingCatamaranHeight;
}
public bool SetPictureSize(int width, int height) public bool SetPictureSize(int width, int height)
{ {
if (width > _drawingCatamaranWidth && height > _drawingCatamaranHeight) if (width > _drawingCatamaranWidth && height > _drawingCatamaranHeight)
@ -54,13 +72,13 @@ namespace Catamaran
_startPosY = _pictureHeight - _drawingCatamaranHeight; _startPosY = _pictureHeight - _drawingCatamaranHeight;
} }
} }
return true; return true;
} }
return false; return false;
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{ {
@ -81,12 +99,12 @@ namespace Catamaran
_startPosY = _pictureHeight - _drawingCatamaranHeight; _startPosY = _pictureHeight - _drawingCatamaranHeight;
} }
} }
} }
public bool MoveTransport(DirectionType direction) public bool MoveTransport(DirectionType direction)
{ {
if (EntityCatamaran == null || !_startPosX.HasValue || !_startPosY.HasValue) if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue)
{ {
return false; return false;
} }
@ -94,30 +112,30 @@ namespace Catamaran
{ {
//влево //влево
case DirectionType.Left: case DirectionType.Left:
if (_startPosX.Value - EntityCatamaran.Step > 0) if (_startPosX.Value - EntityBoat.Step > 0)
{ {
_startPosX -= (int)EntityCatamaran.Step; _startPosX -= (int)EntityBoat.Step;
} }
return true; return true;
//вверх //вверх
case DirectionType.Up: case DirectionType.Up:
if (_startPosY.Value - EntityCatamaran.Step > 0) if (_startPosY.Value - EntityBoat.Step > 0)
{ {
_startPosY -= (int)EntityCatamaran.Step; _startPosY -= (int)EntityBoat.Step;
} }
return true; return true;
// вправо // вправо
case DirectionType.Right: case DirectionType.Right:
if (_startPosX.Value + _drawingCatamaranWidth + EntityCatamaran.Step < _pictureWidth) if (_startPosX.Value + _drawingCatamaranWidth + EntityBoat.Step < _pictureWidth)
{ {
_startPosX += (int)EntityCatamaran.Step; _startPosX += (int)EntityBoat.Step;
} }
return true; return true;
//вниз //вниз
case DirectionType.Down: case DirectionType.Down:
if(_startPosY.Value + _drawingCatamaranHeight + EntityCatamaran.Step < _pictureHeight) if (_startPosY.Value + _drawingCatamaranHeight + EntityBoat.Step < _pictureHeight)
{ {
_startPosY += (int)EntityCatamaran.Step; _startPosY += (int)EntityBoat.Step;
} }
return true; return true;
default: default:
@ -126,64 +144,37 @@ namespace Catamaran
} }
public void DrawTransport(Graphics g) public virtual void DrawTransport(Graphics g)
{ {
if (EntityCatamaran == null || !_startPosX.HasValue || !_startPosY.HasValue) if (EntityBoat == null || !_startPosX.HasValue || !_startPosY.HasValue)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush baseBrush = new SolidBrush(EntityCatamaran.BodyColor); Brush baseBrush = new SolidBrush(EntityBoat.BodyColor);
Brush additionalBrush = new SolidBrush(EntityCatamaran.AdditionalColor);
Brush brBrush = new SolidBrush(Color.Brown); Brush brBrush = new SolidBrush(Color.Brown);
// левый поплавок
if (EntityCatamaran.LeftBobber)
{
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 8, 100, 15);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 8, 100, 15);
}
// правый поплавок
if (EntityCatamaran.RightBobber)
{
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 37, 100, 15);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 37, 100, 15);
}
// начало отрисовки // начало отрисовки
Point[] body = Point[] body =
{ {
new Point(_startPosX.Value + 15, _startPosY.Value + 15), new Point(_startPosX.Value + 10, _startPosY.Value + 10),
new Point(_startPosX.Value + 80, _startPosY.Value + 15), new Point(_startPosX.Value + 75, _startPosY.Value + 10),
new Point(_startPosX.Value + 100, _startPosY.Value + 30), new Point(_startPosX.Value + 95, _startPosY.Value + 25),
new Point(_startPosX.Value + 80, _startPosY.Value + 45), new Point(_startPosX.Value + 75, _startPosY.Value + 40),
new Point(_startPosX.Value + 15, _startPosY.Value + 45), new Point(_startPosX.Value + 10, _startPosY.Value + 40),
new Point(_startPosX.Value + 15, _startPosY.Value + 15) new Point(_startPosX.Value + 10, _startPosY.Value + 10)
}; };
g.FillPolygon(baseBrush, body); g.FillPolygon(baseBrush, body);
g.DrawPolygon(pen, body); g.DrawPolygon(pen, body);
g.FillEllipse(brBrush, _startPosX.Value + 20, _startPosY.Value + 20, 60, 20); g.FillEllipse(brBrush, _startPosX.Value + 15, _startPosY.Value + 15, 60, 20);
g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 20, 60, 20); g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 15, 60, 20);
// конец отрисовки
// парус // конец отрисовки
if (EntityCatamaran.Sail)
{
Point[] sail =
{
new Point(_startPosX.Value + 50, _startPosY.Value + 30),
new Point(_startPosX.Value + 50, _startPosY.Value + 5),
new Point(_startPosX.Value + 80, _startPosY.Value + 25),
new Point(_startPosX.Value + 50, _startPosY.Value + 25),
};
g.FillPolygon(additionalBrush, sail);
g.DrawPolygon(pen, sail);
}
} }
} }
} }

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Catamaran.Entities;
namespace Catamaran.Drawings
{
public class DrawingCatamaran : DrawingBoat
{
public DrawingCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail) : base(120, 90)
{
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, leftBobber, rightBobber, sail);
}
public override void DrawTransport(Graphics g)
{
if (EntityBoat == null || EntityBoat is not EntityCatamaran EntityCatamaran || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush baseBrush = new SolidBrush(EntityCatamaran.BodyColor);
Brush additionalBrush = new SolidBrush(EntityCatamaran.AdditionalColor);
Brush brBrush = new SolidBrush(Color.Brown);
// левый поплавок
if (EntityCatamaran.LeftBobber)
{
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 8, 100, 15);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 8, 100, 15);
}
// правый поплавок
if (EntityCatamaran.RightBobber)
{
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 37, 100, 15);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 37, 100, 15);
}
// начало отрисовки
_startPosX += 5;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 5;
_startPosY -= 5;
// конец отрисовки
// парус
if (EntityCatamaran.Sail)
{
Point[] sail =
{
new Point(_startPosX.Value + 50, _startPosY.Value + 30),
new Point(_startPosX.Value + 50, _startPosY.Value + 5),
new Point(_startPosX.Value + 80, _startPosY.Value + 25),
new Point(_startPosX.Value + 50, _startPosY.Value + 25),
};
g.FillPolygon(additionalBrush, sail);
g.DrawPolygon(pen, sail);
}
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.Entities
{
public class EntityCatamaran : EntityBoat
{
public Color AdditionalColor { get; private set; }
public bool LeftBobber { get; private set; }
public bool RightBobber { get; private set; }
public bool Sail { get; private set; }
public double Step => Speed * 100 / Weight;
public EntityCatamaran(int speed, double weight, Color bodyColor, Color additionalColor, bool leftBobber, bool rightBobber, bool sail) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
LeftBobber = leftBobber;
RightBobber = rightBobber;
Sail = sail;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.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;
}
}
}

View File

@ -34,6 +34,9 @@
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonUp = new Button(); buttonUp = new Button();
CreateBoat_button = new Button();
StrategyBox = new ComboBox();
StepButton = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -51,11 +54,11 @@
CreateButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; CreateButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
CreateButton.Location = new Point(12, 456); CreateButton.Location = new Point(12, 456);
CreateButton.Name = "CreateButton"; CreateButton.Name = "CreateButton";
CreateButton.Size = new Size(94, 29); CreateButton.Size = new Size(168, 29);
CreateButton.TabIndex = 1; CreateButton.TabIndex = 1;
CreateButton.Text = "Создать"; CreateButton.Text = "Создать катамаран";
CreateButton.UseVisualStyleBackColor = true; CreateButton.UseVisualStyleBackColor = true;
CreateButton.Click += button1_Click; CreateButton.Click += CreateButton_Click;
// //
// buttonLeft // buttonLeft
// //
@ -105,11 +108,47 @@
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click; buttonUp.Click += ButtonMove_Click;
// //
// CreateBoat_button
//
CreateBoat_button.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
CreateBoat_button.Location = new Point(186, 456);
CreateBoat_button.Name = "CreateBoat_button";
CreateBoat_button.Size = new Size(168, 29);
CreateBoat_button.TabIndex = 6;
CreateBoat_button.Text = "Создать лодку";
CreateBoat_button.UseVisualStyleBackColor = true;
CreateBoat_button.Click += CreateBoat_button_Click;
//
// StrategyBox
//
StrategyBox.Anchor = AnchorStyles.Top | AnchorStyles.Right;
StrategyBox.DropDownStyle = ComboBoxStyle.DropDownList;
StrategyBox.FormattingEnabled = true;
StrategyBox.Items.AddRange(new object[] { "В центр", "К краю" });
StrategyBox.Location = new Point(505, 12);
StrategyBox.Name = "StrategyBox";
StrategyBox.Size = new Size(151, 28);
StrategyBox.TabIndex = 7;
//
// StepButton
//
StepButton.Anchor = AnchorStyles.Top | AnchorStyles.Right;
StepButton.Location = new Point(562, 46);
StepButton.Name = "StepButton";
StepButton.Size = new Size(94, 29);
StepButton.TabIndex = 8;
StepButton.Text = "Шаг";
StepButton.UseVisualStyleBackColor = true;
StepButton.Click += StepButton_Click;
//
// FormCatamaran // FormCatamaran
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(668, 497); ClientSize = new Size(668, 497);
Controls.Add(StepButton);
Controls.Add(StrategyBox);
Controls.Add(CreateBoat_button);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -131,5 +170,8 @@
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button CreateBoat_button;
private ComboBox StrategyBox;
private Button StepButton;
} }
} }

View File

@ -7,65 +7,84 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Catamaran.Drawings;
using Catamaran.Entities;
using Catamaran.MovementStrategy;
namespace Catamaran namespace Catamaran
{ {
public partial class FormCatamaran : Form public partial class FormCatamaran : Form
{ {
private DrawingCatamaran? _drawingCatamaran; private DrawingBoat? _drawingBoat;
private EntityCatamaran? _entityCatamaran; private AbstractStrategy? _strategy;
public FormCatamaran() public FormCatamaran()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
private void Draw() private void Draw()
{ {
if (_drawingCatamaran == null) return; if (_drawingBoat == null) return;
Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingCatamaran.DrawTransport(gr); _drawingBoat.DrawTransport(gr);
pictureBox1.Image = bmp; pictureBox1.Image = bmp;
} }
private void button1_Click(object sender, EventArgs e) private void CreateObject(string type)
{ {
Random random = new(); Random random = new Random();
_drawingCatamaran = new DrawingCatamaran();
_entityCatamaran = new EntityCatamaran(); switch (type)
_entityCatamaran.Init(random.Next(100, 500), random.Next(1000, 3000), {
case nameof(DrawingBoat):
_drawingBoat = new DrawingBoat(random.Next(100, 500), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
case nameof(DrawingCatamaran):
_drawingBoat = new DrawingCatamaran(random.Next(100, 500), 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)),
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)), Convert.ToBoolean(random.Next(0, 2))); Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
_drawingCatamaran.Init(_entityCatamaran); default:
_drawingCatamaran.SetPictureSize(pictureBox1.Width, pictureBox1.Height); return;
}
_drawingCatamaran.SetPosition(random.Next(10, 600), random.Next(10, 600));
_drawingBoat.SetPictureSize(pictureBox1.Width, pictureBox1.Height);
_drawingBoat.SetPosition(random.Next(10, 600), random.Next(10, 600));
Draw(); Draw();
}
private void CreateButton_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawingCatamaran));
}
private void CreateBoat_button_Click(object sender, EventArgs e)
{
CreateObject(nameof(DrawingBoat));
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawingCatamaran == null) return; if (_drawingBoat == null) return;
string name = ((Button)sender)?.Name ?? string.Empty; string name = ((Button)sender)?.Name ?? string.Empty;
bool result = false; bool result = false;
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
result = _drawingCatamaran.MoveTransport(DirectionType.Up); break; result = _drawingBoat.MoveTransport(DirectionType.Up); break;
case "buttonDown": case "buttonDown":
result = _drawingCatamaran.MoveTransport(DirectionType.Down); break; result = _drawingBoat.MoveTransport(DirectionType.Down); break;
case "buttonLeft": case "buttonLeft":
result = _drawingCatamaran.MoveTransport(DirectionType.Left); break; result = _drawingBoat.MoveTransport(DirectionType.Left); break;
case "buttonRight": case "buttonRight":
result = _drawingCatamaran.MoveTransport(DirectionType.Right); break; result = _drawingBoat.MoveTransport(DirectionType.Right); break;
} }
if (result) if (result)
{ {
@ -75,19 +94,58 @@ namespace Catamaran
private void FormCatamaran_SizeChanged(object sender, EventArgs e) private void FormCatamaran_SizeChanged(object sender, EventArgs e)
{ {
if (_drawingCatamaran == null) return; if (_drawingBoat == null) return;
_drawingCatamaran.SetPictureSize(pictureBox1.Width, pictureBox1.Height); _drawingBoat.SetPictureSize(pictureBox1.Width, pictureBox1.Height);
if (_drawingCatamaran.SetPictureSize(pictureBox1.Width, pictureBox1.Height)) if (_drawingBoat.SetPictureSize(pictureBox1.Width, pictureBox1.Height))
{ {
Draw(); Draw();
} }
}
private void StepButton_Click(object sender, EventArgs e)
{
if (_drawingBoat == null)
{
return;
}
if (StrategyBox.Enabled)
{
_strategy = StrategyBox.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableBoat(_drawingBoat), pictureBox1.Width, pictureBox1.Height);
}
if (_strategy == null)
{
return;
}
StrategyBox.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
StrategyBox.Enabled = true;
_strategy = null;
}
} }
} }
} }

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.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 (IsTargetDestination())
{
_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 abstract void MoveToTarget();
protected abstract bool IsTargetDestination();
protected int? GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
private bool MoveTo(MovementDirection direction)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
return _moveableObject?.TryMoveObject(direction) ?? false;
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool TryMoveObject(MovementDirection direction);
}
}

View File

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

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.MovementStrategy
{
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
protected override void MoveToTarget()
{
ObjectParameters? objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,55 @@
using Catamaran.Drawings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.MovementStrategy
{
internal class MoveableBoat : IMoveableObject
{
private readonly DrawingBoat? _boat = null;
public MoveableBoat(DrawingBoat 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.Unknown
};
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.MovementStrategy
{
public enum MovementDirection
{
//Unknown = -1,
Up = 1,
Down = 2,
Left = 3,
Right = 4
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.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;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran.MovementStrategy
{
public enum StrategyStatus
{
NotInit,
InProgress,
Finish
}
}