Добавление интерфейса

This commit is contained in:
Артем Харламов 2022-10-26 19:35:34 +04:00
parent d4ebff9421
commit b1a4a7ce49
7 changed files with 261 additions and 11 deletions

View File

@ -14,15 +14,15 @@ namespace Stormtrooper
/// </summary>
internal class Drawning
{
public EntityStormtrooper Storm { get; private set; }
public EntityStormtrooper Storm { get; protected set; }
/// <summary>
/// Левая координата отрисовки самолёта
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Нижняя кооридната отрисовки самолёта
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -50,6 +50,20 @@ namespace Stormtrooper
Storm = new EntityStormtrooper(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолёта</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки самолёта</param>
/// <param name="carHeight">Высота отрисовки самолёта</param>
protected Drawning(int speed, float weight, Color bodyColor, int stormWidth, int stormHeight) :
this(speed, weight, bodyColor)
{
_StormWidth = stormWidth;
_StormHeight = stormHeight;
}
/// <summary>
/// Установка позиции самолёта
/// </summary>
/// <param name="x">Координата X</param>
@ -116,7 +130,7 @@ namespace Stormtrooper
/// Отрисовка штурмовика
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
@ -194,5 +208,13 @@ namespace Stormtrooper
_startPosY = _pictureHeight.Value - _StormHeight;
}
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _StormWidth, _startPosY + _StormHeight);
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class DrawningMilitary : Drawning
{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолёта</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="rocket">Признак наличия антикрыла</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
public DrawningMilitary(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool rocket, bool sportLine) :
base(speed, weight, bodyColor, 135, 100)
{
Storm = new EntityMilitaryStormtrooper(speed, weight, bodyColor, dopColor, bodyKit, rocket, sportLine);
}
public override void DrawTransport(Graphics g)
{
if (Storm is not EntityMilitaryStormtrooper militaryTrop)
{
return;
}
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(militaryTrop.DopColor);
if (militaryTrop.BodyKit)
{
g.DrawEllipse(pen, _startPosX - 2, _startPosY - 70, 5, 20);
g.DrawEllipse(pen, _startPosX - 2, _startPosY - 50, 5, 20);
g.FillEllipse(dopBrush, _startPosX - 2, _startPosY - 70, 5, 20);
g.FillEllipse(dopBrush, _startPosX - 2, _startPosY - 50, 5, 20);
}
_startPosX += 2;
base.DrawTransport(g);
_startPosX -= 2;
if (militaryTrop.Rocket)
{
g.FillRectangle(dopBrush, _startPosX + 50, _startPosY - 70, 40, 3);
g.FillRectangle(dopBrush, _startPosX + 50, _startPosY - 90, 40, 3);
g.FillRectangle(dopBrush, _startPosX + 50, _startPosY - 30, 40, 3);
g.FillRectangle(dopBrush, _startPosX + 50, _startPosY - 10, 40, 3);
}
if (militaryTrop.SportLine)
{
g.FillRectangle(dopBrush, _startPosX + 30, _startPosY - 60, 8, 20);
g.FillRectangle(dopBrush, _startPosX + 100, _startPosY - 60, 8, 20);
}
}
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class DrawningObjectStorm : IDrawningObject
{
private Drawning _storm = null;
public DrawningObjectStorm(Drawning storm)
{
_storm = storm;
}
public float Step => _storm?.Storm?.Step ?? 0;
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return _storm?.GetCurrentPosition() ?? default;
}
public void MoveObject(Direction direction)
{
_storm?.MoveTransport(direction);
}
public void SetObject(int x, int y, int width, int height)
{
_storm.SetPosition(x, y, width, height);
}
void IDrawningObject.DrawningObject(Graphics g)
{
_storm.DrawTransport(g);
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class EntityMilitaryStormtrooper : EntityStormtrooper
{
/// <summary>
/// Дополнительный цвет
/// </summary>
public Color DopColor { get; private set; }
/// <summary>
/// Признак наличия обвеса
/// </summary>
public bool BodyKit { get; private set; }
/// <summary>
/// Признак наличия рокет
/// </summary>
public bool Rocket { get; private set; }
/// <summary>
/// Признак наличия полосы
/// </summary>
public bool SportLine { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолёта</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="rocket">Признак наличия рокет</param>
/// <param name="sportLine">Признак наличия гоночной полосы</param>
public EntityMilitaryStormtrooper(int speed, float weight, Color bodyColor, Color
dopColor, bool bodyKit, bool rocket, bool sportLine) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
Rocket = rocket;
SportLine = sportLine;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonCreateModif = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxStormtrooper)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -147,11 +148,23 @@
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonCreateModif
//
this.buttonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreateModif.Location = new System.Drawing.Point(125, 396);
this.buttonCreateModif.Name = "buttonCreateModif";
this.buttonCreateModif.Size = new System.Drawing.Size(120, 29);
this.buttonCreateModif.TabIndex = 7;
this.buttonCreateModif.Text = "Модификация";
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click);
//
// FormStormtrooper
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(831, 461);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -181,5 +194,6 @@
private Button buttonLeft;
private Button buttonDown;
private Button buttonUp;
private Button buttonCreateModif;
}
}

View File

@ -3,12 +3,11 @@ namespace Stormtrooper
public partial class FormStormtrooper : Form
{
private Drawning _storm;
public FormStormtrooper()
{
InitializeComponent();
}
//private EntityStormtrooper Storm;
/// <summary>
/// Ìåòîä ïðîðèñîâêè ñàìîë¸òà
/// </summary>
@ -19,6 +18,18 @@ namespace Stormtrooper
_storm?.DrawTransport(gr);
pictureBoxStormtrooper.Image = bmp;
}
/// <summary>
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
private void SetData()
{
Random rnd = new();
_storm.SetPosition(rnd.Next(0, 150), rnd.Next(0, 300), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_storm.Storm.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_storm.Storm.Weight}";
toolStripStatusLabelColor.Text = $"Öâåò: {_storm.Storm.BodyColor.Name}";
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü"
/// </summary>
@ -27,11 +38,8 @@ namespace Stormtrooper
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_storm = new Drawning(rnd.Next(100,300), rnd.Next(1000,2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_storm.SetPosition(rnd.Next(0, 150), rnd.Next(0, 300), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_storm.Storm.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_storm.Storm.Weight}";
toolStripStatusLabelColor.Text = $"Öâåò: {_storm.Storm.BodyColor.Name}";
_storm = new Drawning(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
SetData();
Draw();
}
/// <summary>
@ -70,5 +78,21 @@ namespace Stormtrooper
_storm?.ChangeBorders(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height);
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_storm = new DrawningMilitary(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData();
Draw();
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
/// <summary>
/// Интерфейс для работы с объектом, прорисовываемым на форме
/// </summary>
internal interface IDrawningObject
{
/// <summary>
/// Шаг перемещения объекта
/// </summary>
public float Step { get; }
/// <summary>
/// Установка позиции объекта
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина полотна</param>
/// <param name="height">Высота полотна</param>
void SetObject(int x, int y, int width, int height);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
/// <summary>
/// Отрисовка объекта
/// </summary>
/// <param name="g"></param>
void DrawningObject(Graphics g);
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
}
}