Лабораторная работа 2

This commit is contained in:
Efi 2023-11-30 10:56:34 +04:00
parent 688c992165
commit 59883cb493
15 changed files with 719 additions and 226 deletions

View File

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

View File

@ -2,7 +2,7 @@
namespace AirBomber
{
internal enum DirectionType
public enum DirectionType
{
Up = 1,
Down = 2,

View File

@ -1,132 +1,29 @@
using System;
using AirBomber.Entities;
using System;
namespace AirBomber
namespace AirBomber.DrawingObjects
{
internal class DrawingAirBomber
internal class DrawingAirBomber : DrawingWarAirplane
{
public EntityAirBomber AirBomber { get; private set; }
private float _startPosX;
private float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
private readonly int _AirBomberWidth = 100;
private readonly int _AirBomberHeight = 90;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, bool fuelTank,
bool bombs, int numEngine)
public DrawingAirBomber(int speed, float weight, Color bodyColor, Color additionalColor, bool fuelTank,
bool bombs, int width, int height) : base(speed, weight, bodyColor, width, height, 110, 100)
{
AirBomber = new EntityAirBomber();
AirBomber.Init(speed, weight, bodyColor, additionalColor, fuelTank, bombs, numEngine);
}
public void SetPosition(int x, int y, int width, int height)
if(EntityWarAirplane != null)
{
//Сделать проверки (все параметры больше 0 и координаты не выходят за границы полей)
//x
if ((x < 0 || (x + _AirBomberWidth > width)) || (y < 0 || (y + _AirBomberHeight > height)))
{
_startPosX = 0;
_startPosY = 0;
_pictureWidth = width;
_pictureHeight = height;
}
else
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
}
public void checkMove()
{
if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY < 0)
{
_startPosY = 0;
EntityWarAirplane = new EntityAirBomber(speed, weight, bodyColor, additionalColor, fuelTank, bombs);
}
}
public void MoveTransport(DirectionType direction)
public override void DrawTransport(Graphics g)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
case DirectionType.Right:
if (_startPosX + _AirBomberWidth + AirBomber.Step < _pictureWidth)
{
_startPosX += AirBomber.Step;
}
break;
case DirectionType.Left:
if (_startPosX - _AirBomberWidth - AirBomber.Step < _pictureWidth)
{
_startPosX -= AirBomber.Step;
}
break;
case DirectionType.Up:
if (_startPosY - _AirBomberHeight - AirBomber.Step < _pictureHeight)
{
_startPosY -= AirBomber.Step;
}
break;
case DirectionType.Down:
if (_startPosY + _AirBomberHeight + AirBomber.Step < _pictureHeight)
{
_startPosY += AirBomber.Step;
}
break;
}
checkMove();
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
if (EntityWarAirplane is not EntityAirBomber airBomber)
{
return;
}
Pen pen = new(Color.Black);
//нос бомбардировщика
PointF point1 = new PointF(_startPosX + 100, _startPosY + 45);
PointF point2 = new PointF(_startPosX + 80, _startPosY + 40);
PointF point3 = new PointF(_startPosX + 80, _startPosY + 50);
PointF point4 = new PointF(_startPosX + 100, _startPosY + 45);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4
};
Brush br = new SolidBrush(Color.Gray);
g.FillPolygon(br, curvePoints);
g.DrawPolygon(pen, curvePoints);
Brush additionalBrush = new SolidBrush(airBomber.AdditionalColor);
base.DrawTransport(g);
//Крылья
PointF point5 = new PointF(_startPosX + 50, _startPosY + 40);
@ -141,8 +38,7 @@ namespace AirBomber
point8
};
SolidBrush wingBrush = new SolidBrush(AirBomber?.AdditionalColor ?? Color.Gray);
g.FillPolygon(wingBrush, upWing);
g.FillPolygon(additionalBrush, upWing);
g.DrawPolygon(pen, upWing);
PointF point9 = new PointF(_startPosX + 50, _startPosY + 50);
@ -157,7 +53,7 @@ namespace AirBomber
point12
};
g.FillPolygon(wingBrush, downWing);
g.FillPolygon(additionalBrush, downWing);
g.DrawPolygon(pen, downWing);
//Хвост
@ -174,18 +70,11 @@ namespace AirBomber
point16
};
g.FillPolygon(wingBrush, tail);
g.FillPolygon(additionalBrush, tail);
g.DrawPolygon(pen, tail);
//основная часть бомбардировщика
SolidBrush bodyBrush = new SolidBrush(AirBomber?.BodyColor ?? Color.Gray);
Rectangle bodyAirBomber = new Rectangle((int)_startPosX + 30, (int)_startPosY + 40, 50, 10);
g.FillRectangle(bodyBrush, bodyAirBomber);
g.DrawRectangle(pen, bodyAirBomber);
if (airBomber.Bombs)
{
//бомбы
PointF point17 = new PointF(_startPosX + 40, _startPosY + 40);
PointF point18 = new PointF(_startPosX + 35, _startPosY + 35);
@ -220,48 +109,18 @@ namespace AirBomber
g.FillPolygon(brBomb, bomb2);
g.DrawPolygon(pen, bomb2);
//Двигатели (2,4,6)
int yPos = 45;
int yNeg = 30;
for(int i = 0; i < (AirBomber?.NumEngine ?? 1); i++)
{
Rectangle diselEngine = new Rectangle((int)_startPosX, (int)_startPosY + yPos, 15, 15);
g.FillRectangle(bodyBrush, diselEngine);
g.DrawRectangle(pen, diselEngine);
yPos += 15;
diselEngine = new Rectangle((int)_startPosX, (int)_startPosY + yNeg, 15, 15);
g.FillRectangle(bodyBrush, diselEngine);
g.DrawRectangle(pen, diselEngine);
yNeg -= 15;
}
if (airBomber.FuelTank)
{
//топливные баки
Rectangle fuelTank = new Rectangle((int)_startPosX + 50, (int)_startPosY + 43, 20, 5);
g.FillRectangle(wingBrush, fuelTank);
g.FillRectangle(additionalBrush, fuelTank);
g.DrawRectangle(pen, fuelTank);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _AirBomberWidth || _pictureHeight <= _AirBomberHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _AirBomberWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _AirBomberWidth;
}
if (_startPosY + _AirBomberHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _AirBomberHeight;
}
}
}

View File

@ -0,0 +1,37 @@
using AirBomber.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.DrawingObjects;
namespace AirBomber.MovementStrategy
{
internal class DrawingObjectWarAirplane : IMoveableObject
{
private readonly DrawingWarAirplane? _drawingWarAirplane = null;
public DrawingObjectWarAirplane(DrawingWarAirplane drawingWarAirplane)
{
_drawingWarAirplane = drawingWarAirplane;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawingWarAirplane == null || _drawingWarAirplane.EntityWarAirplane ==
null)
{
return null;
}
return new ObjectParameters(_drawingWarAirplane.GetPosX,
_drawingWarAirplane.GetPosY, _drawingWarAirplane.GetWidth, _drawingWarAirplane.GetHeight);
}
}
public int GetStep => (int)(_drawingWarAirplane?.EntityWarAirplane?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawingWarAirplane?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawingWarAirplane?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,235 @@
using AirBomber.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirBomber.Entities;
namespace AirBomber.DrawingObjects
{
public class DrawingWarAirplane
{
public EntityWarAirplane? EntityWarAirplane { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _WarAirplaneWidth = 100;
protected readonly int _WarAirplaneHeight = 90;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _WarAirplaneWidth;
public int GetHeight => _WarAirplaneHeight;
public DrawingWarAirplane(int speed, double weight, Color bodyColor,
int width, int height)
{
if (width < _WarAirplaneWidth || height < _WarAirplaneHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityWarAirplane = new EntityWarAirplane(speed, weight, bodyColor);
}
protected DrawingWarAirplane(int speed, double weight, Color bodyColor,
int width, int height, int WarAirplaneWidth, int WarAirplaneHeight)
{
if (width <= _WarAirplaneWidth || height <= _WarAirplaneHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_WarAirplaneWidth = WarAirplaneWidth;
_WarAirplaneHeight = WarAirplaneHeight;
EntityWarAirplane = new EntityWarAirplane(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
{
_startPosX = 0;
_startPosY = 0;
}
_startPosX = x;
_startPosY = y;
}
public bool CanMove(DirectionType direction)
{
if (EntityWarAirplane == null)
{
return false;
}
return direction switch
{
//влево
DirectionType.Left => _startPosX - EntityWarAirplane.Step > 0,
//вверх
DirectionType.Up => _startPosY - EntityWarAirplane.Step > 0,
// вправо
DirectionType.Right => _startPosX + EntityWarAirplane.Step + _WarAirplaneWidth < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + EntityWarAirplane.Step + _WarAirplaneHeight < _pictureHeight,
_ => false,
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityWarAirplane == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityWarAirplane.Step;
break;
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityWarAirplane.Step;
break;
// вправо
case DirectionType.Right:
_startPosX += (int)EntityWarAirplane.Step;
break;
//вниз
case DirectionType.Down:
_startPosY += (int)EntityWarAirplane.Step;
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityWarAirplane == null)
{
return;
}
Pen pen = new(Color.Black);
//нос бомбардировщика
PointF point1 = new PointF(_startPosX + 100, _startPosY + 45);
PointF point2 = new PointF(_startPosX + 80, _startPosY + 40);
PointF point3 = new PointF(_startPosX + 80, _startPosY + 50);
PointF point4 = new PointF(_startPosX + 100, _startPosY + 45);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4
};
Brush br = new SolidBrush(Color.Gray);
g.FillPolygon(br, curvePoints);
g.DrawPolygon(pen, curvePoints);
//Крылья
PointF point5 = new PointF(_startPosX + 50, _startPosY + 40);
PointF point6 = new PointF(_startPosX + 50, _startPosY + 0);
PointF point7 = new PointF(_startPosX + 55, _startPosY + 0);
PointF point8 = new PointF(_startPosX + 65, _startPosY + 40);
PointF[] upWing =
{
point5,
point6,
point7,
point8
};
SolidBrush wingBrush = new SolidBrush(EntityWarAirplane.BodyColor);
g.FillPolygon(wingBrush, upWing);
g.DrawPolygon(pen, upWing);
PointF point9 = new PointF(_startPosX + 50, _startPosY + 50);
PointF point10 = new PointF(_startPosX + 50, _startPosY + 90);
PointF point11 = new PointF(_startPosX + 55, _startPosY + 90);
PointF point12 = new PointF(_startPosX + 65, _startPosY + 50);
PointF[] downWing =
{
point9,
point10,
point11,
point12
};
g.FillPolygon(wingBrush, downWing);
g.DrawPolygon(pen, downWing);
//Хвост
PointF point13 = new PointF(_startPosX + 30, _startPosY + 30);
PointF point14 = new PointF(_startPosX + 15, _startPosY + 0);
PointF point15 = new PointF(_startPosX + 15, _startPosY + 90);
PointF point16 = new PointF(_startPosX + 30, _startPosY + 60);
PointF[] tail =
{
point13,
point14,
point15,
point16
};
g.FillPolygon(wingBrush, tail);
g.DrawPolygon(pen, tail);
//основная часть бомбардировщика
SolidBrush bodyBrush = new SolidBrush(EntityWarAirplane.BodyColor);
Rectangle bodyAirBomber = new Rectangle((int)_startPosX + 30, (int)_startPosY + 40, 50, 10);
g.FillRectangle(bodyBrush, bodyAirBomber);
g.DrawRectangle(pen, bodyAirBomber);
//бомбы
PointF point17 = new PointF(_startPosX + 40, _startPosY + 40);
PointF point18 = new PointF(_startPosX + 35, _startPosY + 35);
PointF point19 = new PointF(_startPosX + 40, _startPosY + 30);
PointF point20 = new PointF(_startPosX + 50, _startPosY + 40);
PointF[] bomb1 =
{
point17,
point18,
point19,
point20
};
Brush brBomb = new SolidBrush(Color.Gray);
g.FillPolygon(brBomb, bomb1);
g.DrawPolygon(pen, bomb1);
PointF point21 = new PointF(_startPosX + 40, _startPosY + 50);
PointF point22 = new PointF(_startPosX + 35, _startPosY + 55);
PointF point23 = new PointF(_startPosX + 40, _startPosY + 60);
PointF point24 = new PointF(_startPosX + 50, _startPosY + 50);
PointF[] bomb2 =
{
point21,
point22,
point23,
point24
};
g.FillPolygon(brBomb, bomb2);
g.DrawPolygon(pen, bomb2);
}
}
}

View File

@ -1,24 +1,19 @@
using System;
using AirBomber.Entities;
using System;
namespace AirBomber
namespace AirBomber.Entities
{
internal class EntityAirBomber
public class EntityAirBomber : EntityWarAirplane
{
public int Speed { get; private set; }
public int NumEngine { get; private set; }
public float Weight { get; private set; }
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
public float Step => Speed * 100 / Weight;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, bool fuelTank,
bool bombs, int numEngine)
public bool FuelTank { get; private set; }
public bool Bombs { get; private set; }
public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, bool fuelTank,
bool bombs) : base(speed, weight, bodyColor)
{
Random rnd = new();
NumEngine = numEngine <= 0 ? rnd.Next(1, 4): numEngine;
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
FuelTank = fuelTank;
Bombs = bombs;
}
}
}

View File

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

View File

@ -29,11 +29,14 @@
private void InitializeComponent()
{
pictureBoxAirBomber = new PictureBox();
buttonCreate = new Button();
buttonCreateWarAirplane = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonLeft = new Button();
buttonRight = new Button();
buttonCreateAirBomber = new Button();
buttonStep = new Button();
comboBoxStrategy = new ComboBox();
((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit();
SuspendLayout();
//
@ -47,15 +50,15 @@
pictureBoxAirBomber.TabIndex = 0;
pictureBoxAirBomber.TabStop = false;
//
// buttonCreate
// buttonCreateWarAirplane
//
buttonCreate.Location = new Point(12, 394);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(156, 29);
buttonCreate.TabIndex = 1;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += ButtonCreate_Click;
buttonCreateWarAirplane.Location = new Point(12, 394);
buttonCreateWarAirplane.Name = "buttonCreateWarAirplane";
buttonCreateWarAirplane.Size = new Size(156, 41);
buttonCreateWarAirplane.TabIndex = 1;
buttonCreateWarAirplane.Text = "Создать Военный самолёт";
buttonCreateWarAirplane.UseVisualStyleBackColor = true;
buttonCreateWarAirplane.Click += ButtonCreateWarAirplane_Click;
//
// buttonUp
//
@ -105,16 +108,49 @@
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click;
//
// buttonCreateAirBomber
//
buttonCreateAirBomber.Location = new Point(193, 394);
buttonCreateAirBomber.Name = "buttonCreateAirBomber";
buttonCreateAirBomber.Size = new Size(156, 41);
buttonCreateAirBomber.TabIndex = 6;
buttonCreateAirBomber.Text = "Создать Бомбардировщик";
buttonCreateAirBomber.UseVisualStyleBackColor = true;
buttonCreateAirBomber.Click += ButtonCreateAirBomber_Click;
//
// buttonStep
//
buttonStep.Location = new Point(774, 74);
buttonStep.Name = "buttonStep";
buttonStep.Size = new Size(78, 29);
buttonStep.TabIndex = 7;
buttonStep.Text = "Шаг";
buttonStep.UseVisualStyleBackColor = true;
buttonStep.Click += buttonStep_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder" });
comboBoxStrategy.Location = new Point(696, 30);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(156, 23);
comboBoxStrategy.TabIndex = 8;
//
// FormAirBomber
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonStep);
Controls.Add(buttonCreateAirBomber);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonCreate);
Controls.Add(buttonCreateWarAirplane);
Controls.Add(pictureBoxAirBomber);
Name = "FormAirBomber";
StartPosition = FormStartPosition.CenterScreen;
@ -127,10 +163,13 @@
#endregion
private PictureBox pictureBoxAirBomber;
private Button buttonCreate;
private Button buttonCreateWarAirplane;
private Button buttonUp;
private Button buttonDown;
private Button buttonLeft;
private Button buttonRight;
private Button buttonCreateAirBomber;
private Button buttonStep;
private ComboBox comboBoxStrategy;
}
}

View File

@ -1,8 +1,13 @@
using AirBomber.DrawingObjects;
using AirBomber.MovementStrategy;
using System.Windows.Forms;
namespace AirBomber
{
public partial class FormAirBomber : Form
{
private DrawingAirBomber _AirBomber;
private DrawingWarAirplane? _drawingWarAirplane;
private AbstractStrategy? _abstractStrategy;
public FormAirBomber()
{
InitializeComponent();
@ -10,48 +15,96 @@ namespace AirBomber
private void Draw()
{
if(_drawingWarAirplane == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Graphics gr = Graphics.FromImage(bmp);
_AirBomber?.DrawTransport(gr);
_drawingWarAirplane.DrawTransport(gr);
pictureBoxAirBomber.Image = bmp;
}
private void ButtonCreate_Click(object sender, EventArgs e)
private void ButtonCreateAirBomber_Click(object sender, EventArgs e)
{
Random rnd = new();
_AirBomber = new DrawingAirBomber();
_AirBomber.Init(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), true, true, rnd.Next(1,4));
_AirBomber.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawingWarAirplane = new DrawingAirBomber(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void ButtonCreateWarAirplane_Click(object sender, EventArgs e)
{
Random rnd = new();
_drawingWarAirplane = new DrawingWarAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
_drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawingWarAirplane == null)
{
return;
}
//äâèæåíèå
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_AirBomber?.MoveTransport(DirectionType.Up);
_drawingWarAirplane?.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_AirBomber?.MoveTransport(DirectionType.Down);
_drawingWarAirplane?.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_AirBomber?.MoveTransport(DirectionType.Left);
_drawingWarAirplane?.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_AirBomber?.MoveTransport(DirectionType.Right);
_drawingWarAirplane?.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
private void PictureBoxAirBomber_Resize(object sender, EventArgs e)
private void buttonStep_Click(object sender, EventArgs e)
{
_AirBomber?.ChangeBorders(pictureBoxAirBomber.Width, pictureBoxAirBomber.Height);
Draw();
if (_drawingWarAirplane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawingObjectWarAirplane(_drawingWarAirplane), pictureBoxAirBomber.Width,
pictureBoxAirBomber.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}

View File

@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>

View File

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

View File

@ -0,0 +1,60 @@
using AirBomber.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber.MovementStrategy
{
internal class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var 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()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

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

View File

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