ИСЭбд-22 Семеленова Юлия лаб_2 простая #4
76
AirFighter/AirFighter/AbstractStrategy.cs
Normal file
76
AirFighter/AirFighter/AbstractStrategy.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using AirFighter.Drawnings;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace AirFighter.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(DirectionAirFighter.Left);
|
||||
protected bool MoveRight() => MoveTo(DirectionAirFighter.Right);
|
||||
protected bool MoveUp() => MoveTo(DirectionAirFighter.Up);
|
||||
protected bool MoveDown() => MoveTo(DirectionAirFighter.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(DirectionAirFighter directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject?.CheckCanMove(directionType) ?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter
|
||||
namespace AirFighter.Drawnings
|
||||
{
|
||||
public enum DirectionAirFighter
|
||||
{
|
||||
|
@ -6,37 +6,48 @@ using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AirFighter.Entities;
|
||||
using AirFighter.Drawnings;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace AirFighter
|
||||
namespace AirFighter.DrawningObjects
|
||||
{
|
||||
public class DrawningAirFighter
|
||||
{
|
||||
public EntityAirFighter? EntityAirFighter { get; private set; }
|
||||
public EntityAirFighter? EntityAirFighter { get; protected set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _startPosX;
|
||||
|
||||
protected int _startPosX;
|
||||
|
||||
protected int _startPosY;
|
||||
|
||||
protected readonly int _fighterWidth = 195;
|
||||
|
||||
protected readonly int _fighterHeight = 166;
|
||||
|
||||
private int _startPosY;
|
||||
|
||||
private readonly int _fighterWidth = 195;
|
||||
|
||||
private readonly int _fighterHeight = 166;
|
||||
|
||||
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool wing, bool rocket, int width, int height)
|
||||
public DrawningAirFighter(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _pictureWidth || height < _pictureHeight)
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityAirFighter = new EntityAirFighter(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
protected DrawningAirFighter(int speed, double weight, Color bodyColor, int
|
||||
width, int height, int fighterWidth, int fighterHeight)
|
||||
{
|
||||
if (width <= _pictureWidth || height <= _pictureHeight)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityAirFighter = new EntityAirFighter();
|
||||
EntityAirFighter.Init(speed, weight, bodyColor, additionalColor, wing, rocket);
|
||||
return true;
|
||||
}
|
||||
_fighterWidth = fighterWidth;
|
||||
_fighterHeight = fighterHeight;
|
||||
EntityAirFighter = new EntityAirFighter(speed, weight, bodyColor);
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0)
|
||||
@ -57,62 +68,60 @@ namespace AirFighter
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void MoveTransport(DirectionAirFighter direction)
|
||||
}
|
||||
protected int PictureWidth
|
||||
{
|
||||
get { return _pictureWidth; }
|
||||
}
|
||||
protected int PictureHeight
|
||||
{
|
||||
get { return _pictureHeight; }
|
||||
}
|
||||
public int GetPosX => _startPosX;
|
||||
public int GetPosY => _startPosY;
|
||||
public int GetWidth => _fighterWidth;
|
||||
public int GetHeight => _fighterHeight;
|
||||
public virtual bool CanMove(DirectionAirFighter direction)
|
||||
{
|
||||
if (EntityAirFighter == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return direction switch
|
||||
{
|
||||
DirectionAirFighter.Left => _startPosX - EntityAirFighter.Step > 0,
|
||||
|
||||
DirectionAirFighter.Up => _startPosY - EntityAirFighter.Step > 7,
|
||||
|
||||
DirectionAirFighter.Right => _startPosX + EntityAirFighter.Step + _fighterWidth < _pictureWidth,// TODO: Продумать логику
|
||||
|
||||
DirectionAirFighter.Down => _startPosY + EntityAirFighter.Step + _fighterHeight < _pictureHeight,// TODO: Продумать логику
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
public virtual void MoveTransport(DirectionAirFighter direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityAirFighter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case DirectionAirFighter.Left:
|
||||
if (_startPosX - EntityAirFighter.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityAirFighter.Step;
|
||||
}
|
||||
else if (_startPosX - EntityAirFighter.Step < 0)
|
||||
{
|
||||
_startPosX = _startPosX - _startPosX + 3;
|
||||
}
|
||||
_startPosX -= (int)EntityAirFighter.Step;
|
||||
break;
|
||||
|
||||
case DirectionAirFighter.Up:
|
||||
if (_startPosY - EntityAirFighter.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityAirFighter.Step;
|
||||
}
|
||||
else if (_startPosY - EntityAirFighter.Step < 0)
|
||||
{
|
||||
_startPosY = _startPosY - _startPosY + 0;
|
||||
}
|
||||
_startPosY -= (int)EntityAirFighter.Step;
|
||||
break;
|
||||
|
||||
case DirectionAirFighter.Right:
|
||||
if (_startPosX + EntityAirFighter.Step + _fighterWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityAirFighter.Step;
|
||||
}
|
||||
else if (_startPosX + EntityAirFighter.Step + _fighterWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX += _pictureWidth - _startPosX - _fighterWidth;
|
||||
}
|
||||
|
||||
_startPosX += (int)EntityAirFighter.Step;
|
||||
break;
|
||||
|
||||
case DirectionAirFighter.Down:
|
||||
if (_startPosY + EntityAirFighter.Step + _fighterHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityAirFighter.Step;
|
||||
}
|
||||
else if (_startPosY + EntityAirFighter.Step + _fighterHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY += _pictureHeight - _startPosY - _fighterHeight;
|
||||
}
|
||||
_startPosY += (int)EntityAirFighter.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAirFighter == null)
|
||||
{
|
||||
@ -162,78 +171,8 @@ namespace AirFighter
|
||||
g.DrawPolygon(pen, wingBottom);
|
||||
g.DrawRectangle(pen, _startPosX, _startPosY + 70, 160, 26);
|
||||
g.FillPolygon(brushBlack, front);
|
||||
|
||||
Brush additionalBrush = new SolidBrush(EntityAirFighter.AdditionalColor);
|
||||
|
||||
if (EntityAirFighter.Wing)
|
||||
{
|
||||
PointF[] topDopWing =
|
||||
{
|
||||
new(_startPosX + 78, _startPosY + 56),
|
||||
new(_startPosX + 75, _startPosY + 70),
|
||||
new(_startPosX + 55, _startPosY + 50),
|
||||
new(_startPosX + 60, _startPosY + 45),
|
||||
};
|
||||
|
||||
PointF[] bottomDopWing =
|
||||
{
|
||||
new(_startPosX + 78, _startPosY + 110),
|
||||
new(_startPosX + 75, _startPosY + 96),
|
||||
new(_startPosX + 55, _startPosY + 116),
|
||||
new(_startPosX + 60, _startPosY + 121),
|
||||
};
|
||||
|
||||
g.FillPolygon(additionalBrush, topDopWing);
|
||||
g.FillPolygon(additionalBrush, bottomDopWing);
|
||||
|
||||
}
|
||||
|
||||
if (EntityAirFighter.Rocket)
|
||||
{
|
||||
PointF[] topRocket1 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 20),
|
||||
new(_startPosX + 100, _startPosY + 30),
|
||||
new(_startPosX + 112, _startPosY + 30),
|
||||
new(_startPosX + 120, _startPosY + 25),
|
||||
new(_startPosX + 112, _startPosY + 20)
|
||||
};
|
||||
|
||||
PointF[] topRocket2 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 35),
|
||||
new(_startPosX + 100, _startPosY + 45),
|
||||
new(_startPosX + 112, _startPosY + 45),
|
||||
new(_startPosX + 120, _startPosY + 40),
|
||||
new(_startPosX + 112, _startPosY + 35)
|
||||
};
|
||||
|
||||
PointF[] bottomRocket1 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 146),
|
||||
new(_startPosX + 100, _startPosY + 136),
|
||||
new(_startPosX + 112, _startPosY + 136),
|
||||
new(_startPosX + 120, _startPosY + 141),
|
||||
new(_startPosX + 112, _startPosY + 146)
|
||||
};
|
||||
|
||||
PointF[] bottomRocket2 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 131),
|
||||
new(_startPosX + 100, _startPosY + 121),
|
||||
new(_startPosX + 112, _startPosY + 121),
|
||||
new(_startPosX + 120, _startPosY + 126),
|
||||
new(_startPosX + 112, _startPosY + 131)
|
||||
};
|
||||
|
||||
g.FillPolygon(additionalBrush, topRocket1);
|
||||
g.FillPolygon(additionalBrush, topRocket2);
|
||||
g.FillPolygon(additionalBrush, bottomRocket1);
|
||||
g.FillPolygon(additionalBrush, bottomRocket2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
104
AirFighter/AirFighter/DrawningAirFighterMilitary.cs
Normal file
104
AirFighter/AirFighter/DrawningAirFighterMilitary.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using AirFighter.Drawnings;
|
||||
using AirFighter.Entities;
|
||||
|
||||
|
||||
|
||||
namespace AirFighter.DrawningObjects
|
||||
{
|
||||
public class DrawningAirFighterMilitary : DrawningAirFighter
|
||||
|
||||
{
|
||||
public DrawningAirFighterMilitary(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing, int width, int height) :
|
||||
base(speed, weight, bodyColor, width, height, 195, 166)
|
||||
{
|
||||
if (EntityAirFighter != null)
|
||||
{
|
||||
EntityAirFighter = new EntityAirFighterMilitary(speed, weight, bodyColor,
|
||||
additionalColor, rocket, wing);
|
||||
}
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityAirFighter is not EntityAirFighterMilitary fighterMilitary)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Brush additionalBrush = new SolidBrush(fighterMilitary.AdditionalColor);
|
||||
|
||||
if (fighterMilitary.Wing)
|
||||
{
|
||||
PointF[] topDopWing =
|
||||
{
|
||||
new(_startPosX + 78, _startPosY + 56),
|
||||
new(_startPosX + 75, _startPosY + 70),
|
||||
new(_startPosX + 55, _startPosY + 50),
|
||||
new(_startPosX + 60, _startPosY + 45),
|
||||
};
|
||||
|
||||
PointF[] bottomDopWing =
|
||||
{
|
||||
new(_startPosX + 78, _startPosY + 110),
|
||||
new(_startPosX + 75, _startPosY + 96),
|
||||
new(_startPosX + 55, _startPosY + 116),
|
||||
new(_startPosX + 60, _startPosY + 121),
|
||||
};
|
||||
|
||||
g.FillPolygon(additionalBrush, topDopWing);
|
||||
g.FillPolygon(additionalBrush, bottomDopWing);
|
||||
|
||||
}
|
||||
base.DrawTransport(g);
|
||||
|
||||
if (fighterMilitary.Rocket)
|
||||
{
|
||||
PointF[] topRocket1 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 20),
|
||||
new(_startPosX + 100, _startPosY + 30),
|
||||
new(_startPosX + 112, _startPosY + 30),
|
||||
new(_startPosX + 120, _startPosY + 25),
|
||||
new(_startPosX + 112, _startPosY + 20)
|
||||
};
|
||||
|
||||
PointF[] topRocket2 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 35),
|
||||
new(_startPosX + 100, _startPosY + 45),
|
||||
new(_startPosX + 112, _startPosY + 45),
|
||||
new(_startPosX + 120, _startPosY + 40),
|
||||
new(_startPosX + 112, _startPosY + 35)
|
||||
};
|
||||
|
||||
PointF[] bottomRocket1 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 146),
|
||||
new(_startPosX + 100, _startPosY + 136),
|
||||
new(_startPosX + 112, _startPosY + 136),
|
||||
new(_startPosX + 120, _startPosY + 141),
|
||||
new(_startPosX + 112, _startPosY + 146)
|
||||
};
|
||||
|
||||
PointF[] bottomRocket2 =
|
||||
{
|
||||
new(_startPosX + 100, _startPosY + 131),
|
||||
new(_startPosX + 100, _startPosY + 121),
|
||||
new(_startPosX + 112, _startPosY + 121),
|
||||
new(_startPosX + 120, _startPosY + 126),
|
||||
new(_startPosX + 112, _startPosY + 131)
|
||||
};
|
||||
|
||||
g.FillPolygon(additionalBrush, topRocket1);
|
||||
g.FillPolygon(additionalBrush, topRocket2);
|
||||
g.FillPolygon(additionalBrush, bottomRocket1);
|
||||
g.FillPolygon(additionalBrush, bottomRocket2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -5,28 +5,19 @@ using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter
|
||||
namespace AirFighter.Entities
|
||||
{
|
||||
public class EntityAirFighter
|
||||
{
|
||||
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 Rocket { get; private set; }
|
||||
public bool Wing { get; private set; }
|
||||
public double Step => (double)Speed * 100 / Weight;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing)
|
||||
{
|
||||
|
||||
public EntityAirFighter(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Rocket = rocket;
|
||||
Wing = wing;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
22
AirFighter/AirFighter/EntityAirFighterMilitary.cs
Normal file
22
AirFighter/AirFighter/EntityAirFighterMilitary.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter.Entities
|
||||
{
|
||||
public class EntityAirFighterMilitary : EntityAirFighter
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
public bool Rocket { get; private set; }
|
||||
public bool Wing { get; private set; }
|
||||
public EntityAirFighterMilitary(int speed, double weight, Color bodyColor, Color additionalColor, bool rocket, bool wing)
|
||||
: base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
Rocket = rocket;
|
||||
Wing = wing;
|
||||
}
|
||||
}
|
||||
}
|
65
AirFighter/AirFighter/FormAirFighter.Designer.cs
generated
65
AirFighter/AirFighter/FormAirFighter.Designer.cs
generated
@ -1,6 +1,6 @@
|
||||
namespace AirFighter
|
||||
{
|
||||
partial class AirFighter
|
||||
partial class FormAirFighter
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
@ -34,6 +34,9 @@
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonCreate = new Button();
|
||||
ButtonStepAirFighter = new Button();
|
||||
comboBoxAirFighter = new ComboBox();
|
||||
ButtonCreateFighter = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -97,29 +100,64 @@
|
||||
//
|
||||
// buttonCreate
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.BackColor = Color.Azure;
|
||||
buttonCreate.Font = new Font("Times New Roman", 14.25F, FontStyle.Bold, GraphicsUnit.Point);
|
||||
buttonCreate.Location = new Point(12, 405);
|
||||
buttonCreate.BackColor = SystemColors.Control;
|
||||
buttonCreate.Font = new Font("Times New Roman", 12F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
buttonCreate.Location = new Point(12, 390);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(95, 33);
|
||||
buttonCreate.TabIndex = 5;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = false;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreate.Size = new Size(164, 48);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать истребитель с ракетами\r\n\r\n";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreateAirFighterMilitary_Click;
|
||||
//
|
||||
// AirFighter
|
||||
// ButtonStepAirFighter
|
||||
//
|
||||
ButtonStepAirFighter.BackColor = Color.Azure;
|
||||
ButtonStepAirFighter.Font = new Font("Times New Roman", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
|
||||
ButtonStepAirFighter.Location = new Point(713, 57);
|
||||
ButtonStepAirFighter.Name = "ButtonStepAirFighter";
|
||||
ButtonStepAirFighter.Size = new Size(75, 23);
|
||||
ButtonStepAirFighter.TabIndex = 3;
|
||||
ButtonStepAirFighter.Text = "Шаг";
|
||||
ButtonStepAirFighter.UseVisualStyleBackColor = true;
|
||||
ButtonStepAirFighter.Click += ButtonStep_Click;
|
||||
//
|
||||
// comboBoxAirFighter
|
||||
//
|
||||
comboBoxAirFighter.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxAirFighter.FormattingEnabled = true;
|
||||
comboBoxAirFighter.Items.AddRange(new object[] { "MoveToCenter", "MoveToBorder", "-" });
|
||||
comboBoxAirFighter.Location = new Point(667, 12);
|
||||
comboBoxAirFighter.Name = "comboBoxAirFighter";
|
||||
comboBoxAirFighter.Size = new Size(121, 23);
|
||||
comboBoxAirFighter.TabIndex = 6;
|
||||
//
|
||||
// ButtonCreateFighter
|
||||
//
|
||||
ButtonCreateFighter.Font = new Font("Times New Roman", 12F, FontStyle.Regular, GraphicsUnit.Point);
|
||||
ButtonCreateFighter.Location = new Point(182, 390);
|
||||
ButtonCreateFighter.Name = "ButtonCreateFighter";
|
||||
ButtonCreateFighter.Size = new Size(164, 47);
|
||||
ButtonCreateFighter.TabIndex = 2;
|
||||
ButtonCreateFighter.Text = "Создаать истребитель";
|
||||
ButtonCreateFighter.UseVisualStyleBackColor = true;
|
||||
ButtonCreateFighter.Click += ButtonCreateFighter_Click;
|
||||
//
|
||||
// FormAirFighter
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
Controls.Add(ButtonCreateFighter);
|
||||
Controls.Add(comboBoxAirFighter);
|
||||
Controls.Add(ButtonStepAirFighter);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(pictureBoxAirFighter);
|
||||
Name = "AirFighter";
|
||||
Name = "FormAirFighter";
|
||||
Text = "AirFighter";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).EndInit();
|
||||
ResumeLayout(false);
|
||||
@ -134,5 +172,8 @@
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonCreate;
|
||||
private Button ButtonStepAirFighter;
|
||||
private ComboBox comboBoxAirFighter;
|
||||
private Button ButtonCreateFighter;
|
||||
}
|
||||
}
|
@ -1,9 +1,14 @@
|
||||
using AirFighter.DrawningObjects;
|
||||
using AirFighter.Drawnings;
|
||||
using AirFighter.MovementStrategy;
|
||||
|
||||
namespace AirFighter
|
||||
{
|
||||
public partial class AirFighter : Form
|
||||
public partial class FormAirFighter : Form
|
||||
{
|
||||
private DrawningAirFighter? _drawningAirFighter;
|
||||
public AirFighter()
|
||||
private AbstractStrategy? _abstractStrategy;
|
||||
public FormAirFighter()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
@ -19,15 +24,31 @@ namespace AirFighter
|
||||
pictureBoxAirFighter.Image = bmp;
|
||||
}
|
||||
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void buttonCreateAirFighterMilitary_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningAirFighter = new DrawningAirFighter();
|
||||
_drawningAirFighter.Init(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(0, 2)), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
|
||||
_drawningAirFighter.SetPosition(random.Next(10, 100),
|
||||
random.Next(10, 100));
|
||||
|
||||
_drawningAirFighter = new DrawningAirFighterMilitary(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(0, 2)),
|
||||
pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
|
||||
_drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
private void ButtonCreateFighter_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random random = new();
|
||||
_drawningAirFighter = new DrawningAirFighter(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);
|
||||
_drawningAirFighter.SetPosition(random.Next(10, 100), random.Next(10,
|
||||
100));
|
||||
Draw();
|
||||
}
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
@ -54,5 +75,44 @@ Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pict
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningAirFighter == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (comboBoxAirFighter.Enabled)
|
||||
{
|
||||
_abstractStrategy = comboBoxAirFighter.SelectedIndex
|
||||
switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawningObjectAirFighter(_drawningAirFighter), pictureBoxAirFighter.Width,
|
||||
pictureBoxAirFighter.Height);
|
||||
comboBoxAirFighter.Enabled = false;
|
||||
}
|
||||
if (_abstractStrategy != null)
|
||||
{
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxAirFighter.Enabled = true;
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
18
AirFighter/AirFighter/IMoveableObject.cs
Normal file
18
AirFighter/AirFighter/IMoveableObject.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using AirFighter.Drawnings;
|
||||
|
||||
namespace AirFighter.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
int GetStep { get; }
|
||||
bool CheckCanMove(DirectionAirFighter direction);
|
||||
void MoveObject(DirectionAirFighter direction);
|
||||
}
|
||||
}
|
37
AirFighter/AirFighter/IMoveableObject_Realise.cs
Normal file
37
AirFighter/AirFighter/IMoveableObject_Realise.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using AirFighter.DrawningObjects;
|
||||
using AirFighter.Drawnings;
|
||||
|
||||
namespace AirFighter.MovementStrategy
|
||||
{
|
||||
public class DrawningObjectAirFighter : IMoveableObject
|
||||
{
|
||||
private readonly DrawningAirFighter? _drawningAirFighter = null;
|
||||
public DrawningObjectAirFighter(DrawningAirFighter drawningAirFighter)
|
||||
{
|
||||
_drawningAirFighter = drawningAirFighter;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningAirFighter == null || _drawningAirFighter.EntityAirFighter ==
|
||||
null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningAirFighter.GetPosX, _drawningAirFighter.GetPosY, _drawningAirFighter.GetWidth, _drawningAirFighter.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningAirFighter?.EntityAirFighter?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionAirFighter direction) =>
|
||||
_drawningAirFighter?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionAirFighter direction) =>
|
||||
_drawningAirFighter?.MoveTransport(direction);
|
||||
}
|
||||
}
|
45
AirFighter/AirFighter/MoveToBorder.cs
Normal file
45
AirFighter/AirFighter/MoveToBorder.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта к границе экрана
|
||||
/// </summary>
|
||||
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 <= 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())
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
64
AirFighter/AirFighter/MoveToCenter.cs
Normal file
64
AirFighter/AirFighter/MoveToCenter.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter.MovementStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
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;
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
|
||||
if (Math.Abs(diffX) > GetStep() || Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
AirFighter/AirFighter/ObjectParameters.cs
Normal file
29
AirFighter/AirFighter/ObjectParameters.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter.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;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ namespace AirFighter
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new AirFighter());
|
||||
Application.Run(new FormAirFighter());
|
||||
}
|
||||
}
|
||||
}
|
15
AirFighter/AirFighter/Status.cs
Normal file
15
AirFighter/AirFighter/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirFighter.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Класс из первой части должен стать дочерним классом, а не родительским