PIbd-23_Lisov_N.A._AirFight.../AirFighter/DrawingAircraft.cs

175 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
internal class DrawingAircraft
{
public EntityAircraft Plane { get; private set; }
private float _startPosX;
private float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
protected readonly int _aircraftWidth = 124;
protected readonly int _aircraftHeight = 70;
public void Init(int speed, float weight, Color bodyColor)
{
Plane = new EntityAircraft();
Plane.Init(speed, weight, bodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
// Check if coords inside the picture
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
case Direction.Right:
{
if (_startPosX + _aircraftWidth + Plane.Step < _pictureWidth)
{
_startPosX += Plane.Step;
}
}
break;
case Direction.Left:
{
if (_startPosX - Plane.Step > 0)
{
_startPosX -= Plane.Step;
}
}
break;
case Direction.Up:
{
if (_startPosY - Plane.Step > 0)
{
_startPosY -= Plane.Step;
}
}
break;
case Direction.Down:
{
if (_startPosY + _aircraftHeight + Plane.Step < _pictureHeight)
{
_startPosY += Plane.Step;
}
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
//Aircraft Body
Pen pen = new(Color.Black);
Brush brWhite = new SolidBrush(Plane.BodyColor);
g.FillEllipse(brWhite, _startPosX + 3, _startPosY + 37, 120, 23);
g.DrawEllipse(pen,_startPosX + 3, _startPosY + 37, 120, 23);
GraphicsPath pathBody = new GraphicsPath();
Point point1B = new Point((int)(_startPosX + 110), (int)(_startPosY + 36));
Point point2B = new Point(point1B.X + 14, point1B.Y + 12);
Point point3B = new Point(point1B.X, point2B.Y + 12);
Point point4B = new Point(point1B.X, point1B.Y);
Point[] pointsBody = new Point[] { point1B, point2B, point3B, point4B };
pathBody.AddLines(pointsBody);
g.DrawPath(pen, pathBody);
g.FillPath(brWhite, pathBody);
g.DrawLine(pen, point1B.X, (point3B.Y + point1B.Y) / 2, point2B.X + 6, point2B.Y);
GraphicsPath pathWings = new GraphicsPath();
//Wings
Point point1W = new Point((int)(_startPosX + 10), (int)(_startPosY)+ 4);
Point point2W = new Point(point1W.X + 35, point1W.Y + 40);
Point point3W = new Point(point1W.X,point1W.Y + 40);
Point point4W = new Point(point1W.X, point1W.Y);
Point[] pointsWings = new Point[] {point1W , point2W , point3W , point4W};
pathWings.AddLines(pointsWings);
g.DrawPath(pen,pathWings);
g.FillPath(brWhite, pathWings);
Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack,point1W.X,point3W.Y,25,10);
g.FillEllipse(brBlack, (int)(_startPosX + 40), (int)(_startPosY + 45),45,8);
//Wheels
Point point1WH = new Point((int)(_startPosX + 40), (int)(_startPosY + 60));
Point point2WH = new Point(point1WH.X, point1WH.Y + 5);
g.DrawLine(pen, point1WH, point2WH);
Point point3WH = new Point((int)(_startPosX + 95),point1WH.Y - 2);
Point point4WH = new Point(point3WH.X,point2WH.Y);
g.DrawLine(pen, point3WH, point4WH);
g.FillEllipse(brBlack, point1WH.X - 5, point2WH.Y,5,5);
g.FillEllipse(brBlack, point1WH.X, point2WH.Y, 5, 5);
g.FillEllipse(brBlack, point3WH.X - 2, point4WH.Y , 5, 5);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _aircraftWidth || _pictureHeight <= _aircraftHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
if (_startPosX + _aircraftWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _aircraftWidth;
}
if (_startPosY + _aircraftHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _aircraftHeight;
}
}
}
}