PIbd-23. Kislitsa E.D. Lab work 02 #2 #2

Closed
Egor wants to merge 2 commits from Laba2 into Laba1
14 changed files with 686 additions and 338 deletions

View File

@ -0,0 +1,121 @@

using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ProjectAirFighter.MovementStrategy
{
/// <summary>
/// Класс-стратегия перемещения объекта
/// </summary>
public abstract class AbstractStrategy
{
/// <summary>
/// Перемещаемый объект
/// </summary>
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
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;
}
/// <summary>
/// Шаг перемещения
/// </summary>
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);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
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

@ -1,156 +1,31 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirFighter.Entities;
namespace ProjectAirFighter
namespace ProjectAirFighter.DrawningObjects
{
public class DrawningAirFighter
public class DrawningAirFighter : DrawningAirplane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAirFighter? EntityAirFighter { get; private set; }
/// <summary>
/// </summary>
private int _pictureWidth;
/// <summary>
/// </summary>
private int _pictureHeight;
/// <summary>
/// </summary>
private int _startPosX;
/// <summary>
/// </summary>
private int _startPosY;
/// <summary>
/// </summary>
private readonly int _airfighterWidth = 163;
/// <summary>
/// </summary>
private readonly int _airfighterHeight = 70;
private readonly int _airfighterwingkorpusHeight = 90;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor"></param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="racket"
/// <param name="wing"
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена,
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool racket, bool wing, int width, int height)
public DrawningAirFighter(int speed, double weight, Color bodyColor, Color
additionalColor, bool racket, bool wing, int width, int height) :
base(speed, weight, bodyColor, width, height, 160, 160)
{
// TODO: Продумать проверки
if (width <= _airfighterWidth || height <= _airfighterHeight)
return false;
_pictureWidth = width;
_pictureHeight = height;
EntityAirFighter = new EntityAirFighter();
EntityAirFighter.Init(speed, weight, bodyColor, additionalColor,
racket, wing);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
// TODO: Изменение x, y
_startPosX = x;
_startPosY = y;
if (x + _airfighterWidth >= _pictureWidth || y + _airfighterHeight >= _pictureHeight)
if (EntityAirplane != null)
{
_startPosX = 1;
_startPosY = (_airfighterHeight+_airfighterwingkorpusHeight)/2;
EntityAirplane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, racket, wing);
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
public override void DrawTransport(Graphics g)
{
if (EntityAirFighter == null)
if (EntityAirplane is not EntityAirFighter airFighter)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityAirFighter.Step > 0)
{
_startPosX -= (int)EntityAirFighter.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - _airfighterHeight - EntityAirFighter.Step > 0)
{
_startPosY -= (int)EntityAirFighter.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + EntityAirFighter.Step + _airfighterWidth < _pictureWidth)
{
_startPosX += (int)EntityAirFighter.Step;
}
else
_startPosX = _pictureWidth - _airfighterWidth;
break;
//вниз
case DirectionType.Down:
if (_startPosY + EntityAirFighter.Step + _airfighterwingkorpusHeight < _pictureHeight)
{
_startPosY += (int)EntityAirFighter.Step;
}
else
_startPosY = _pictureHeight - _airfighterwingkorpusHeight;
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityAirFighter == null)
{
return;
}
Brush additionalBrush = new SolidBrush(airFighter.AdditionalColor);
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(EntityAirFighter.AdditionalColor);
// ракеты
if (EntityAirFighter.Racket)
base.DrawTransport(g);
if (airFighter.Racket)
{
Brush brGrey = new SolidBrush(Color.LightGray);
g.FillRectangle(brGrey, _startPosX + 70, _startPosY - 15, 10, 10);
g.DrawRectangle(pen, _startPosX + 70, _startPosY - 15, 10, 10);
@ -172,7 +47,6 @@ namespace ProjectAirFighter
new Point(_startPosX + 70, _startPosY - 40),
new Point(_startPosX + 60,_startPosY -35)
};
g.FillPolygon(brRed, noseracketPoints2);
g.DrawPolygon(pen, noseracketPoints2);
g.FillPolygon(brRed, noseracketPoints);
@ -186,11 +60,9 @@ namespace ProjectAirFighter
new Point(_startPosX + 70, _startPosY + 69),
new Point(_startPosX + 60,_startPosY + 64)
};
g.FillPolygon(brRed, noseracketPoints3);
g.DrawPolygon(pen, noseracketPoints3);
g.FillRectangle(brGrey, _startPosX + 70, _startPosY + 34, 10, 10);
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 34, 10, 10);
Point[] noseracketPoints4 =
@ -199,88 +71,10 @@ namespace ProjectAirFighter
new Point(_startPosX + 70, _startPosY + 44),
new Point(_startPosX + 60,_startPosY + 39)
};
g.FillPolygon(brRed, noseracketPoints4);
g.DrawPolygon(pen, noseracketPoints4);
}
Point[] nosePoints =
{
new Point(_startPosX + 20, _startPosY + 4),
new Point(_startPosX + 20, _startPosY + 24),
new Point(_startPosX-3,_startPosY + 12)
};
Brush brBlack = new SolidBrush(Color.Black);
g.FillPolygon(brBlack, nosePoints);
g.DrawPolygon(pen, nosePoints);
Point[] rightwingPoints =
{
new Point(_startPosX + 80, _startPosY + 4),
new Point(_startPosX+80,_startPosY - 66),
new Point(_startPosX+85,_startPosY - 66),
new Point(_startPosX + 100, _startPosY + 4)
};
g.FillPolygon(additionalBrush, rightwingPoints);
g.DrawPolygon(pen, rightwingPoints);
Point[] lefttwingPoints =
{
new Point(_startPosX + 80, _startPosY + 24),
new Point(_startPosX + 100, _startPosY + 24),
new Point(_startPosX+85,_startPosY + 94),
new Point(_startPosX+80,_startPosY + 94)
};
g.FillPolygon(additionalBrush, lefttwingPoints);
g.DrawPolygon(pen, lefttwingPoints);
Point[] leftenginePoints =
{
new Point(_startPosX + 140, _startPosY + 24),
new Point(_startPosX + 160, _startPosY + 24),
new Point(_startPosX+160,_startPosY + 50),
new Point(_startPosX+140,_startPosY + 32)
};
g.FillPolygon(additionalBrush, leftenginePoints);
g.DrawPolygon(pen, leftenginePoints);
Point[] rightenginePoints =
{
new Point(_startPosX + 140, _startPosY + 24),
new Point(_startPosX + 160, _startPosY + 24),
new Point(_startPosX+160,_startPosY - 16),
new Point(_startPosX+140,_startPosY -4)
};
g.FillPolygon(additionalBrush, rightenginePoints);
g.DrawPolygon(pen, rightenginePoints);
g.FillRectangle(additionalBrush, _startPosX + 20, _startPosY + 4, 140, 20);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 140, 20);
// крыло
if (EntityAirFighter.Wing)
if (airFighter.Wing)
{
Point[] doprightwingPoints =
{
@ -289,8 +83,6 @@ namespace ProjectAirFighter
new Point(_startPosX+35,_startPosY - 34),
new Point(_startPosX + 45, _startPosY + 4)
};
g.FillPolygon(additionalBrush, doprightwingPoints);
g.DrawPolygon(pen, doprightwingPoints);
@ -305,9 +97,7 @@ namespace ProjectAirFighter
};
g.FillPolygon(additionalBrush, doplefttwingPoints);
g.DrawPolygon(pen, doplefttwingPoints);
}
}
}
}
}

View File

@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirFighter.Entities;
namespace ProjectAirFighter.DrawningObjects
{
public class DrawningAirplane
{
public EntityAirplane? EntityAirplane { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _airplaneWidth = 163;
protected readonly int _airplaneHeight = 160;
protected readonly int _airplanewingHeight = 70;
protected readonly int _airplanerwingkorpusHeight = 90;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _airplaneWidth;
public int GetHeight => _airplaneHeight;
public DrawningAirplane(int speed, double weight, Color bodyColor,int width, int height)
{
if (width <= _airplaneWidth || height <= _airplanewingHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
protected DrawningAirplane(int speed, double weight, Color bodyColor, int
width, int height, int airplaneWidth, int airplaneHeight)
{
if (width <= _airplaneWidth || height <= _airplanewingHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
_airplaneWidth = airplaneWidth;
_airplanewingHeight = airplaneHeight;
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (EntityAirplane == null)
return;
_startPosX = x;
_startPosY = y;
if (x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight)
{
_startPosX = 1;
_startPosY = (_airplanewingHeight+_airplanerwingkorpusHeight)/2;
}
}
public bool CanMove(DirectionType direction)
{
if (EntityAirplane == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityAirplane.Step > 0,
DirectionType.Up => _startPosY - EntityAirplane.Step - (_airplaneHeight - _airplaneHeight * 125 / 1000) / 2 > 0,
DirectionType.Right => _startPosX+ EntityAirplane.Step + _airplaneWidth < _pictureWidth,
DirectionType.Down => _startPosY + EntityAirplane.Step + _airplanerwingkorpusHeight < _pictureHeight,
};
}
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityAirplane == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
_startPosX -= (int)EntityAirplane.Step;
break;
case DirectionType.Up:
_startPosY -= (int)EntityAirplane.Step;
break;
case DirectionType.Right:
_startPosX += (int)EntityAirplane.Step;
break;
case DirectionType.Down:
_startPosY += (int)EntityAirplane.Step;
break;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityAirplane == null)
{
return;
}
Pen pen = new(Color.Black);
Brush br = new SolidBrush(EntityAirplane.BodyColor);
Point[] nosePoints =
{
new Point(_startPosX + 20, _startPosY + 4),
new Point(_startPosX + 20, _startPosY + 24),
new Point(_startPosX-3,_startPosY + 12)
};
Brush brBlack = new SolidBrush(Color.Black);
g.FillPolygon(brBlack, nosePoints);
g.DrawPolygon(pen, nosePoints);
Point[] rightwingPoints =
{
new Point(_startPosX + 80, _startPosY + 4),
new Point(_startPosX+80,_startPosY - 64),
new Point(_startPosX+85,_startPosY - 64),
new Point(_startPosX + 100, _startPosY + 4)
};
g.DrawPolygon(pen, rightwingPoints);
g.FillPolygon(br, rightwingPoints);
Point[] lefttwingPoints =
{
new Point(_startPosX + 80, _startPosY + 24),
new Point(_startPosX + 100, _startPosY + 24),
new Point(_startPosX+85,_startPosY + 94),
new Point(_startPosX+80,_startPosY + 94)
};
g.DrawPolygon(pen, lefttwingPoints);
g.FillPolygon(br, lefttwingPoints);
Point[] leftenginePoints =
{
new Point(_startPosX + 140, _startPosY + 24),
new Point(_startPosX + 160, _startPosY + 24),
new Point(_startPosX+160,_startPosY + 50),
new Point(_startPosX+140,_startPosY + 32)
};
g.DrawPolygon(pen, leftenginePoints);
g.FillPolygon(br, leftenginePoints);
Point[] rightenginePoints =
{
new Point(_startPosX + 140, _startPosY + 24),
new Point(_startPosX + 160, _startPosY + 24),
new Point(_startPosX+160,_startPosY - 16),
new Point(_startPosX+140,_startPosY -4)
};
g.DrawPolygon(pen, rightenginePoints);
g.FillPolygon(br, rightenginePoints);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 140, _airplaneHeight * 125 / 1000);
g.FillRectangle(br, _startPosX + 20, _startPosY + 4, 140, _airplaneHeight * 125 / 1000);
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectAirFighter.DrawningObjects;
namespace ProjectAirFighter.MovementStrategy
{
public class DrawningObjectAirplane: IMoveableObject
{
private readonly DrawningAirplane? _drawningAirplane = null;
public DrawningObjectAirplane(DrawningAirplane drawningAirplane)
{
_drawningAirplane = drawningAirplane;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningAirplane == null || _drawningAirplane.EntityAirplane ==
null)
{
return null;
}
return new ObjectParameters(_drawningAirplane.GetPosX,
_drawningAirplane.GetPosY, _drawningAirplane.GetWidth, _drawningAirplane.GetHeight);
}
}
public int GetStep => (int)(_drawningAirplane?.EntityAirplane?.Step ?? 0);
public bool CheckCanMove(DirectionType direction) =>
_drawningAirplane?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningAirplane?.MoveTransport(direction);
}
}

View File

@ -4,63 +4,21 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirFighter
namespace ProjectAirFighter.Entities
{
public class EntityAirFighter
public class EntityAirFighter: EntityAirplane
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public double Weight { get; private set; }
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Дополнительный цвет (для опциональных элементов)
/// </summary>
public Color AdditionalColor { get; private set; }
/// <summary>
/// Признак (опция) наличия обвеса
/// </summary>
public bool Racket { get; private set; }
/// <summary>
/// Признак (опция) наличия антикрыла
/// </summary>
public bool Wing { get; private set; }
/// <summary>
/// Признак (опция) наличия гоночной полосы
/// </summary>
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public double Step => (double)Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса спортивного автомобиля
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="racket">Признак наличия обвеса</param>
/// <param name="wing"
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool racket, bool wing)
public EntityAirFighter(int speed, double weight, Color bodyColor, Color additionalColor, bool racket, bool wing):
base(speed,weight,bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Racket = racket;
Wing = wing;
}
}
}

View File

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

View File

@ -2,15 +2,8 @@
{
partial class FormAirFighter
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
@ -19,7 +12,6 @@
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
@ -34,6 +26,9 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.ButtonCreateAirplane = new System.Windows.Forms.Button();
this.ButtonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirFighter)).BeginInit();
this.SuspendLayout();
//
@ -50,11 +45,11 @@
// ButtonCreateAirFighter
//
this.ButtonCreateAirFighter.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateAirFighter.Location = new System.Drawing.Point(0, 424);
this.ButtonCreateAirFighter.Location = new System.Drawing.Point(0, 405);
this.ButtonCreateAirFighter.Name = "ButtonCreateAirFighter";
this.ButtonCreateAirFighter.Size = new System.Drawing.Size(94, 29);
this.ButtonCreateAirFighter.Size = new System.Drawing.Size(144, 48);
this.ButtonCreateAirFighter.TabIndex = 1;
this.ButtonCreateAirFighter.Text = "Создать";
this.ButtonCreateAirFighter.Text = "Создать военный самолёт";
this.ButtonCreateAirFighter.UseVisualStyleBackColor = true;
this.ButtonCreateAirFighter.Click += new System.EventHandler(this.ButtonCreateAirFighter_Click);
//
@ -106,11 +101,47 @@
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// comboBoxStrategy
//
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"0",
"1"});
this.comboBoxStrategy.Location = new System.Drawing.Point(731, 0);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28);
this.comboBoxStrategy.TabIndex = 6;
//
// ButtonCreateAirplane
//
this.ButtonCreateAirplane.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateAirplane.Location = new System.Drawing.Point(150, 405);
this.ButtonCreateAirplane.Name = "ButtonCreateAirplane";
this.ButtonCreateAirplane.Size = new System.Drawing.Size(143, 47);
this.ButtonCreateAirplane.TabIndex = 7;
this.ButtonCreateAirplane.Text = "Создать самолёт";
this.ButtonCreateAirplane.UseVisualStyleBackColor = true;
this.ButtonCreateAirplane.Click += new System.EventHandler(this.ButtonCreateAirplane_Click);
//
// ButtonStep
//
this.ButtonStep.Location = new System.Drawing.Point(788, 34);
this.ButtonStep.Name = "ButtonStep";
this.ButtonStep.Size = new System.Drawing.Size(94, 29);
this.ButtonStep.TabIndex = 8;
this.ButtonStep.Text = "Шаг";
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// FormAirFighter
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(882, 453);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.ButtonCreateAirplane);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -127,12 +158,14 @@
}
#endregion
private PictureBox pictureBoxAirFighter;
private Button ButtonCreateAirFighter;
private Button buttonUp;
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private ComboBox comboBoxStrategy;
private Button ButtonCreateAirplane;
private Button ButtonStep;
}
}

View File

@ -1,84 +1,118 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectAirFighter.MovementStrategy;
using ProjectAirFighter.DrawningObjects;
namespace ProjectAirFighter
{
public partial class FormAirFighter : Form
{
private DrawningAirFighter? _drawningAirFighter;
private DrawningAirplane? _drawningAirplane;
private AbstractStrategy? _abstractStrategy;
public FormAirFighter()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningAirFighter == null)
if (_drawningAirplane == null)
{
return;
}
Bitmap bmp = new(pictureBoxAirFighter.Width,
pictureBoxAirFighter.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningAirFighter.DrawTransport(gr);
_drawningAirplane.DrawTransport(gr);
pictureBoxAirFighter.Image = bmp;
}
private void ButtonCreateAirplane_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirplane = new DrawningAirplane(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100));
Draw();
}
private void ButtonCreateAirFighter_Click(object sender, EventArgs e)
{
Random random = new();
_drawningAirFighter = new DrawningAirFighter();
_drawningAirFighter.Init(random.Next(100, 300),
_drawningAirplane = new DrawningAirFighter (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)),
pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningAirFighter.SetPosition(random.Next(10, 100),
random.Next(70, 100));
_drawningAirplane.SetPosition(random.Next(10, 100), random.Next(70, 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningAirFighter == null)
private void buttonMove_Click(object sender, EventArgs e)
{
return;
if (_drawningAirplane == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningAirplane.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningAirplane.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningAirplane.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningAirplane.MoveTransport(DirectionType.Right);
break;
}
Draw();
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
private void ButtonStep_Click(object sender, EventArgs e)
{
case "buttonUp":
_drawningAirFighter.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
_drawningAirFighter.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
_drawningAirFighter.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
_drawningAirFighter.MoveTransport(DirectionType.Right);
break;
if (_drawningAirplane == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectAirplane(_drawningAirplane), pictureBoxAirFighter.Width,
pictureBoxAirFighter.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
Draw();
}
}
}

View File

@ -0,0 +1,28 @@
namespace ProjectAirFighter.MovementStrategy
{
/// <summary>
/// Интерфейс для работы с перемещаемым объектом
/// </summary>
public interface IMoveableObject
{
/// <summary>
/// Получение координаты X объекта
/// </summary>
ObjectParameters? GetObjectPosition { get; }
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep { get; }
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
bool CheckCanMove(DirectionType direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(DirectionType direction);
}
}

View File

@ -0,0 +1,50 @@
using ProjectAirFighter.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirFighter.MovementStrategy
{
public 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 + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - (FieldWidth - 1);
if (Math.Abs(diffX) > GetStep())
{
if (diffX < 0)
{
MoveRight();
}
}
var diffY = objParams.DownBorder - (FieldHeight - 1);
if (Math.Abs(diffY) > GetStep())
{
if (diffY < 0)
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,51 @@
namespace ProjectAirFighter.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,31 @@
namespace ProjectAirFighter.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 * 125 / 1000 + (_height-_height * 125 / 1000)/2 ;
public int ObjectMiddleHorizontal => _x + _width / 2;
public int ObjectMiddleVertical => _y +(_height - _height * 125 / 1000) / 2 / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

View File

@ -4,18 +4,12 @@ namespace ProjectAirFighter
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI
ApplicationConfiguration.Initialize();
Application.Run(new FormAirFighter());
}
}
}

12
AirFighter/Status.cs Normal file
View File

@ -0,0 +1,12 @@
namespace ProjectAirFighter.MovementStrategy
{
/// <summary>
/// Статус выполнения операции перемещения
/// </summary>
public enum Status
{
NotInit,
InProgress,
Finish
}
}