Лабораторная работа 2
This commit is contained in:
parent
688c992165
commit
59883cb493
73
AirBomber/AirBomber/AbstractStrategy.cs
Normal file
73
AirBomber/AirBomber/AbstractStrategy.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
internal enum DirectionType
|
||||
public enum DirectionType
|
||||
{
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
|
@ -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)
|
||||
{
|
||||
//Сделать проверки (все параметры больше 0 и координаты не выходят за границы полей)
|
||||
//x
|
||||
if ((x < 0 || (x + _AirBomberWidth > width)) || (y < 0 || (y + _AirBomberHeight > height)))
|
||||
if(EntityWarAirplane != null)
|
||||
{
|
||||
_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,43 +70,36 @@ namespace AirBomber
|
||||
point16
|
||||
};
|
||||
|
||||
g.FillPolygon(wingBrush, tail);
|
||||
g.FillPolygon(additionalBrush, tail);
|
||||
g.DrawPolygon(pen, tail);
|
||||
|
||||
//основная часть бомбардировщика
|
||||
SolidBrush bodyBrush = new SolidBrush(AirBomber?.BodyColor ?? Color.Gray);
|
||||
if (airBomber.Bombs)
|
||||
{
|
||||
//бомбы
|
||||
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);
|
||||
|
||||
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 =
|
||||
{
|
||||
PointF[] bomb1 =
|
||||
{
|
||||
point17,
|
||||
point18,
|
||||
point19,
|
||||
point20
|
||||
};
|
||||
Brush brBomb = new SolidBrush(Color.Gray);
|
||||
Brush brBomb = new SolidBrush(Color.Gray);
|
||||
|
||||
g.FillPolygon(brBomb, bomb1);
|
||||
g.DrawPolygon(pen, bomb1);
|
||||
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 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 =
|
||||
{
|
||||
PointF[] bomb2 =
|
||||
{
|
||||
point21,
|
||||
point22,
|
||||
point23,
|
||||
@ -218,51 +107,21 @@ 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;
|
||||
g.FillPolygon(brBomb, bomb2);
|
||||
g.DrawPolygon(pen, bomb2);
|
||||
}
|
||||
|
||||
//топливные баки
|
||||
Rectangle fuelTank = new Rectangle((int)_startPosX + 50, (int)_startPosY + 43, 20, 5);
|
||||
g.FillRectangle(wingBrush, fuelTank);
|
||||
g.DrawRectangle(pen, fuelTank);
|
||||
|
||||
if (airBomber.FuelTank)
|
||||
{
|
||||
//топливные баки
|
||||
Rectangle fuelTank = new Rectangle((int)_startPosX + 50, (int)_startPosY + 43, 20, 5);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
37
AirBomber/AirBomber/DrawingObjectWarAirplane.cs
Normal file
37
AirBomber/AirBomber/DrawingObjectWarAirplane.cs
Normal 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);
|
||||
}
|
||||
}
|
235
AirBomber/AirBomber/DrawingWarAirplane.cs
Normal file
235
AirBomber/AirBomber/DrawingWarAirplane.cs
Normal 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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
AirBomber/AirBomber/EntityWarAirplane.cs
Normal file
22
AirBomber/AirBomber/EntityWarAirplane.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
61
AirBomber/AirBomber/FormAirBomber.Designer.cs
generated
61
AirBomber/AirBomber/FormAirBomber.Designer.cs
generated
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
Draw();
|
||||
_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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
17
AirBomber/AirBomber/IMoveableObject.cs
Normal file
17
AirBomber/AirBomber/IMoveableObject.cs
Normal 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);
|
||||
}
|
||||
}
|
60
AirBomber/AirBomber/MoveToBorder.cs
Normal file
60
AirBomber/AirBomber/MoveToBorder.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
58
AirBomber/AirBomber/MoveToCenter.cs
Normal file
58
AirBomber/AirBomber/MoveToCenter.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
AirBomber/AirBomber/ObjectParameters.cs
Normal file
30
AirBomber/AirBomber/ObjectParameters.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
15
AirBomber/AirBomber/Status.cs
Normal file
15
AirBomber/AirBomber/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 AirBomber.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user