закончено

This commit is contained in:
tyxz0 2024-03-04 14:08:32 +04:00
parent d5621e0d6d
commit f10745536b
8 changed files with 226 additions and 11 deletions

View File

@ -1,7 +1,9 @@
namespace DoubleDeckerBus.Drawnings; namespace DoubleDeckerBus.Drawnings;
public enum DirectionType public enum DirectionType
{ /// <summary> {
Unknown = -1,
/// <summary>
/// вверх /// вверх
/// </summary> /// </summary>
Up = 1, Up = 1,

View File

@ -27,9 +27,9 @@ public class DrawingBus
public int? GetPosY => _startPosY; public int? GetPosY => _startPosY;
public int? GetWidth => _drawingBusWidth; public int GetWidth => _drawingBusWidth;
public int? GetHeight => _drawingBusHeight; public int GetHeight => _drawingBusHeight;
private DrawingBus() private DrawingBus()
{ {

View File

@ -6,7 +6,7 @@ namespace DoubleDeckerBus.Drawnings;
/// </summary> /// </summary>
public class DrawingDoubleDeckerBus : DrawingBus public class DrawingDoubleDeckerBus : DrawingBus
{ {
public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,50) public DrawingDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondFloor, bool stripes) : base (115,55)
{ {
EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes); EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondFloor, stripes);
} }

View File

@ -35,6 +35,8 @@
buttonDown = new Button(); buttonDown = new Button();
buttonRight = new Button(); buttonRight = new Button();
CreateBus = new Button(); CreateBus = new Button();
comboBoxStrategy = new ComboBox();
buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -117,11 +119,35 @@
CreateBus.UseVisualStyleBackColor = true; CreateBus.UseVisualStyleBackColor = true;
CreateBus.Click += buttonCreateBus_Click; CreateBus.Click += buttonCreateBus_Click;
// //
// comboBoxStrategy
//
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "To center", "To border" });
comboBoxStrategy.Location = new Point(667, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 8;
//
// buttonStrategyStep
//
buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
buttonStrategyStep.Location = new Point(713, 41);
buttonStrategyStep.Name = "buttonStrategyStep";
buttonStrategyStep.Size = new Size(75, 23);
buttonStrategyStep.TabIndex = 9;
buttonStrategyStep.Text = "Do step";
buttonStrategyStep.UseVisualStyleBackColor = true;
buttonStrategyStep.Click += buttonStrategyStep_Click;
//
// FormDoubleDeckerBus // FormDoubleDeckerBus
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(buttonStrategyStep);
Controls.Add(comboBoxStrategy);
Controls.Add(CreateBus); Controls.Add(CreateBus);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -144,5 +170,7 @@
private Button buttonDown; private Button buttonDown;
private Button buttonRight; private Button buttonRight;
private Button CreateBus; private Button CreateBus;
private ComboBox comboBoxStrategy;
private Button buttonStrategyStep;
} }
} }

View File

@ -8,15 +8,20 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using DoubleDeckerBus.Drawnings; using DoubleDeckerBus.Drawnings;
using DoubleDeckerBus.MovementStrategy;
namespace DoubleDeckerBus namespace DoubleDeckerBus
{ {
public partial class FormDoubleDeckerBus : Form public partial class FormDoubleDeckerBus : Form
{ {
private DrawingBus? _drawingBus; private DrawingBus? _drawingBus;
private AbstractStrategy? _strategy;
public FormDoubleDeckerBus() public FormDoubleDeckerBus()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
private void Draw() private void Draw()
@ -53,6 +58,8 @@ namespace DoubleDeckerBus
_drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height); _drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height);
_drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw(); Draw();
} }
@ -99,5 +106,48 @@ namespace DoubleDeckerBus
} }
} }
private void buttonStrategyStep_Click(object sender, EventArgs e)
{
if (_drawingBus == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MovetoBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableBus(_drawingBus), pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height);
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
}
else
{
if (_strategy == null)
{
return;
}
_strategy.MakeStep();
Draw();
}
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
} }
} }

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 MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParametrs? objParams = GetObjectParaments;
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()
{
ObjectParametrs? objParams = GetObjectParaments;
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

@ -1,4 +1,5 @@
using System; using DoubleDeckerBus.Drawnings;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,15 +7,50 @@ using System.Threading.Tasks;
namespace DoubleDeckerBus.MovementStrategy; namespace DoubleDeckerBus.MovementStrategy;
private DrawningBus? _drawningBus;
public class MoveableBus : IMoveableObject public class MoveableBus : IMoveableObject
{ {
public ObjectParametrs? GetObjectPosition => throw new NotImplementedException(); private DrawingBus? _bus = null;
public int GetStep => throw new NotImplementedException();
public MoveableBus(DrawingBus bus)
{
_bus = bus;
}
public ObjectParametrs? GetObjectPosition
{
get
{
if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue)
{
return null;
}
return new ObjectParametrs(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHeight);
}
}
public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0);
public bool TryMoveObject(MovementDirection direction) public bool TryMoveObject(MovementDirection direction)
{ {
throw new NotImplementedException(); if (_bus == null || _bus.EntityBus == null)
{
return false;
} }
return _bus.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,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus.MovementStrategy;
internal class MovetoBorder : AbstractStrategy
{
protected override bool IsTargetDestination()
{
ObjectParametrs? objParams = GetObjectParaments;
if (objParams == null)
{
return false;
}
return objParams.DownBorder + GetStep() > FieldHeight - 5 && objParams.RightBorder + GetStep() > FieldWidth - 5;
}
protected override void MoveToTarget()
{
ObjectParametrs? objParams = GetObjectParaments;
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();
}
}
}