Laba2 KozyrevSS GasolineTanker Base #3
73
Lab/AbstractStrategy.cs
Normal file
73
Lab/AbstractStrategy.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.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;
|
||||
FieldHeight = height;
|
||||
FieldWidth = width;
|
||||
}
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return;
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected bool MoveLeft() => MoveTo(Direction.Left);
|
||||
protected bool MoveRight() => MoveTo(Direction.Right);
|
||||
protected bool MoveUp() => MoveTo(Direction.Up);
|
||||
protected bool MoveDown() => MoveTo(Direction.Down);
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectParameters;
|
||||
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract bool IsTargetDestination();
|
||||
private bool MoveTo(Direction direction)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
return false;
|
||||
if (_moveableObject?.CheckCanMove(direction) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
25
Lab/AddBaseCar.cs
Normal file
25
Lab/AddBaseCar.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.Entities
|
||||
{
|
||||
public class AddBaseCar : BaseCar
|
||||
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
public bool BodyKit { get; private set; }
|
||||
public bool Wing { get; private set; }
|
||||
public bool SportLine { get; private set; }
|
||||
public AddBaseCar(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine) : base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
BodyKit = bodyKit;
|
||||
Wing = wing;
|
||||
SportLine = sportLine;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -4,27 +4,19 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab
|
||||
namespace Lab.Entities
|
||||
{
|
||||
public class BaseCar
|
||||
{
|
||||
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 BodyKit { get; private set; }
|
||||
public bool Wing { get; private set; }
|
||||
public bool SportLine { get; private set; }
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine)
|
||||
public BaseCar(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
BodyKit = bodyKit;
|
||||
Wing = wing;
|
||||
SportLine = sportLine;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,4 +13,4 @@ namespace Lab
|
||||
Left = 3,
|
||||
Right = 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,110 +3,44 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lab.Entities;
|
||||
|
||||
namespace Lab
|
||||
namespace Lab.DrawningObjects
|
||||
{
|
||||
public class DrawGasolineTanker
|
||||
public class DrawGasolineTanker : DrawTanker
|
||||
{
|
||||
public BaseCar? GasolineTanker { get; private set; }
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private readonly int _carWidth = 100;
|
||||
private readonly int _carHeight = 80;
|
||||
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height)
|
||||
public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) : base(speed, weight, bodyColor, width, height)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
GasolineTanker = new BaseCar();
|
||||
GasolineTanker.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
return;
|
||||
switch (direction)
|
||||
if (GasolineTanker != null)
|
||||
{
|
||||
case Direction.Left:
|
||||
{
|
||||
if (_startPosX - GasolineTanker.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Up:
|
||||
{
|
||||
if (_startPosY - GasolineTanker.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Right:
|
||||
{
|
||||
if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Down:
|
||||
{
|
||||
if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
GasolineTanker = new AddBaseCar(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
if (GasolineTanker is not AddBaseCar Gasoline)
|
||||
return;
|
||||
Pen pen = new(GasolineTanker.BodyColor, 2);
|
||||
Brush brush = new SolidBrush(GasolineTanker.BodyColor);
|
||||
if (GasolineTanker.BodyKit)
|
||||
base.DrawTransport(g);
|
||||
if (Gasoline.BodyKit)
|
||||
{
|
||||
Brush bodyBrush = new SolidBrush(GasolineTanker.AdditionalColor);
|
||||
Brush bodyBrush = new SolidBrush(Gasoline.AdditionalColor);
|
||||
g.FillEllipse(bodyBrush, 10 + _startPosX, 10 + _startPosY, 70, 30);
|
||||
}
|
||||
// Отрисовка корпуса
|
||||
g.FillRectangle(brush, 10 + _startPosX, 40 + _startPosY, 90, 20);
|
||||
g.FillRectangle(brush, 80 + _startPosX, 10 + _startPosY, 20, 40);
|
||||
// Отрисовка колесиков
|
||||
g.FillEllipse(brush, 10 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g.FillEllipse(brush, 30 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g.FillEllipse(brush, 80 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
if (GasolineTanker.SportLine)
|
||||
|
||||
if (Gasoline.SportLine)
|
||||
{
|
||||
Brush lineBrush = new SolidBrush(GasolineTanker.AdditionalColor);
|
||||
Brush lineBrush = new SolidBrush(Gasoline.AdditionalColor);
|
||||
g.FillRectangle(lineBrush, 15 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
g.FillRectangle(lineBrush, 40 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
g.FillRectangle(lineBrush, 65 + _startPosX, 45 + _startPosY, 20, 5);
|
||||
}
|
||||
|
||||
if (GasolineTanker.Wing)
|
||||
if (Gasoline.Wing)
|
||||
{
|
||||
Brush lightBrush = new SolidBrush(GasolineTanker.AdditionalColor);
|
||||
Brush lightBrush = new SolidBrush(Gasoline.AdditionalColor);
|
||||
g.FillRectangle(lightBrush, 87 + _startPosX, 5 + _startPosY, 5, 5);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
123
Lab/DrawTanker.cs
Normal file
123
Lab/DrawTanker.cs
Normal file
@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.Pkcs;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lab.Entities;
|
||||
|
||||
namespace Lab.DrawningObjects
|
||||
{
|
||||
public class DrawTanker
|
||||
{
|
||||
public BaseCar? GasolineTanker { get; protected set; }
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected readonly int _carWidth = 100;
|
||||
protected readonly int _carHeight = 80;
|
||||
public int GetPosX => _startPosX;
|
||||
public int GetPosY => _startPosY;
|
||||
public int GetWidth => _carWidth;
|
||||
public int GetHeight => _carHeight;
|
||||
|
||||
public bool CanMove(Direction direction)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
return false;
|
||||
return direction switch
|
||||
{
|
||||
Direction.Left => _startPosX - GasolineTanker.Step > 0,
|
||||
Direction.Up => _startPosY - GasolineTanker.Step > 0,
|
||||
Direction.Right => _startPosX + _carWidth + GasolineTanker.Step < _pictureWidth,
|
||||
Direction.Down => _startPosY + _carHeight + GasolineTanker.Step < _pictureHeight,
|
||||
_ => false
|
||||
|
||||
};
|
||||
}
|
||||
// Конструктор класса
|
||||
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
GasolineTanker = new BaseCar(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public DrawTanker(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight)
|
||||
{
|
||||
_pictureHeight = height;
|
||||
_pictureWidth = width;
|
||||
_carHeight = carHeight;
|
||||
_carWidth = carWidth;
|
||||
GasolineTanker = new BaseCar(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || GasolineTanker == null)
|
||||
return;
|
||||
switch (direction)
|
||||
{
|
||||
case Direction.Left:
|
||||
{
|
||||
if (_startPosX - GasolineTanker.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Up:
|
||||
{
|
||||
if (_startPosY - GasolineTanker.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Right:
|
||||
{
|
||||
if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Direction.Down:
|
||||
{
|
||||
if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)GasolineTanker.Step;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (GasolineTanker == null)
|
||||
return;
|
||||
Pen pen = new(GasolineTanker.BodyColor, 2);
|
||||
Brush brush = new SolidBrush(GasolineTanker.BodyColor);
|
||||
|
||||
// Отрисовка корпуса
|
||||
g.FillRectangle(brush, 10 + _startPosX, 40 + _startPosY, 90, 20);
|
||||
g.FillRectangle(brush, 80 + _startPosX, 10 + _startPosY, 20, 40);
|
||||
// Отрисовка колесиков
|
||||
g.FillEllipse(brush, 10 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g.FillEllipse(brush, 30 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
g.FillEllipse(brush, 80 + _startPosX, 60 + _startPosY, 20, 20);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
32
Lab/DrawingObjectTanker.cs
Normal file
32
Lab/DrawingObjectTanker.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lab.DrawningObjects;
|
||||
|
||||
namespace Lab.MovementStrategy
|
||||
{
|
||||
public class DrawingObjectTanker : IMoveableObject
|
||||
{
|
||||
private readonly DrawTanker? _drawTanker = null;
|
||||
public DrawingObjectTanker(DrawTanker drawTanker )
|
||||
{
|
||||
_drawTanker = drawTanker;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectParameters
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawTanker == null || _drawTanker.GasolineTanker == null)
|
||||
return null;
|
||||
return new ObjectParameters(_drawTanker.GetPosX, _drawTanker.GetPosY, _drawTanker.GetWidth, _drawTanker.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawTanker?.GasolineTanker?.Step ?? 0);
|
||||
public bool CheckCanMove(Direction direction) => _drawTanker?.CanMove(direction) ?? false;
|
||||
public void MoveObject(Direction direction) => _drawTanker?.MoveTransport(direction);
|
||||
|
||||
}
|
||||
}
|
68
Lab/Frame.Designer.cs
generated
68
Lab/Frame.Designer.cs
generated
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
DrawCar = new PictureBox();
|
||||
CreateCarButton = new Button();
|
||||
CreateBaseCarButton = new Button();
|
||||
Up = new Button();
|
||||
Left = new Button();
|
||||
Down = new Button();
|
||||
Right = new Button();
|
||||
buttonCreateGasolineTanker = new Button();
|
||||
buttonStep = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
((System.ComponentModel.ISupportInitialize)DrawCar).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -46,16 +49,16 @@
|
||||
DrawCar.TabIndex = 1;
|
||||
DrawCar.TabStop = false;
|
||||
//
|
||||
// CreateCarButton
|
||||
// CreateBaseCarButton
|
||||
//
|
||||
CreateCarButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
CreateCarButton.Location = new Point(12, 512);
|
||||
CreateCarButton.Name = "CreateCarButton";
|
||||
CreateCarButton.Size = new Size(94, 29);
|
||||
CreateCarButton.TabIndex = 2;
|
||||
CreateCarButton.Text = "Создать";
|
||||
CreateCarButton.UseVisualStyleBackColor = true;
|
||||
CreateCarButton.Click += CreateCarButton_Click;
|
||||
CreateBaseCarButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
CreateBaseCarButton.Location = new Point(12, 441);
|
||||
CreateBaseCarButton.Name = "CreateBaseCarButton";
|
||||
CreateBaseCarButton.Size = new Size(200, 100);
|
||||
CreateBaseCarButton.TabIndex = 2;
|
||||
CreateBaseCarButton.Text = "Создать простой бензовоз";
|
||||
CreateBaseCarButton.UseVisualStyleBackColor = true;
|
||||
CreateBaseCarButton.Click += CreateCarButton_Click;
|
||||
//
|
||||
// Up
|
||||
//
|
||||
@ -105,18 +108,52 @@
|
||||
Right.UseVisualStyleBackColor = true;
|
||||
Right.Click += ButtonMove_Click;
|
||||
//
|
||||
// Frame
|
||||
// buttonCreateGasolineTanker
|
||||
//
|
||||
buttonCreateGasolineTanker.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateGasolineTanker.Location = new Point(218, 442);
|
||||
buttonCreateGasolineTanker.Name = "buttonCreateGasolineTanker";
|
||||
buttonCreateGasolineTanker.Size = new Size(200, 100);
|
||||
buttonCreateGasolineTanker.TabIndex = 7;
|
||||
buttonCreateGasolineTanker.Text = "Создать красивый бензовоз";
|
||||
buttonCreateGasolineTanker.UseVisualStyleBackColor = true;
|
||||
buttonCreateGasolineTanker.Click += CreateGasolineTankerButton_Click;
|
||||
//
|
||||
// buttonStep
|
||||
//
|
||||
buttonStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonStep.Location = new Point(776, 72);
|
||||
buttonStep.Name = "buttonStep";
|
||||
buttonStep.Size = new Size(94, 29);
|
||||
buttonStep.TabIndex = 8;
|
||||
buttonStep.Text = "Шаг";
|
||||
buttonStep.UseVisualStyleBackColor = true;
|
||||
buttonStep.Click += ButtonStep_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "0", "1" });
|
||||
comboBoxStrategy.Location = new Point(719, 27);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(151, 28);
|
||||
comboBoxStrategy.TabIndex = 9;
|
||||
//
|
||||
// GasolineTanker
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(882, 553);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonStep);
|
||||
Controls.Add(buttonCreateGasolineTanker);
|
||||
Controls.Add(Right);
|
||||
Controls.Add(Down);
|
||||
Controls.Add(Left);
|
||||
Controls.Add(Up);
|
||||
Controls.Add(CreateCarButton);
|
||||
Controls.Add(CreateBaseCarButton);
|
||||
Controls.Add(DrawCar);
|
||||
Name = "Frame";
|
||||
Name = "GasolineTanker";
|
||||
StartPosition = FormStartPosition.CenterScreen;
|
||||
Text = "GasolineTanker";
|
||||
((System.ComponentModel.ISupportInitialize)DrawCar).EndInit();
|
||||
@ -125,10 +162,13 @@
|
||||
|
||||
#endregion
|
||||
private PictureBox DrawCar;
|
||||
private Button CreateCarButton;
|
||||
private Button CreateBaseCarButton;
|
||||
private Button Up;
|
||||
private Button Left;
|
||||
private Button Down;
|
||||
private Button Right;
|
||||
private Button buttonCreateGasolineTanker;
|
||||
private Button buttonStep;
|
||||
private ComboBox comboBoxStrategy;
|
||||
}
|
||||
}
|
66
Lab/Frame.cs
66
Lab/Frame.cs
@ -1,8 +1,13 @@
|
||||
using Lab.DrawningObjects;
|
||||
using Lab.MovementStrategy;
|
||||
using Lab;
|
||||
|
||||
namespace Lab
|
||||
{
|
||||
public partial class Frame : Form
|
||||
{
|
||||
private DrawGasolineTanker? _drawingSportCar;
|
||||
private DrawTanker? _drawingTanker;
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
public Frame()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -10,43 +15,80 @@ namespace Lab
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawingSportCar == null)
|
||||
if (_drawingTanker == null)
|
||||
return;
|
||||
Bitmap bitmap = new(DrawCar.Width, DrawCar.Height);
|
||||
Graphics g = Graphics.FromImage(bitmap);
|
||||
_drawingSportCar.DrawTransport(g);
|
||||
_drawingTanker.DrawTransport(g);
|
||||
DrawCar.Image = bitmap;
|
||||
}
|
||||
private void CreateCarButton_Click(object sender, EventArgs e)
|
||||
private void CreateGasolineTankerButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new();
|
||||
_drawingSportCar = new();
|
||||
_drawingSportCar.Init(rnd.Next(100, 200), rnd.Next(2000, 4000),
|
||||
_drawingTanker = new DrawGasolineTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)),
|
||||
DrawCar.Width, DrawCar.Height);
|
||||
_drawingSportCar.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
|
||||
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void CreateCarButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new();
|
||||
_drawingTanker = new DrawTanker(rnd.Next(100, 200), rnd.Next(2000, 4000),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
DrawCar.Width, DrawCar.Height);
|
||||
_drawingTanker.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingSportCar == null)
|
||||
if (_drawingTanker == null)
|
||||
return;
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
switch (name)
|
||||
{
|
||||
case "Up":
|
||||
_drawingSportCar.MoveTransport(Direction.Up); break;
|
||||
_drawingTanker.MoveTransport(Direction.Up); break;
|
||||
case "Down":
|
||||
_drawingSportCar.MoveTransport(Direction.Down); break;
|
||||
_drawingTanker.MoveTransport(Direction.Down); break;
|
||||
case "Left":
|
||||
_drawingSportCar.MoveTransport(Direction.Left); break;
|
||||
_drawingTanker.MoveTransport(Direction.Left); break;
|
||||
case "Right":
|
||||
_drawingSportCar.MoveTransport(Direction.Right); break;
|
||||
_drawingTanker.MoveTransport(Direction.Right); break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawingTanker == null)
|
||||
return;
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
return;
|
||||
_abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), DrawCar.Width, DrawCar.Height);
|
||||
comboBoxStrategy.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
return;
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
Lab/IMoveableObject.cs
Normal file
17
Lab/IMoveableObject.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectParameters { get; }
|
||||
int GetStep { get; }
|
||||
bool CheckCanMove(Direction direction);
|
||||
void MoveObject(Direction direction);
|
||||
|
||||
}
|
||||
}
|
26
Lab/Laba1WithoutGit.csproj
Normal file
26
Lab/Laba1WithoutGit.csproj
Normal file
@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Update="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
48
Lab/MoveToBorder.cs
Normal file
48
Lab/MoveToBorder.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth && objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder - FieldWidth;
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if (diffX >= 0)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
else if (diffY >= 0)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else if (Math.Abs(diffX) > Math.Abs(diffY))
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
54
Lab/MoveToCenter.cs
Normal file
54
Lab/MoveToCenter.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
Lab/ObjectParameters.cs
Normal file
31
Lab/ObjectParameters.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab.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 => _width + _x; // Правая граница
|
||||
public int DownBorder => _height + _y; // Нижняя граница
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
15
Lab/Status.cs
Normal file
15
Lab/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lab
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit = 0,
|
||||
InProgress = 1,
|
||||
Finish = 2
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user
Имя класса не соответствует указанному в задании