Compare commits

...

4 Commits

Author SHA1 Message Date
d3ddb430be Merge pull request 'Mavrina J.E. Lab work 2' (#2) from Лаб_2 into Лаб_1
Reviewed-on: http://student.git.athene.tech/Mavrina_Julia/ISEbd-22_Mavrina_J.E._DoubleDeckerBus_Base/pulls/2
2023-10-16 12:30:22 +04:00
yuliya.mavrina@internet.ru
d07056b1bd ntcn 2023-10-16 12:30:36 +03:00
yuliya.mavrina@internet.ru
41f1398da6 Лабораторная №2. 2023-10-14 18:26:14 +03:00
yuliya.mavrina@internet.ru
5b3f2c0ab1 Лабораторная №2 2023-10-14 17:51:12 +03:00
15 changed files with 681 additions and 175 deletions

View File

@ -29,11 +29,14 @@
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxBus = new PictureBox(); pictureBoxBus = new PictureBox();
buttonCreate = new Button(); buttonCreateBus = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
buttonCreatDoubleDeckerDus = new Button();
buttonStep_Click = new Button();
comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -46,18 +49,18 @@
pictureBoxBus.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxBus.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxBus.TabIndex = 0; pictureBoxBus.TabIndex = 0;
pictureBoxBus.TabStop = false; pictureBoxBus.TabStop = false;
pictureBoxBus.Click += pictureBoxBus_Click;
// //
// buttonCreate // buttonCreateBus
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 426); buttonCreateBus.Location = new Point(12, 409);
buttonCreate.Name = "buttonCreate"; buttonCreateBus.Name = "buttonCreateBus";
buttonCreate.Size = new Size(75, 23); buttonCreateBus.Size = new Size(117, 40);
buttonCreate.TabIndex = 1; buttonCreateBus.TabIndex = 1;
buttonCreate.Text = "Создать"; buttonCreateBus.Text = "Создать автобус";
buttonCreate.UseVisualStyleBackColor = true; buttonCreateBus.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; buttonCreateBus.Click += buttonCreateBus_Click;
// //
// buttonUp // buttonUp
// //
@ -69,7 +72,8 @@
buttonUp.Size = new Size(30, 30); buttonUp.Size = new Size(30, 30);
buttonUp.TabIndex = 2; buttonUp.TabIndex = 2;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click; buttonUp.AutoSizeChanged += ButtonMove_Click;
buttonUp.Click += ButtonMove_Click;
// //
// buttonLeft // buttonLeft
// //
@ -81,7 +85,8 @@
buttonLeft.Size = new Size(30, 30); buttonLeft.Size = new Size(30, 30);
buttonLeft.TabIndex = 3; buttonLeft.TabIndex = 3;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click; buttonLeft.AutoSizeChanged += ButtonMove_Click;
buttonLeft.Click += ButtonMove_Click;
// //
// buttonDown // buttonDown
// //
@ -93,7 +98,8 @@
buttonDown.Size = new Size(30, 30); buttonDown.Size = new Size(30, 30);
buttonDown.TabIndex = 4; buttonDown.TabIndex = 4;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click; buttonDown.AutoSizeChanged += ButtonMove_Click;
buttonDown.Click += ButtonMove_Click;
// //
// buttonRight // buttonRight
// //
@ -105,18 +111,52 @@
buttonRight.Size = new Size(30, 30); buttonRight.Size = new Size(30, 30);
buttonRight.TabIndex = 5; buttonRight.TabIndex = 5;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click; buttonRight.AutoSizeChanged += ButtonMove_Click;
buttonRight.Click += ButtonMove_Click;
//
// buttonCreatDoubleDeckerDus
//
buttonCreatDoubleDeckerDus.Location = new Point(135, 409);
buttonCreatDoubleDeckerDus.Name = "buttonCreatDoubleDeckerDus";
buttonCreatDoubleDeckerDus.Size = new Size(136, 40);
buttonCreatDoubleDeckerDus.TabIndex = 6;
buttonCreatDoubleDeckerDus.Text = "Создать двухэтажный автобус";
buttonCreatDoubleDeckerDus.UseVisualStyleBackColor = true;
buttonCreatDoubleDeckerDus.Click += buttonCreatDoubleDeckerDus_Click;
//
// buttonStep_Click
//
buttonStep_Click.Location = new Point(797, 41);
buttonStep_Click.Name = "buttonStep_Click";
buttonStep_Click.Size = new Size(75, 23);
buttonStep_Click.TabIndex = 7;
buttonStep_Click.Text = "Шаг";
buttonStep_Click.UseVisualStyleBackColor = true;
buttonStep_Click.Click += buttonStep_Click_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxStrategy.Location = new Point(751, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 8;
// //
// DoubleDeckerBus // DoubleDeckerBus
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461); ClientSize = new Size(884, 461);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep_Click);
Controls.Add(buttonCreatDoubleDeckerDus);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonCreate); Controls.Add(buttonCreateBus);
Controls.Add(pictureBoxBus); Controls.Add(pictureBoxBus);
Name = "DoubleDeckerBus"; Name = "DoubleDeckerBus";
StartPosition = FormStartPosition.CenterParent; StartPosition = FormStartPosition.CenterParent;
@ -129,10 +169,13 @@
#endregion #endregion
private PictureBox pictureBoxBus; private PictureBox pictureBoxBus;
private Button buttonCreate; private Button buttonCreateBus;
private Button buttonUp; private Button buttonUp;
private Button buttonLeft; private Button buttonLeft;
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button buttonCreatDoubleDeckerDus;
private Button buttonStep_Click;
private ComboBox comboBoxStrategy;
} }
} }

View File

@ -1,8 +1,12 @@
using DoubleDeckerBus.DrawningObjects;
using DoubleDeckerBus.MovementStrategy;
namespace DoubleDeckerBus namespace DoubleDeckerBus
{ {
public partial class DoubleDeckerBus : Form public partial class DoubleDeckerBus : Form
{ {
private DrawningBus? _drawningBus; private DrawningBus? _drawningBus;
private AbstractStrategy? _abstractStrategy;
public DoubleDeckerBus() public DoubleDeckerBus()
{ {
InitializeComponent(); InitializeComponent();
@ -19,14 +23,36 @@ namespace DoubleDeckerBus
pictureBoxBus.Image = bmp; pictureBoxBus.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreateBus_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawningBus = new DrawningBus(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), _drawningBus = new DrawningBus(random.Next(100, 300),
random.Next(0, 256)), pictureBoxBus.Width, pictureBoxBus.Height); random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxBus.Width, pictureBoxBus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw(); Draw();
} }
private void buttonMove_Click(object sender, EventArgs e)
private void buttonCreatDoubleDeckerDus_Click(object sender, EventArgs e)
{
Random random = new();
_drawningBus = new DrawningDoubleDeckerBus(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)), Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBus.Width, pictureBoxBus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawningBus == null) if (_drawningBus == null)
{ {
@ -51,9 +77,42 @@ random.Next(0, 256)), pictureBoxBus.Width, pictureBoxBus.Height);
Draw(); Draw();
} }
private void pictureBoxBus_Click(object sender, EventArgs e) private void buttonStep_Click_Click(object sender, EventArgs e)
{ {
if (_drawningBus == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectBus(_drawningBus), pictureBoxBus.Width,
pictureBoxBus.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
} }
} }
} }

View File

@ -1,128 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class DrawningBus
{
public EntityBus? EntityBus { get; private set; }
private int? _pictureWidth = null;
private int? _pictureHeight = null;
private float _startPosX;
private float _startPosY;
private readonly int _busWidth = 100;
private readonly int _busHeight = 70;
public DrawningBus(int speed, float weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionBus direction)
{
if (EntityBus == null)
{
return;
}
switch (direction)
{
case DirectionBus.Left:
if (_startPosX - EntityBus.Step > 0)
{
_startPosX -= (int)EntityBus.Step;
}
break;
case DirectionBus.Up:
if (_startPosY - EntityBus.Step > 0)
{
_startPosY -= (int)EntityBus.Step;
}
break;
case DirectionBus.Right:
if (_startPosX + _busWidth + EntityBus.Step < _pictureWidth)
{
_startPosX += EntityBus.Step;
}
break;
case DirectionBus.Down:
if (_startPosY + _busHeight + EntityBus.Step < _pictureHeight)
{
_startPosY += EntityBus.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (EntityBus == null)
{
return;
}
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 60);
Brush brBodyColor = new SolidBrush(EntityBus.BodyColor);
g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 60);
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20);
g.DrawEllipse(pen, _startPosX + 7, _startPosY + 55, 20, 20);
g.DrawEllipse(pen, _startPosX + 77, _startPosY + 55, 20, 20);
g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 55, 20, 20);
g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 55, 20, 20);
Brush brBlue = new SolidBrush(Color.Blue);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 35, 10, 15);
g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 35, 10, 15);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 35, 10, 15);
g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 35, 10, 15);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _busWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _busWidth;
}
if (_startPosY + _busHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _busHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight);
}
}
}

View File

@ -0,0 +1,139 @@
using DoubleDeckerBus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.DrawningObjects
{
public class DrawningBus
{
public EntityBus? EntityBus { get; protected set; }
private int? _pictureWidth;
private int? _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _busWidth = 110;
protected readonly int _busHeight = 70;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _busWidth;
public int GetHeight => _busHeight;
public DrawningBus(int speed, float weight, Color bodyColor, int
width, int height)
{
if (width < _busWidth || height < _busHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
protected DrawningBus(int speed, float weight, Color bodyColor, int width, int height, int busWidth, int busHeight)
{
if (width <= _busWidth || height <= _busHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_busWidth = busWidth;
_busHeight = busHeight;
EntityBus = new EntityBus(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
}
public bool CanMove(DirectionBus direction)
{
if (EntityBus == null)
{
return false;
}
return direction switch
{
//влево
DirectionBus.Left => _startPosX - EntityBus.Step > 0,
//вверх
DirectionBus.Up => _startPosY - EntityBus.Step > 0,
//вправо
DirectionBus.Right => _startPosX + _busWidth + EntityBus.Step < _pictureWidth,
//вниз
DirectionBus.Down => _startPosY + _busHeight + EntityBus.Step < _pictureHeight,
_ => false,
};
}
public void MoveTransport(DirectionBus direction)
{
if (!CanMove(direction) || EntityBus == null)
{
return;
}
switch (direction)
{
//влево
case DirectionBus.Left:
_startPosX -= (int)EntityBus.Step;
break;
//вверх
case DirectionBus.Up:
_startPosY -= (int)EntityBus.Step;
break;
// вправо
case DirectionBus.Right:
_startPosX += (int)EntityBus.Step;
break;
//вниз
case DirectionBus.Down:
_startPosY += (int)EntityBus.Step;
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityBus == null)
{
return;
}
if (EntityBus == null)
{
return;
}
Pen pen = new(Color.Black);
g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30);
Brush brBodyColor = new SolidBrush(EntityBus.BodyColor);
g.FillRectangle(brBodyColor, _startPosX, _startPosY + 10, 100, 30);
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20);
g.DrawEllipse(pen, _startPosX + 7, _startPosY + 35, 10, 10);
g.DrawEllipse(pen, _startPosX + 77, _startPosY + 35, 10, 10);
g.FillEllipse(brBlack, _startPosX + 7, _startPosY + 35, 10, 10);
g.FillEllipse(brBlack, _startPosX + 77, _startPosY + 35, 10, 10);
Brush brBlue = new SolidBrush(Color.Blue);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15);
}
}
}

View File

@ -0,0 +1,78 @@
using DoubleDeckerBus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace DoubleDeckerBus.DrawningObjects
{
public class DrawningDoubleDeckerBus : DrawningBus
{
public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor,
Color additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor, int width, int height) :
base(speed, weight, bodyColor, width, height, 120, 85)
{
if (EntityBus != null)
{
EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor,
additionalColor, secondFloor, ladder, lineBetweenFloor);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityBus is not EntityDoubleDeckerBus doubleDeckerBus)
{
return;
}
Pen pen = new(Color.Black);
Brush brAdditionalColor = new SolidBrush(doubleDeckerBus.AdditionalColor);
Brush brBlue = new SolidBrush(Color.Blue);
Brush brBlack = new SolidBrush(Color.Black);
if (doubleDeckerBus.SecondFloor)
{
g.FillRectangle(brAdditionalColor, _startPosX, _startPosY + 10, 100, 30);
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 20, 10, 20);
g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 20, 10, 20);
g.FillEllipse(brBlue, _startPosX + 10, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 15, 10, 15);
g.FillEllipse(brBlue, _startPosX + 90, _startPosY + 15, 10, 15);
}
_startPosY += 30;
base.DrawTransport(g);
_startPosY -= 30;
if (doubleDeckerBus.Ladder)
{
if (doubleDeckerBus.SecondFloor == true)
{
//Вертикальные прямые
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 70)), new Point((int)(_startPosX), (int)(_startPosY + 10)));
g.DrawLine(pen, new Point((int)(_startPosX + 10), (int)(_startPosY + 70)), new Point((int)(_startPosX + 10), (int)(_startPosY + 10)));
//Горизонтальные прямые
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 20)), new Point((int)(_startPosX + 10), (int)(_startPosY + 20)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 30)), new Point((int)(_startPosX + 10), (int)(_startPosY + 30)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 40)), new Point((int)(_startPosX + 10), (int)(_startPosY + 40)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 50)), new Point((int)(_startPosX + 10), (int)(_startPosY + 50)));
g.DrawLine(pen, new Point((int)(_startPosX), (int)(_startPosY + 60)), new Point((int)(_startPosX + 10), (int)(_startPosY + 60)));
}
}
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.Entities
{
public class EntityBus
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public EntityBus(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.Entities
{
public class EntityDoubleDeckerBus : EntityBus
{
public Color AdditionalColor { get; private set; }
public bool SecondFloor { get; private set; }
public bool Ladder { get; private set; }
public bool LineBetweenFloor { get; private set; }
public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color
additionalColor, bool secondFloor, bool ladder, bool lineBetweenFloor) :
base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
SecondFloor = secondFloor;
Ladder = ladder;
LineBetweenFloor = lineBetweenFloor;
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class EntityBus
{
public int Speed { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public float Step => (float)Speed * 100 / Weight;
public EntityBus(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = (speed <= 0) ? rnd.Next(250, 300) : speed;
Weight = (weight <= 0) ? rnd.Next(50, 70) : weight;
BodyColor = bodyColor;
}
}
}

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
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;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(DirectionBus.Left);
protected bool MoveRight() => MoveTo(DirectionBus.Right);
protected bool MoveUp() => MoveTo(DirectionBus.Up);
protected bool MoveDown() => MoveTo(DirectionBus.Down);
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(DirectionBus directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoubleDeckerBus.DrawningObjects;
namespace DoubleDeckerBus.MovementStrategy
{
public class DrawningObjectBus : IMoveableObject
{
private readonly DrawningBus? _drawningBus = null;
public DrawningObjectBus(DrawningBus drawningBus)
{
_drawningBus = drawningBus;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningBus == null || _drawningBus.EntityBus == null)
{
return null;
}
return new ObjectParameters(_drawningBus.GetPosX,
_drawningBus.GetPosY, _drawningBus.GetWidth, _drawningBus.GetHeight);
}
}
public int GetStep => (int)(_drawningBus?.EntityBus?.Step ?? 0);
public bool CheckCanMove(DirectionBus direction) =>
_drawningBus?.CanMove(direction) ?? false;
public void MoveObject(DirectionBus direction) =>
_drawningBus?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionBus direction);
void MoveObject(DirectionBus direction);
}
}

View File

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

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.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,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.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 DoubleDeckerBus.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}