исправление

This commit is contained in:
Salikh 2023-11-27 23:25:40 +04:00
parent 669f8b4568
commit 64abc50ba4
13 changed files with 585 additions and 169 deletions

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawningObjects;
namespace AirBomber.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(Diraction.Left);
protected bool MoveRight() => MoveTo(Diraction.Right);
protected bool MoveUp() => MoveTo(Diraction.Up);
protected bool MoveDown() => MoveTo(Diraction.Down);
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosit;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(Diraction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -4,113 +4,30 @@ using System.Drawing.Drawing2D;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using AirBomber.Entities;
namespace AirBomber namespace AirBomber.DrawningObjects
{ {
public class DrawningAirBomber public class DrawningAirBomber : DrawningBomber
{ {
public EntityAirBomber? EntityAirBomber { get; private set; } public DrawningAirBomber(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool rocket, int width, int height) : base(speed, weight, bodycolor, width, height, 160, 185)
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _PlaneWidth = 160;
private int _PlaneHeight = 185;
public bool Init(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool rocket, int width, int height)
{ {
if (width < _pictureWidth || height < _pictureHeight) if (EntityBomber != null)
{ {
return false; EntityBomber = new EntityAirBomber(speed, weight, bodycolor, dopcolor, toplivo, rocket);
} }
_pictureWidth = width;
_pictureHeight = height;
EntityAirBomber = new EntityAirBomber();
EntityAirBomber.Init(speed, weight, bodycolor, dopcolor, toplivo, rocket);
return true;
} }
public override void DrawBomber(Graphics g)
public void SetPosition(int x, int y)
{ {
if (x < 0 || x + _PlaneWidth > _pictureWidth) if (EntityBomber is not EntityAirBomber airBomber)
{
x = _pictureWidth - _PlaneWidth;
}
if (y < 0 || y + _PlaneWidth > _pictureHeight)
{
y = _pictureHeight - _PlaneWidth;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Diraction diraction)
{
if (EntityAirBomber == null)
{ {
return; return;
} }
int step = (int)EntityAirBomber.Step;
switch (diraction)
{
case Diraction.Left:
if (_startPosX - step >= 0)
{
_startPosX -= step;
}
else
{
_startPosX = 0;
}
break;
case Diraction.Right:
if (_startPosX + step + _PlaneWidth <= _pictureWidth)
{
_startPosX += step;
}
else
{
_startPosX = _pictureWidth - _PlaneWidth;
}
break;
case Diraction.Up:
if (_startPosY - step >= 0)
{
_startPosY -= step;
}
else
{
_startPosY = 0;
}
break;
case Diraction.Down:
if (_startPosY + step + _PlaneHeight <= _pictureHeight)
{
_startPosY += step;
}
else
{
_startPosY = _pictureHeight - _PlaneHeight;
}
break;
}
}
public void DrawCar(Graphics g)
{
if (EntityAirBomber == null)
{
return;
}
Pen pen = new(Color.Black); Pen pen = new(Color.Black);
Brush dopcolor = new SolidBrush(EntityAirBomber.DopColor); Brush dopcolor = new SolidBrush(airBomber.DopColor);
if (EntityAirBomber.Toplivo) //отрисовка ракет
if (airBomber.Rocket)
{ {
//отрисовка ракет
GraphicsPath rocket_1 = new GraphicsPath(); GraphicsPath rocket_1 = new GraphicsPath();
rocket_1.AddLine(_startPosX + 70, _startPosY + 35, _startPosX + 80, _startPosY + 25); rocket_1.AddLine(_startPosX + 70, _startPosY + 35, _startPosX + 80, _startPosY + 25);
rocket_1.AddLine(_startPosX + 80, _startPosY + 25, _startPosX + 80, _startPosY + 45); rocket_1.AddLine(_startPosX + 80, _startPosY + 25, _startPosX + 80, _startPosY + 45);
@ -136,7 +53,8 @@ namespace AirBomber
g.FillPath(dopcolor, rocket_4); g.FillPath(dopcolor, rocket_4);
g.DrawPath(Pens.Black, rocket_4); g.DrawPath(Pens.Black, rocket_4);
} }
if (EntityAirBomber.Rocket) { if (airBomber.Toplivo)
{
//отрисовка баков //отрисовка баков
g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 5, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 5, 8, 10);
g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 25, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 25, 8, 10);
@ -145,42 +63,7 @@ namespace AirBomber
g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 150, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 150, 8, 10);
g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 170, 8, 10); g.FillRectangle(dopcolor, _startPosX + 82, _startPosY + 170, 8, 10);
} }
//отрисовка крыла 1 base.DrawBomber(g);
GraphicsPath fly_1 = new GraphicsPath();
fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 80, _startPosY + 80);
fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 90, _startPosY + 2);
fly_1.AddLine(_startPosX + 90, _startPosY + 2, _startPosX + 100, _startPosY + 80);
fly_1.AddLine(_startPosX + 100, _startPosY + 80, _startPosX + 80, _startPosY + 80);
g.DrawPath(Pens.Black, fly_1);
//отрисовка кабины пилота
GraphicsPath treygol = new GraphicsPath();
treygol.AddLine(_startPosX + 3, _startPosY + 95, _startPosX + 30, _startPosY + 80);
treygol.AddLine(_startPosX + 30, _startPosY + 80, _startPosX + 30, _startPosY + 105);
treygol.CloseFigure();
g.FillPath(Brushes.Black, treygol);
g.DrawPath(Pens.Black, treygol);
//отрисовка корпуса
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 80, 120, 25);
//отрисовка крыла 2
GraphicsPath fly_2 = new GraphicsPath();
fly_2.AddLine(_startPosX + 80, _startPosY + 105, _startPosX + 80, _startPosY + 185);
fly_2.AddLine(_startPosX + 80, _startPosY + 185, _startPosX + 90, _startPosY + 185);
fly_2.AddLine(_startPosX + 90, _startPosY + 185, _startPosX + 100, _startPosY + 105);
fly_2.CloseFigure();
g.DrawPath(Pens.Black, fly_2);
//отриосвка хвоста
GraphicsPath wing = new GraphicsPath();
wing.AddLine(_startPosX + 135, _startPosY + 80, _startPosX + 135, _startPosY + 70);
wing.AddLine(_startPosX + 135, _startPosY + 70, _startPosX + 150, _startPosY + 50);
wing.AddLine(_startPosX + 150, _startPosY + 50, _startPosX + 150, _startPosY + 80);
wing.CloseFigure();
g.DrawPath(Pens.Black, wing);
GraphicsPath wing_2 = new GraphicsPath();
wing_2.AddLine(_startPosX + 135, _startPosY + 105, _startPosX + 135, _startPosY + 115);
wing_2.AddLine(_startPosX + 135, _startPosY + 115, _startPosX + 150, _startPosY + 135);
wing_2.AddLine(_startPosX + 150, _startPosY + 135, _startPosX + 150, _startPosY + 105);
wing_2.CloseFigure();
g.DrawPath(Pens.Black, wing_2);
} }
} }
} }

162
AirBomber/DrawningBomber.cs Normal file
View File

@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.Entities;
namespace AirBomber.DrawningObjects
{
public class DrawningBomber
{
public EntityBomber? EntityBomber { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _PlaneWidth = 160;
protected int _PlaneHeight = 185;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _PlaneWidth;
public int GetHeight => _PlaneHeight;
public DrawningBomber(int speed, double weight, Color bodycolor, int width, int height)
{
if (width < _pictureWidth || height < _pictureHeight)
{
throw new InvalidOperationException("Invalid weight or height");
}
_pictureWidth = width;
_pictureHeight = height;
EntityBomber = new EntityBomber(speed, weight, bodycolor);
}
protected DrawningBomber(int speed, double weight, Color bodycolor, int width, int height, int planeWidth, int planeHeight)
{
if (width < _pictureWidth || height < _pictureHeight)
{
throw new InvalidOperationException("Invalid width or height");
}
_pictureWidth = width;
_pictureHeight = height;
_PlaneWidth = planeWidth;
_PlaneHeight = planeHeight;
EntityBomber = new EntityBomber(speed, weight, bodycolor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _PlaneWidth > _pictureWidth)
{
x = _pictureWidth - _PlaneWidth;
}
if (y < 0 || y + _PlaneWidth > _pictureHeight)
{
y = _pictureHeight - _PlaneWidth;
}
_startPosX = x;
_startPosY = y;
}
public bool CanMove(Diraction direction)
{
if (EntityBomber == null)
{
return false;
}
int newPosX = _startPosX;
int newPosY = _startPosY;
switch (direction)
{
case Diraction.Left:
newPosX -= (int)EntityBomber.Step;
break;
case Diraction.Right:
newPosX += (int)EntityBomber.Step;
break;
case Diraction.Up:
newPosY -= (int)EntityBomber.Step;
break;
case Diraction.Down:
newPosY += (int)EntityBomber.Step;
break;
}
return newPosX >= 0 && newPosX <= _pictureWidth - _PlaneWidth &&
newPosY >= 0 && newPosY <= _pictureHeight - _PlaneHeight;
}
public void MoveTransport(Diraction diraction)
{
if (!CanMove(diraction) || EntityBomber == null)
{
return;
}
switch (diraction)
{
case Diraction.Left:
_startPosX -= (int)EntityBomber.Step;
break;
case Diraction.Right:
_startPosX += (int)EntityBomber.Step;
break;
case Diraction.Up:
_startPosY -= (int)EntityBomber.Step;
break;
case Diraction.Down:
_startPosY += (int)EntityBomber.Step;
break;
}
}
public virtual void DrawBomber(Graphics g)
{
if (EntityBomber == null)
{
return;
}
Pen pen = new(Color.Black);
//отрисовка крыла 1
GraphicsPath fly_1 = new GraphicsPath();
fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 80, _startPosY + 80);
fly_1.AddLine(_startPosX + 80, _startPosY + 2, _startPosX + 90, _startPosY + 2);
fly_1.AddLine(_startPosX + 90, _startPosY + 2, _startPosX + 100, _startPosY + 80);
fly_1.AddLine(_startPosX + 100, _startPosY + 80, _startPosX + 80, _startPosY + 80);
g.DrawPath(Pens.Black, fly_1);
//отрисовка кабины пилота
GraphicsPath treygol = new GraphicsPath();
treygol.AddLine(_startPosX + 3, _startPosY + 95, _startPosX + 30, _startPosY + 80);
treygol.AddLine(_startPosX + 30, _startPosY + 80, _startPosX + 30, _startPosY + 105);
treygol.CloseFigure();
g.FillPath(Brushes.Black, treygol);
g.DrawPath(Pens.Black, treygol);
//отрисовка корпуса
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 80, 120, 25);
//отрисовка крыла 2
GraphicsPath fly_2 = new GraphicsPath();
fly_2.AddLine(_startPosX + 80, _startPosY + 105, _startPosX + 80, _startPosY + 185);
fly_2.AddLine(_startPosX + 80, _startPosY + 185, _startPosX + 90, _startPosY + 185);
fly_2.AddLine(_startPosX + 90, _startPosY + 185, _startPosX + 100, _startPosY + 105);
fly_2.CloseFigure();
g.DrawPath(Pens.Black, fly_2);
//отриосвка хвоста
GraphicsPath wing = new GraphicsPath();
wing.AddLine(_startPosX + 135, _startPosY + 80, _startPosX + 135, _startPosY + 70);
wing.AddLine(_startPosX + 135, _startPosY + 70, _startPosX + 150, _startPosY + 50);
wing.AddLine(_startPosX + 150, _startPosY + 50, _startPosX + 150, _startPosY + 80);
wing.CloseFigure();
g.DrawPath(Pens.Black, wing);
GraphicsPath wing_2 = new GraphicsPath();
wing_2.AddLine(_startPosX + 135, _startPosY + 105, _startPosX + 135, _startPosY + 115);
wing_2.AddLine(_startPosX + 135, _startPosY + 115, _startPosX + 150, _startPosY + 135);
wing_2.AddLine(_startPosX + 150, _startPosY + 135, _startPosX + 150, _startPosY + 105);
wing_2.CloseFigure();
g.DrawPath(Pens.Black, wing_2);
}
}
}

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawningObjects;
namespace AirBomber.MovementStrategy
{
public class DrawningObjectBomber : IMoveableObject
{
private readonly DrawningBomber? _drawningBomber = null;
public DrawningObjectBomber(DrawningBomber drawningBomber)
{
_drawningBomber = drawningBomber;
}
public ObjectParameters? GetObjectPosit
{
get
{
if (_drawningBomber == null || _drawningBomber.EntityBomber == null)
{
return null;
}
return new ObjectParameters(_drawningBomber.GetPosX, _drawningBomber.GetPosY, _drawningBomber.GetWidth, _drawningBomber.GetHeight);
}
}
public int GetStep => (int)(_drawningBomber?.EntityBomber?.Step ?? 0);
public bool CheckCanMove(Diraction direction) => _drawningBomber?.CanMove(direction) ?? false;
public void MoveObject(Diraction direction) => _drawningBomber?.MoveTransport(direction);
}
}

View File

@ -1,26 +1,13 @@
using System; namespace AirBomber.Entities
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{ {
public class EntityAirBomber public class EntityAirBomber : EntityBomber
{ {
public int Speed { get; private set; }
public int Weight { get; private set; }
public Color BodyColor { get; private set; }
public Color DopColor { get; private set; } public Color DopColor { get; private set; }
public bool Toplivo { get; private set; } public bool Toplivo { get; private set; }
public bool Rocket { get; private set; } public bool Rocket { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, int weight, Color bodycolor, Color dopcolor, bool toplivo, bool ropcket) public EntityAirBomber(int speed, double weight, Color bodycolor, Color dopcolor, bool toplivo, bool ropcket) : base(speed, weight, bodycolor)
{ {
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
DopColor = dopcolor; DopColor = dopcolor;
Toplivo = toplivo; Toplivo = toplivo;
Rocket = ropcket; Rocket = ropcket;

23
AirBomber/EntityBomber.cs Normal file
View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber.Entities
{
public class EntityBomber
{
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 EntityBomber(int speed, double weight, Color bodycolor)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
}
}
}

View File

@ -34,6 +34,9 @@
buttonLeft = new Button(); buttonLeft = new Button();
buttonUp = new Button(); buttonUp = new Button();
buttonCreate = new Button(); buttonCreate = new Button();
buttonCreateWarBomber = new Button();
comboBoxStrategy = new ComboBox();
ButtonStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -97,19 +100,52 @@
// buttonCreate // buttonCreate
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(30, 395); buttonCreate.Location = new Point(223, 385);
buttonCreate.Name = "buttonCreate"; buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(131, 43); buttonCreate.Size = new Size(181, 59);
buttonCreate.TabIndex = 6; buttonCreate.TabIndex = 6;
buttonCreate.Text = "Создать"; buttonCreate.Text = "Создать самолёт";
buttonCreate.UseVisualStyleBackColor = true; buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; buttonCreate.Click += buttonCreate_Click;
// //
// buttonCreateWarBomber
//
buttonCreateWarBomber.Location = new Point(12, 385);
buttonCreateWarBomber.Name = "buttonCreateWarBomber";
buttonCreateWarBomber.Size = new Size(205, 59);
buttonCreateWarBomber.TabIndex = 7;
buttonCreateWarBomber.Text = "Создать военный самолёт";
buttonCreateWarBomber.UseVisualStyleBackColor = true;
buttonCreateWarBomber.Click += buttonCreateWarBomber_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "В центр", "В правый нижний угол" });
comboBoxStrategy.Location = new Point(637, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(151, 28);
comboBoxStrategy.TabIndex = 8;
//
// ButtonStep
//
ButtonStep.Location = new Point(694, 60);
ButtonStep.Name = "ButtonStep";
ButtonStep.Size = new Size(94, 29);
ButtonStep.TabIndex = 9;
ButtonStep.Text = "Шаг";
ButtonStep.UseVisualStyleBackColor = true;
ButtonStep.Click += ButtonStep_Click;
//
// FormAirBomber // FormAirBomber
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(ButtonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateWarBomber);
Controls.Add(buttonCreate); Controls.Add(buttonCreate);
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
@ -130,5 +166,8 @@
private Button buttonLeft; private Button buttonLeft;
private Button buttonUp; private Button buttonUp;
private Button buttonCreate; private Button buttonCreate;
private Button buttonCreateWarBomber;
private ComboBox comboBoxStrategy;
private Button ButtonStep;
} }
} }

View File

@ -1,8 +1,13 @@
using AirBomber.DrawningObjects;
using AirBomber.MovementStrategy;
namespace AirBomber namespace AirBomber
{ {
public partial class FormAirBomber : Form public partial class FormAirBomber : Form
{ {
private DrawningAirBomber? _drawingair; private DrawningBomber? _drawningBomber;
private AbstractStrategy? _abstractStrategy;
public FormAirBomber() public FormAirBomber()
{ {
InitializeComponent(); InitializeComponent();
@ -10,37 +15,29 @@ namespace AirBomber
private void Draw() private void Draw()
{ {
if (_drawningBomber == null)
if (_drawingair == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBox.Width, Bitmap bmp = new(pictureBox.Width,
pictureBox.Height); pictureBox.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingair.DrawCar(gr); _drawningBomber.DrawBomber(gr);
pictureBox.Image = bmp; pictureBox.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) private void buttonCreate_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawingair = new DrawningAirBomber(); _drawningBomber = new DrawningBomber(random.Next(100, 300), random.Next(1000, 3000),
_drawingair.Init(random.Next(100, 300), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBox.Width, pictureBox.Height);
random.Next(1000, 3000), _drawningBomber.SetPosition(random.Next(10, 100), random.Next(10, 100));
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)),
pictureBox.Width, pictureBox.Height);
_drawingair.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw(); Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_drawingair == null) if (_drawningBomber == null)
{ {
return; return;
} }
@ -48,19 +45,71 @@ namespace AirBomber
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawingair.MoveTransport(Diraction.Up); _drawningBomber.MoveTransport(Diraction.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawingair.MoveTransport(Diraction.Down); _drawningBomber.MoveTransport(Diraction.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawingair.MoveTransport(Diraction.Left); _drawningBomber.MoveTransport(Diraction.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawingair.MoveTransport(Diraction.Right); _drawningBomber.MoveTransport(Diraction.Right);
break; break;
} }
Draw(); Draw();
} }
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningBomber == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBottomRight(),
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectBomber(_drawningBomber), pictureBox.Width,
pictureBox.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonCreateWarBomber_Click(object sender, EventArgs e)
{
Random random = new();
_drawningBomber = new DrawningAirBomber(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(1, 2)),
pictureBox.Width, pictureBox.Height);
_drawningBomber.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
} }
} }

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawningObjects;
namespace AirBomber.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosit { get; }
int GetStep { get; }
bool CheckCanMove(Diraction direction);
void MoveObject(Diraction direction);
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.MovementStrategy;
namespace AirBomber.MovementStrategy
{
public class MoveToBottomRight : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
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();
}
}
}
}

55
AirBomber/MoveToCenter.cs Normal file
View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.MovementStrategy;
namespace AirBomber.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,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber.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;
}
}
}

15
AirBomber/Status.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}