Реализован способ инициализации разных типов двигателей

This commit is contained in:
Данияр Аглиуллов 2022-09-22 07:50:53 +04:00
parent 006cea0aca
commit a02a602dce
4 changed files with 54 additions and 20 deletions

View File

@ -19,8 +19,9 @@ namespace AirBomber
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="hasBombs">Признак наличия бомб</param>
/// <param name="hasFuelTanks">Признак наличия топливных баков</param>
public DrawningAirBomber(int speed, float weight, Color bodyColor, Color dopColor, bool hasBombs, bool hasFuelTanks)
: base(speed, weight, bodyColor, 115, 155)
/// <param name="typeAirplaneEngines">Какой будет двигатель самолета. null - двигатели отсутствуют</param>
public DrawningAirBomber(int speed, float weight, Color bodyColor, Color dopColor, bool hasBombs, bool hasFuelTanks, IAirplaneEngines? typeAirplaneEngines = null)
: base(speed, weight, bodyColor, 115, 155, typeAirplaneEngines)
{
Airplane = new EntityAirBomber(speed, weight, bodyColor, dopColor, hasBombs, hasFuelTanks);
}

View File

@ -10,7 +10,7 @@
/// </summary>
public EntityAirplane Airplane { get; protected set; }
public IAirplaneEngines DrawningEngines { get; private set; }
public IAirplaneEngines? DrawningEngines { get; private set; }
/// <summary>
/// Левая координата отрисовки автомобиля
/// </summary>
@ -30,21 +30,22 @@
/// <summary>
/// Ширина отрисовки автомобиля
/// </summary>
protected readonly int _airplaneWidth = 80;
protected readonly int _airplaneWidth = 90;
/// <summary>
/// Высота отрисовки автомобиля
/// </summary>
protected readonly int _airplaneHeight = 90;
protected readonly int _airplaneHeight = 105;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолета</param>
/// <param name="bodyColor">Цвет обшивки</param>
public DrawningAirplane(int speed, float weight, Color bodyColor)
/// <param name="typeAirplaneEngines">Какой будет двигатель самолета. null - двигатели отсутствуют</param>
public DrawningAirplane(int speed, float weight, Color bodyColor, IAirplaneEngines? typeAirplaneEngines = null)
{
Airplane = new EntityAirplane(speed, weight, bodyColor);
DrawningEngines = new DrawningAirplaneEngines();
DrawningEngines = typeAirplaneEngines;
}
/// <summary>
@ -55,8 +56,9 @@
/// <param name="bodyColor">Цвет обшивки</param>
/// <param name="airplaneWidth">Ширина отрисовки самолета</param>
/// <param name="airplaneHeight">Высота отрисовки самолета</param>
protected DrawningAirplane(int speed, float weight, Color bodyColor, int airplaneWidth, int airplaneHeight) :
this(speed, weight, bodyColor)
/// <param name="typeAirplaneEngines">Какой будет двигатель самолета. null - двигатели отсутствуют</param>
protected DrawningAirplane(int speed, float weight, Color bodyColor, int airplaneWidth, int airplaneHeight, IAirplaneEngines? typeAirplaneEngines) :
this(speed, weight, bodyColor, typeAirplaneEngines)
{
_airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight;
@ -169,7 +171,7 @@
// Конец хвоста
g.FillRectangle(BodyColorBrush, x + w - 5, y + 15, 5, h - 30);
// Двигатели
DrawningEngines.DrawEngines(g, Airplane.BodyColor, x + w / 2 - 20, y + h, y, 15);
DrawningEngines?.DrawEngines(g, Airplane.BodyColor, x + w / 2 - 20, y + h, y, 15);
}

View File

@ -33,6 +33,7 @@
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusCountEngines = new System.Windows.Forms.ToolStripStatusLabel();
this.buttonCreate = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
@ -40,7 +41,7 @@
this.buttonDown = new System.Windows.Forms.Button();
this.countEngineBox = new System.Windows.Forms.NumericUpDown();
this.labelInformCountEngines = new System.Windows.Forms.Label();
this.toolStripStatusCountEngines = new System.Windows.Forms.ToolStripStatusLabel();
this.comboTypeEngines = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCar)).BeginInit();
this.statusStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.countEngineBox)).BeginInit();
@ -87,10 +88,16 @@
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(36, 17);
this.toolStripStatusLabelBodyColor.Text = "Цвет:";
//
// toolStripStatusCountEngines
//
this.toolStripStatusCountEngines.Name = "toolStripStatusCountEngines";
this.toolStripStatusCountEngines.Size = new System.Drawing.Size(139, 17);
this.toolStripStatusCountEngines.Text = "Количество двигателей:";
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(244, 390);
this.buttonCreate.Location = new System.Drawing.Point(372, 390);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(75, 23);
this.buttonCreate.TabIndex = 2;
@ -149,37 +156,45 @@
// countEngineBox
//
this.countEngineBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.countEngineBox.Location = new System.Drawing.Point(142, 390);
this.countEngineBox.Location = new System.Drawing.Point(306, 390);
this.countEngineBox.Maximum = new decimal(new int[] {
10000,
0,
0,
0});
this.countEngineBox.Name = "countEngineBox";
this.countEngineBox.Size = new System.Drawing.Size(96, 23);
this.countEngineBox.Size = new System.Drawing.Size(60, 23);
this.countEngineBox.TabIndex = 7;
//
// labelInformCountEngines
//
this.labelInformCountEngines.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.labelInformCountEngines.AutoSize = true;
this.labelInformCountEngines.Location = new System.Drawing.Point(0, 394);
this.labelInformCountEngines.Location = new System.Drawing.Point(161, 394);
this.labelInformCountEngines.Name = "labelInformCountEngines";
this.labelInformCountEngines.Size = new System.Drawing.Size(139, 15);
this.labelInformCountEngines.TabIndex = 8;
this.labelInformCountEngines.Text = "Количество двигателей:";
//
// toolStripStatusCountEngines
// comboTypeEngines
//
this.toolStripStatusCountEngines.Name = "toolStripStatusCountEngines";
this.toolStripStatusCountEngines.Size = new System.Drawing.Size(139, 17);
this.toolStripStatusCountEngines.Text = "Количество двигателей:";
this.comboTypeEngines.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.comboTypeEngines.FormattingEnabled = true;
this.comboTypeEngines.Items.AddRange(new object[] {
"Закругленный",
"Квадратный",
"Стрелка"});
this.comboTypeEngines.Location = new System.Drawing.Point(12, 390);
this.comboTypeEngines.Name = "comboTypeEngines";
this.comboTypeEngines.Size = new System.Drawing.Size(143, 23);
this.comboTypeEngines.TabIndex = 9;
//
// FormAirBomber
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.comboTypeEngines);
this.Controls.Add(this.labelInformCountEngines);
this.Controls.Add(this.countEngineBox);
this.Controls.Add(this.buttonDown);
@ -215,5 +230,6 @@
private NumericUpDown countEngineBox;
private Label labelInformCountEngines;
private ToolStripStatusLabel toolStripStatusCountEngines;
private ComboBox comboTypeEngines;
}
}

View File

@ -25,8 +25,23 @@ namespace AirBomber
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
IAirplaneEngines? typeAirplaneEngines = null;
switch (comboTypeEngines.Text)
{
case "Êâàäðàòíûé":
typeAirplaneEngines = new AirplaneRectEngines();
break;
case "Ñòðåëêà":
typeAirplaneEngines = new AirplaneArrowEngines();
break;
default:
typeAirplaneEngines = new DrawningAirplaneEngines();
break;
}
Random rnd = new();
_airplane = new DrawningAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_airplane = new DrawningAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
typeAirplaneEngines);
_airplane.DrawningEngines.CountEngines = (int)countEngineBox.Value;
_airplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxCar.Width, pictureBoxCar.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_airplane.Airplane.Speed}";