PIbd-12_Karamushko_M.K._Air.../AirFighter/AirFighter/DrawingAirFighter.cs
2022-11-01 17:51:11 +03:00

173 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
public class DrawingAirFighter
{
public EntityAirFighter AirFighter { get; protected set; }
protected float _startPosX;
protected float _startPosY;
private int? _pictureWidth = null;
private int? _pictureHeight = null;
private readonly int _airFighterWidth = 195;
private readonly int _airFighterHeight = 166;
public DrawingAirFighter(int speed, float weight, Color bodyColor)
{
AirFighter = new EntityAirFighter(speed, weight, bodyColor);
}
public DrawingAirFighter(int speed, float weight, Color bodyColor, int airFighterWidth, int airFighterHeight) :
this(speed, weight, bodyColor)
{
_airFighterWidth = airFighterWidth;
_airFighterHeight = airFighterHeight;
}
public DrawingAirFighter Update(int? speed = null, float? weight = null, Color? bodyColor = null)
{
return new DrawingAirFighter(speed ?? AirFighter.Speed, weight ?? AirFighter.Weight, bodyColor ?? AirFighter.BodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width < _airFighterWidth || height < _airFighterHeight) return;
if (x + _airFighterWidth > width || x < 0) return;
if (y + _airFighterHeight > height || y < 0) return;
_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 + _airFighterWidth + AirFighter.Step < _pictureWidth)
{
_startPosX += AirFighter.Step;
}
break;
case Direction.Left:
if (_startPosX - AirFighter.Step > 0)
{
_startPosX -= AirFighter.Step;
}
break;
case Direction.Up:
if (_startPosY - AirFighter.Step > 0)
{
_startPosY -= AirFighter.Step;
}
break;
case Direction.Down:
if (_startPosY + _airFighterHeight + AirFighter.Step < _pictureHeight)
{
_startPosY += AirFighter.Step;
}
break;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return ( _startPosX, _startPosX + _airFighterWidth, _startPosY, _startPosY + _airFighterHeight );
}
public virtual void DrawTransport(Graphics g)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
MessageBox.Show("test");
return;
}
Pen pen = new(AirFighter.BodyColor, 2);
Brush brushBlack = new SolidBrush(AirFighter.BodyColor);
PointF[] front = {
new(_startPosX + 160, _startPosY + 69),
new(_startPosX + 195, _startPosY + 83),
new(_startPosX + 160, _startPosY + 97)
};
PointF[] tailTop = {
new(_startPosX, _startPosY + 30),
new(_startPosX, _startPosY + 70),
new(_startPosX + 25, _startPosY + 70),
new(_startPosX + 25, _startPosY + 55)
};
PointF[] tailBottom = {
new(_startPosX, _startPosY + 96),
new(_startPosX, _startPosY + 136),
new(_startPosX + 25, _startPosY + 111),
new(_startPosX + 25, _startPosY + 96)
};
PointF[] wingTop =
{
new(_startPosX + 100, _startPosY),
new(_startPosX + 100, _startPosY + 70),
new(_startPosX + 75, _startPosY + 70),
new(_startPosX + 90, _startPosY),
};
PointF[] wingBottom =
{
new(_startPosX + 100, _startPosY + 96),
new(_startPosX + 100, _startPosY + 166),
new(_startPosX + 90, _startPosY + 166),
new(_startPosX + 75, _startPosY + 96),
};
g.DrawPolygon(pen, tailTop);
g.DrawPolygon(pen, tailBottom);
g.DrawPolygon(pen, wingTop);
g.DrawPolygon(pen, wingBottom);
g.DrawRectangle(pen, _startPosX, _startPosY + 70, 160, 26);
g.FillPolygon(brushBlack, front);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airFighterWidth || _pictureHeight <= _airFighterHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _airFighterWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _airFighterWidth;
}
if (_startPosY + _airFighterHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _airFighterHeight;
}
}
}
}