Lab02
This commit is contained in:
parent
468a9d2829
commit
9c41c8c23c
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Project_SelfPropelledArtilleryUnit
|
|
||||||
{
|
|
||||||
public enum DirectionType
|
|
||||||
{
|
|
||||||
Up=1,
|
|
||||||
Down=2,
|
|
||||||
Left=3,
|
|
||||||
Right=4
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.Drawings;
|
||||||
|
|
||||||
|
public enum DirectionType
|
||||||
|
{
|
||||||
|
Unknow = -1,
|
||||||
|
Up = 1,
|
||||||
|
Down = 2,
|
||||||
|
Left = 3,
|
||||||
|
Right = 4
|
||||||
|
}
|
@ -1,33 +1,52 @@
|
|||||||
using System;
|
using Project_SelfPropelledArtilleryUnit.Entity;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Project_SelfPropelledArtilleryUnit
|
namespace Project_SelfPropelledArtilleryUnit.Drawings
|
||||||
{
|
{
|
||||||
public class DrawingSAU
|
public class DrawingBase
|
||||||
{
|
{
|
||||||
public EntitySAU? EntitySAU { get; private set; }
|
public BaseSAU? BaseSAU { 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 _drawingSAUWidth = 150;
|
private readonly int _drawingSAUWidth = 150;
|
||||||
private readonly int _drawingSAUHeight = 90;
|
private readonly int _drawingSAUHeight = 80;
|
||||||
|
|
||||||
public void Init (int speed, double weight, Color bodyColor, Color additionalColor, bool armor, bool guns)
|
|
||||||
|
|
||||||
|
public int? GetPosX => _startPosX;
|
||||||
|
public int? GetPosY => _startPosY;
|
||||||
|
public int GetWidth => _drawingSAUWidth;
|
||||||
|
public int GetHeight => _drawingSAUHeight;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public DrawingBase(int speed, double weight, Color bodyColor) : this()
|
||||||
|
{
|
||||||
|
BaseSAU =new BaseSAU(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingBase()
|
||||||
{
|
{
|
||||||
EntitySAU = new EntitySAU();
|
|
||||||
EntitySAU.Init(speed, weight, bodyColor, additionalColor, armor, guns);
|
|
||||||
_pictureWidth = null;
|
_pictureWidth = null;
|
||||||
_pictureHeight = null;
|
_pictureHeight = null;
|
||||||
_startPosX = null;
|
_startPosX = null;
|
||||||
_startPosY = null;
|
_startPosY = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SetPictureSize(int width,int height)
|
public DrawingBase(int width,int height) : this()
|
||||||
|
{
|
||||||
|
_drawingSAUWidth = width;
|
||||||
|
_drawingSAUHeight = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SetPictureSize(int width, int height)
|
||||||
{
|
{
|
||||||
if (width > _pictureWidth || height > _pictureHeight)
|
if (width > _pictureWidth || height > _pictureHeight)
|
||||||
{
|
{
|
||||||
@ -54,17 +73,11 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
|
|
||||||
public void SetPosition(int x, int y)
|
public void SetPosition(int x, int y)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
|
|
||||||
if (x < 0)
|
if (x < 0)
|
||||||
{
|
{
|
||||||
_startPosX = 0;
|
_startPosX = 0;
|
||||||
@ -85,39 +98,41 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
_startPosY = _pictureHeight - _drawingSAUHeight;
|
_startPosY = _pictureHeight - _drawingSAUHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool MoveSAU(DirectionType direction)
|
public bool MoveSAU(DirectionType direction)
|
||||||
{
|
{
|
||||||
if(EntitySAU==null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (BaseSAU == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
case DirectionType.Left:
|
case DirectionType.Left:
|
||||||
if (_startPosX.Value - EntitySAU.Step >0)
|
if (_startPosX.Value - BaseSAU.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosX-=(int)EntitySAU.Step;
|
_startPosX -= (int)BaseSAU.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
case DirectionType.Up:
|
case DirectionType.Up:
|
||||||
if(_startPosY.Value - EntitySAU.Step >0)
|
if (_startPosY.Value - BaseSAU.Step > 0)
|
||||||
{
|
{
|
||||||
_startPosY-=(int)EntitySAU.Step;
|
_startPosY -= (int)BaseSAU.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
case DirectionType.Right:
|
case DirectionType.Right:
|
||||||
if (_startPosX.Value + EntitySAU.Step +_drawingSAUWidth < _pictureWidth.Value)
|
if (_startPosX.Value + BaseSAU.Step + _drawingSAUWidth < _pictureWidth.Value)
|
||||||
{
|
{
|
||||||
_startPosX += (int)EntitySAU.Step;
|
_startPosX += (int)BaseSAU.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
case DirectionType.Down:
|
case DirectionType.Down:
|
||||||
if (_startPosY.Value + EntitySAU.Step + _drawingSAUHeight < _pictureHeight.Value)
|
if (_startPosY.Value + BaseSAU.Step + _drawingSAUHeight < _pictureHeight.Value)
|
||||||
{
|
{
|
||||||
_startPosY += (int)EntitySAU.Step;
|
_startPosY += (int)BaseSAU.Step;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
@ -125,28 +140,17 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawSAU(Graphics g)
|
public virtual void DrawSAU(Graphics g)
|
||||||
{
|
{
|
||||||
if (EntitySAU == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
if (BaseSAU == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pen pen = new(Color.Black);
|
|
||||||
Brush additionalBrush = new SolidBrush(EntitySAU.AdditionalColor);
|
|
||||||
Brush BaseBrush = new SolidBrush(Color.Brown);
|
|
||||||
Brush brBlack = new SolidBrush(Color.Black);
|
|
||||||
|
|
||||||
if (EntitySAU.Wheels)
|
Pen pen = new(Color.Black);
|
||||||
{
|
Brush BaseBrush = new SolidBrush(BaseSAU.BodyColor);
|
||||||
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 45, 40, 40);
|
|
||||||
g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 45, 40, 40);
|
|
||||||
g.FillEllipse(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 45, 40, 40);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 55, 130, 40);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Корпус
|
//Корпус
|
||||||
Point point1 = new Point(_startPosX.Value, _startPosY.Value + 45);
|
Point point1 = new Point(_startPosX.Value, _startPosY.Value + 45);
|
||||||
@ -156,17 +160,17 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
Point point5 = new Point(_startPosX.Value, _startPosY.Value + 65);
|
Point point5 = new Point(_startPosX.Value, _startPosY.Value + 65);
|
||||||
|
|
||||||
Point[] Points1 = { point1, point2, point3, point4, point5 };
|
Point[] Points1 = { point1, point2, point3, point4, point5 };
|
||||||
g.FillPolygon (BaseBrush, Points1);
|
g.FillPolygon(BaseBrush, Points1);
|
||||||
|
|
||||||
g.FillRectangle(BaseBrush, _startPosX.Value + 20, _startPosY.Value + 15, 80, 30);
|
g.FillRectangle(BaseBrush, _startPosX.Value + 20, _startPosY.Value + 15, 80, 30);
|
||||||
|
|
||||||
//Ствол
|
//Ствол
|
||||||
|
|
||||||
Point point6 = new Point(_startPosX.Value+ 100, _startPosY.Value + 25);
|
Point point6 = new Point(_startPosX.Value + 100, _startPosY.Value + 25);
|
||||||
Point point7 = new Point(_startPosX.Value + 150, _startPosY.Value + 10);
|
Point point7 = new Point(_startPosX.Value + 150, _startPosY.Value + 10);
|
||||||
Point point8 = new Point(_startPosX.Value + 150, _startPosY.Value + 20);
|
Point point8 = new Point(_startPosX.Value + 150, _startPosY.Value + 20);
|
||||||
Point point9 = new Point(_startPosX.Value + 100, _startPosY.Value + 35);
|
Point point9 = new Point(_startPosX.Value + 100, _startPosY.Value + 35);
|
||||||
Point[] Points2 = { point6, point7, point8, point9};
|
Point[] Points2 = { point6, point7, point8, point9 };
|
||||||
g.FillPolygon(BaseBrush, Points2);
|
g.FillPolygon(BaseBrush, Points2);
|
||||||
|
|
||||||
Point point10 = new Point(_startPosX.Value + 140, _startPosY.Value + 10);
|
Point point10 = new Point(_startPosX.Value + 140, _startPosY.Value + 10);
|
||||||
@ -176,16 +180,8 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
Point[] Points3 = { point10, point11, point12, point13 };
|
Point[] Points3 = { point10, point11, point12, point13 };
|
||||||
g.FillPolygon(BaseBrush, Points3);
|
g.FillPolygon(BaseBrush, Points3);
|
||||||
|
|
||||||
//пулеметы
|
|
||||||
|
|
||||||
if (EntitySAU.Guns)
|
|
||||||
{
|
|
||||||
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value, 5, 15);
|
|
||||||
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value, 5, 15);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,62 @@
|
|||||||
|
using Project_SelfPropelledArtilleryUnit.Entity;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.Drawings;
|
||||||
|
|
||||||
|
public class DrawingSAU : DrawingBase
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public DrawingSAU(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool guns) : base(150, 100)
|
||||||
|
{
|
||||||
|
BaseSAU = new EntitySAU( speed, weight, bodyColor, additionalColor, wheels, guns);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void DrawSAU(Graphics g)
|
||||||
|
{
|
||||||
|
if (BaseSAU==null || BaseSAU is not EntitySAU SAU || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pen pen = new(Color.Black);
|
||||||
|
Brush additionalBrush = new SolidBrush(SAU.AdditionalColor);
|
||||||
|
|
||||||
|
|
||||||
|
if (SAU.Wheels)
|
||||||
|
{
|
||||||
|
|
||||||
|
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 45, 40, 40);
|
||||||
|
g.FillEllipse(additionalBrush, _startPosX.Value + 40, _startPosY.Value + 45, 40, 40);
|
||||||
|
g.FillEllipse(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 45, 40, 40);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
g.FillEllipse(additionalBrush, _startPosX.Value, _startPosY.Value + 55, 130, 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
//пулеметы
|
||||||
|
|
||||||
|
if (SAU.Guns)
|
||||||
|
{
|
||||||
|
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value, 5, 15);
|
||||||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 75, _startPosY.Value, 5, 15);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
base.DrawSAU(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.Entity
|
||||||
|
{
|
||||||
|
public class BaseSAU
|
||||||
|
{
|
||||||
|
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 BaseSAU(int speed, double weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.Entity;
|
||||||
|
|
||||||
|
public class EntitySAU: BaseSAU
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public Color AdditionalColor { get; private set; }
|
||||||
|
public bool Wheels { get; private set; }
|
||||||
|
//длина ствола
|
||||||
|
public bool Guns { get; private set; }
|
||||||
|
|
||||||
|
public EntitySAU(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool guns) : base(speed, weight, bodyColor)
|
||||||
|
{
|
||||||
|
AdditionalColor = additionalColor;
|
||||||
|
Wheels = wheels;
|
||||||
|
Guns = guns;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,33 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Project_SelfPropelledArtilleryUnit
|
|
||||||
{
|
|
||||||
public class EntitySAU
|
|
||||||
{
|
|
||||||
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 Wheels { get; private set; }
|
|
||||||
//длина ствола
|
|
||||||
public bool Guns { get; private set; }
|
|
||||||
|
|
||||||
public double Step => Speed * 100 / Weight;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool guns)
|
|
||||||
{
|
|
||||||
Speed = speed;
|
|
||||||
Weight = weight;
|
|
||||||
BodyColor = bodyColor;
|
|
||||||
AdditionalColor = additionalColor;
|
|
||||||
Wheels = wheels;
|
|
||||||
Guns = guns;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -34,6 +34,9 @@
|
|||||||
up = new Button();
|
up = new Button();
|
||||||
right = new Button();
|
right = new Button();
|
||||||
down = new Button();
|
down = new Button();
|
||||||
|
baseSAU = new Button();
|
||||||
|
comboBoxStrategy = new ComboBox();
|
||||||
|
strategyStep = new Button();
|
||||||
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
|
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -105,11 +108,45 @@
|
|||||||
down.UseVisualStyleBackColor = true;
|
down.UseVisualStyleBackColor = true;
|
||||||
down.Click += Move_Click;
|
down.Click += Move_Click;
|
||||||
//
|
//
|
||||||
|
// baseSAU
|
||||||
|
//
|
||||||
|
baseSAU.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
|
baseSAU.Location = new Point(112, 481);
|
||||||
|
baseSAU.Name = "baseSAU";
|
||||||
|
baseSAU.Size = new Size(94, 29);
|
||||||
|
baseSAU.TabIndex = 6;
|
||||||
|
baseSAU.Text = "creat base sau";
|
||||||
|
baseSAU.UseVisualStyleBackColor = true;
|
||||||
|
baseSAU.Click += baseSAU_Click;
|
||||||
|
//
|
||||||
|
// comboBoxStrategy
|
||||||
|
//
|
||||||
|
comboBoxStrategy.FormattingEnabled = true;
|
||||||
|
comboBoxStrategy.Items.AddRange(new object[] { "to the center", "to the edge" });
|
||||||
|
comboBoxStrategy.Location = new Point(891, 12);
|
||||||
|
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||||
|
comboBoxStrategy.Size = new Size(151, 28);
|
||||||
|
comboBoxStrategy.TabIndex = 7;
|
||||||
|
//
|
||||||
|
// strategyStep
|
||||||
|
//
|
||||||
|
strategyStep.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||||
|
strategyStep.Location = new Point(948, 46);
|
||||||
|
strategyStep.Name = "strategyStep";
|
||||||
|
strategyStep.Size = new Size(94, 29);
|
||||||
|
strategyStep.TabIndex = 8;
|
||||||
|
strategyStep.Text = "Step";
|
||||||
|
strategyStep.UseVisualStyleBackColor = true;
|
||||||
|
strategyStep.Click += strategyStep_Click;
|
||||||
|
//
|
||||||
// FormSAU
|
// FormSAU
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||||
AutoScaleMode = AutoScaleMode.Font;
|
AutoScaleMode = AutoScaleMode.Font;
|
||||||
ClientSize = new Size(1054, 522);
|
ClientSize = new Size(1054, 522);
|
||||||
|
Controls.Add(strategyStep);
|
||||||
|
Controls.Add(comboBoxStrategy);
|
||||||
|
Controls.Add(baseSAU);
|
||||||
Controls.Add(down);
|
Controls.Add(down);
|
||||||
Controls.Add(right);
|
Controls.Add(right);
|
||||||
Controls.Add(up);
|
Controls.Add(up);
|
||||||
@ -130,5 +167,8 @@
|
|||||||
private Button up;
|
private Button up;
|
||||||
private Button right;
|
private Button right;
|
||||||
private Button down;
|
private Button down;
|
||||||
|
private Button baseSAU;
|
||||||
|
private ComboBox comboBoxStrategy;
|
||||||
|
private Button strategyStep;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,16 +4,20 @@ using System.ComponentModel;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.AccessControl;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using Project_SelfPropelledArtilleryUnit.Drawings;
|
||||||
|
using Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
namespace Project_SelfPropelledArtilleryUnit
|
namespace Project_SelfPropelledArtilleryUnit;
|
||||||
|
|
||||||
|
public partial class FormSAU : Form
|
||||||
{
|
{
|
||||||
public partial class FormSAU : Form
|
private DrawingBase? _drawingBase;
|
||||||
{
|
private AbstractStrategy? _strategy;
|
||||||
private DrawingSAU? _drawingSAU;
|
|
||||||
private EntitySAU? _entity;
|
|
||||||
|
|
||||||
public FormSAU()
|
public FormSAU()
|
||||||
{
|
{
|
||||||
@ -22,67 +26,119 @@ namespace Project_SelfPropelledArtilleryUnit
|
|||||||
|
|
||||||
private void Draw()
|
private void Draw()
|
||||||
{
|
{
|
||||||
if (_drawingSAU == null)
|
if (_drawingBase == null)
|
||||||
{
|
{
|
||||||
return;
|
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);
|
||||||
_drawingSAU.DrawSAU(gr);
|
_drawingBase.DrawSAU(gr);
|
||||||
pictureBox1.Image = bmp;
|
pictureBox1.Image = bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Create_Click(object sender, EventArgs e)
|
private void CreateObject(string type)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Random random = new();
|
Random random = new();
|
||||||
_drawingSAU = new DrawingSAU();
|
switch (type)
|
||||||
// _entity = new EntitySAU();
|
{
|
||||||
_drawingSAU.Init(random.Next(100, 300), random.Next(1000, 3000),
|
case nameof(DrawingBase):
|
||||||
|
_drawingBase = new DrawingBase(random.Next(100, 300), random.Next(1000, 3000),
|
||||||
|
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||||
|
break;
|
||||||
|
case nameof(DrawingSAU):
|
||||||
|
_drawingBase = new DrawingSAU(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)),
|
||||||
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)));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
|
||||||
_drawingSAU.SetPictureSize(pictureBox1.Width, pictureBox1.Height);
|
}
|
||||||
_drawingSAU.SetPosition(pictureBox1.Width , pictureBox1.Height);
|
|
||||||
|
|
||||||
|
|
||||||
|
_drawingBase.SetPictureSize(pictureBox1.Width, pictureBox1.Height);
|
||||||
|
_drawingBase.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||||
|
|
||||||
|
_strategy = null;
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
private void Create_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingSAU));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void Move_Click(object sender, EventArgs e)
|
private void Move_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
if(_drawingSAU==null)
|
if (_drawingBase == null)
|
||||||
{
|
{
|
||||||
return;
|
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 "up":
|
case "up":
|
||||||
result = _drawingSAU.MoveSAU(DirectionType.Up);
|
result = _drawingBase.MoveSAU(DirectionType.Up);
|
||||||
break;
|
break;
|
||||||
case "down":
|
case "down":
|
||||||
result = _drawingSAU.MoveSAU(DirectionType.Down);
|
result = _drawingBase.MoveSAU(DirectionType.Down);
|
||||||
break;
|
break;
|
||||||
case "left":
|
case "left":
|
||||||
result = _drawingSAU.MoveSAU(DirectionType.Left);
|
result = _drawingBase.MoveSAU(DirectionType.Left);
|
||||||
break;
|
break;
|
||||||
case "right":
|
case "right":
|
||||||
result = _drawingSAU.MoveSAU(DirectionType.Right);
|
result = _drawingBase.MoveSAU(DirectionType.Right);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result)
|
if (result)
|
||||||
{
|
{
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void baseSAU_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingBase));
|
||||||
|
|
||||||
|
private void strategyStep_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_drawingBase == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comboBoxStrategy.Enabled)
|
||||||
|
{
|
||||||
|
_strategy = comboBoxStrategy.SelectedIndex
|
||||||
|
switch
|
||||||
|
{
|
||||||
|
0 => new MoveToCenter(),
|
||||||
|
1 => new MoveToBorder(),
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_strategy.SetData(new MoveableSAU(_drawingBase), pictureBox1.Width, pictureBox1.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_strategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
comboBoxStrategy.Enabled = false;
|
||||||
|
_strategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
|
||||||
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.Enabled = true;
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,97 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.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 int? GetStep()
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _moveableObject?.GetStep;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
|
||||||
|
|
||||||
|
protected abstract bool IsTargetDestination();
|
||||||
|
|
||||||
|
|
||||||
|
private bool MoveTo(MovementDirection movementDirection)
|
||||||
|
{
|
||||||
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
|
|
||||||
|
public interface IMoveableObject
|
||||||
|
{
|
||||||
|
|
||||||
|
ObjectParameters? GetObjectPosition { get; }
|
||||||
|
|
||||||
|
|
||||||
|
int GetStep { get; }
|
||||||
|
|
||||||
|
|
||||||
|
bool TryMoveObject(MovementDirection direction);
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveToBorder : AbstractStrategy
|
||||||
|
{
|
||||||
|
protected override bool IsTargetDestination()
|
||||||
|
{
|
||||||
|
ObjectParameters? 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()
|
||||||
|
{
|
||||||
|
ObjectParameters? objParams = GetObjectParameters;
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int diffX = objParams.RightBorder - FieldWidth;
|
||||||
|
if (Math.Abs(diffX) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffX > 0)
|
||||||
|
{
|
||||||
|
MoveLeft();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MoveRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int diffY = objParams.DownBorder - FieldHeight;
|
||||||
|
if (Math.Abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY > 0)
|
||||||
|
{
|
||||||
|
MoveUp();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
using Project_SelfPropelledArtilleryUnit.Drawings;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveableSAU : IMoveableObject
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly DrawingBase? _SAU = null;
|
||||||
|
|
||||||
|
|
||||||
|
public MoveableSAU(DrawingBase sau)
|
||||||
|
{
|
||||||
|
_SAU = sau;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectParameters? GetObjectPosition
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_SAU == null || _SAU.BaseSAU == null || !_SAU.GetPosX.HasValue || !_SAU.GetPosY.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_SAU.GetPosX.Value, _SAU.GetPosY.Value, _SAU.GetWidth, _SAU.GetHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetStep => (int)(_SAU?.BaseSAU.Step ?? 0);
|
||||||
|
|
||||||
|
public bool TryMoveObject(MovementDirection direction)
|
||||||
|
{
|
||||||
|
if (_SAU == null || _SAU.BaseSAU == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _SAU.MoveSAU(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.Unknow,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
|
public enum MovementDirection
|
||||||
|
{
|
||||||
|
|
||||||
|
Up = 1,
|
||||||
|
Down = 2,
|
||||||
|
Left = 3,
|
||||||
|
Right = 4
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Project_SelfPropelledArtilleryUnit.MovementStrategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus
|
||||||
|
{
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user