Создается модифицированный локомотив, требуется логика отрисовки

This commit is contained in:
Данила Мочалов 2022-09-25 17:06:29 +04:00
parent da86c334a4
commit fc639a1deb
5 changed files with 120 additions and 8 deletions

View File

@ -10,11 +10,11 @@ namespace Locomotive
internal class DrawningLocomotive
{
/// Класс-сущность
public EntityLocomotive Locomotive { get; private set; }
public EntityLocomotive Locomotive { get; protected set; }
/// Левая координата отрисовки локомотива
private float _startPosX;
protected float _startPosX;
/// Верхняя координата отрисовки локомотива
private float _startPosY;
protected float _startPosY;
/// Ширина окна отрисовки
private int? _pictureWidth = null;
/// Высота окна отрисовки
@ -29,6 +29,15 @@ namespace Locomotive
{
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
}
// Новый конструктор
protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight)
: this (speed, weight, bodyColor)
{
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
/// Установка позиции локомотива
public void SetPosition(int x, int y, int width, int height)
{
@ -89,7 +98,7 @@ namespace Locomotive
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class DrawningWarmlyLocomotive : DrawningLocomotive
{
public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, bool pipe, bool storage)
: base(speed, weight, bodyColor, locomotiveWidth: 110, locomotiveHeight: 50)
{
Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage);
}
public override void DrawTransport(Graphics g)
{
if (Locomotive is not EntityWarmlyLocomotive warmlyLocomotive)
{
return;
}
Pen pen = new(Color.Black);
Brush extraBrush = new SolidBrush(warmlyLocomotive.ExtraColor);
base.DrawTransport(g);
if (warmlyLocomotive.Pipe)
{
//TODO
}
if (warmlyLocomotive.FuelStorage)
{
//TODO
}
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
internal class EntityWarmlyLocomotive : EntityLocomotive
{
//доп. цвет
public Color ExtraColor { get; private set; }
//признак наличия трубы
public bool Pipe { get; private set; }
//признак наличия отсека под топливо
public bool FuelStorage { get; private set; }
public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, bool pipe, bool fuelStorage)
: base(speed, weight, bodyColor)
{
ExtraColor = extraColor;
Pipe = pipe;
FuelStorage = fuelStorage;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonCreateModified = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxLocomotive)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -51,7 +52,6 @@
this.pictureBoxLocomotive.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxLocomotive.TabIndex = 0;
this.pictureBoxLocomotive.TabStop = false;
this.pictureBoxLocomotive.Resize += new System.EventHandler(this.pictureBoxLocomotive_Resize);
//
// statusStrip1
@ -144,11 +144,22 @@
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonCreateModified
//
this.buttonCreateModified.Location = new System.Drawing.Point(112, 377);
this.buttonCreateModified.Name = "buttonCreateModified";
this.buttonCreateModified.Size = new System.Drawing.Size(94, 29);
this.buttonCreateModified.TabIndex = 7;
this.buttonCreateModified.Text = "Modified";
this.buttonCreateModified.UseVisualStyleBackColor = true;
this.buttonCreateModified.Click += new System.EventHandler(this.buttonCreateModified_Click);
//
// FormLocomotive
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(778, 442);
this.Controls.Add(this.buttonCreateModified);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
@ -178,5 +189,6 @@
private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonCreateModified;
}
}

View File

@ -18,14 +18,19 @@ namespace Locomotive
pictureBoxLocomotive.Image = bmp;
}
private void SetData()
{
toolStripStatusLabelSpeed.Text = $"Speed: {_locomotive.Locomotive.Speed}";
toolStripStatusLabelWeight.Text = $"Weight: {_locomotive.Locomotive.Weight}";
toolStripStatusLabelColor.Text = $"Color: {_locomotive.Locomotive.BodyColor.Name}";
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_locomotive = new DrawningLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
toolStripStatusLabelSpeed.Text = $"Speed: {_locomotive.Locomotive.Speed}";
toolStripStatusLabelWeight.Text = $"Weight: {_locomotive.Locomotive.Weight}";
toolStripStatusLabelColor.Text = $"Color: {_locomotive.Locomotive.BodyColor.Name}";
SetData();
Draw();
}
@ -56,5 +61,19 @@ namespace Locomotive
_locomotive?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
Draw();
}
private void buttonCreateModified_Click(object sender, EventArgs e)
{
Random rnd = new();
_locomotive = new DrawningWarmlyLocomotive(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,1)),
Convert.ToBoolean(rnd.Next(0, 1)));
_locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height);
SetData();
Draw();
}
}
}