PI-12_Zaytsev_A.V_LabWork02_Base #5
@ -1,7 +1,9 @@
|
||||
namespace DoubleDeckerBus.Drawnings;
|
||||
|
||||
public enum DirectionType
|
||||
{ /// <summary>
|
||||
{
|
||||
Unknown = -1,
|
||||
/// <summary>
|
||||
/// вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
@ -27,9 +27,9 @@ public class DrawingBus
|
||||
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
public int? GetWidth => _drawingBusWidth;
|
||||
public int GetWidth => _drawingBusWidth;
|
||||
|
||||
public int? GetHeight => _drawingBusHeight;
|
||||
public int GetHeight => _drawingBusHeight;
|
||||
|
||||
private DrawingBus()
|
||||
{
|
||||
|
@ -6,7 +6,7 @@ namespace DoubleDeckerBus.Drawnings;
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
@ -35,6 +35,8 @@
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
CreateBus = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerBus).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -117,11 +119,35 @@
|
||||
CreateBus.UseVisualStyleBackColor = true;
|
||||
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
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(CreateBus);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
@ -144,5 +170,7 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button CreateBus;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -8,15 +8,20 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using DoubleDeckerBus.Drawnings;
|
||||
using DoubleDeckerBus.MovementStrategy;
|
||||
|
||||
namespace DoubleDeckerBus
|
||||
{
|
||||
public partial class FormDoubleDeckerBus : Form
|
||||
{
|
||||
private DrawingBus? _drawingBus;
|
||||
|
||||
private AbstractStrategy? _strategy;
|
||||
public FormDoubleDeckerBus()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
@ -39,7 +44,7 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
case nameof(DrawingBus):
|
||||
_drawingBus = new DrawingBus(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)));
|
||||
break;
|
||||
case nameof(DrawingDoubleDeckerBus):
|
||||
_drawingBus = new DrawingDoubleDeckerBus(random.Next(100, 300), random.Next(1000, 3000),
|
||||
@ -53,6 +58,8 @@ namespace DoubleDeckerBus
|
||||
|
||||
_drawingBus.SetPictureSize(pictureBoxDoubleDeckerBus.Width, pictureBoxDoubleDeckerBus.Height);
|
||||
_drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.Enabled = true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using DoubleDeckerBus.Drawnings;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -6,15 +7,50 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DoubleDeckerBus.MovementStrategy;
|
||||
|
||||
private DrawningBus? _drawningBus;
|
||||
|
||||
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)
|
||||
{
|
||||
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,
|
||||
} ;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user