2023-11-28 01:11:20 +04:00

167 lines
6.5 KiB
C#

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