Продвинутый объект

This commit is contained in:
just1valery 2022-09-27 12:53:25 +04:00
parent b699fe0553
commit 0e40ea687b
8 changed files with 332 additions and 176 deletions

1
.gitignore vendored
View File

@ -398,3 +398,4 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
/WarmlyShip/WarmlyShip/Properties

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32414.318
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WarmlyShip", "WarmlyShip\WarmlyShip.csproj", "{007C3F82-87B9-4701-BE6F-6598A4B1AE7E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WarmlyShip", "WarmlyShip\WarmlyShip.csproj", "{007C3F82-87B9-4701-BE6F-6598A4B1AE7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -0,0 +1,188 @@
namespace WarmlyShip
{
internal class DrawningShip
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityShip Ship { get; protected set; }
/// <summary>
/// Левая координата отрисовки коробля
/// </summary>
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки коробля
/// </summary>
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки коробля
/// </summary>
private readonly int _shipWidth = 170;
/// <summary>
/// Высота отрисовки коробля
/// </summary>
private readonly int _shipHeight = 65;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корабля</param>
public DrawningShip(int speed, float weight, Color bodyColor)
{
Ship = new EntityShip(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес коробля</param>
/// <param name="bodyColor">Цвет основной части</param>
/// <param name="shipWidth">Ширина отрисовки корабля</param>
/// <param name="shipHeight">Высота отрисовки корабля</param>
protected DrawningShip(int speed, float weight, Color bodyColor, int shipWidth, int shipHeight) :
this(speed, weight, bodyColor)
{
_shipWidth = shipWidth;
_shipHeight = shipHeight;
}
/// <summary>
/// Установка позиции коробля
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
///
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0 || width < x + _shipWidth || height < y + _shipHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
/// <summary>
/// Изменение направления пермещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _shipWidth + Ship.Step < _pictureWidth)
{
_startPosX += Ship.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Ship.Step > 0)
{
_startPosX -= Ship.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Ship.Step > 0)
{
_startPosY -= Ship.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _shipHeight + Ship.Step < _pictureHeight)
{
_startPosY += Ship.Step;
}
break;
}
}
/// <summary>
/// Отрисовка корабля
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
//заполнение коробля
PointF[] P =
{
new Point((int)(_startPosX),(int) _startPosY + 35),
new Point((int)_startPosX + 170, (int)_startPosY + 35),
new Point( (int) _startPosX + 150,(int) _startPosY + 65),
new Point((int) _startPosX + 50, (int) _startPosY + 65),
new Point((int) _startPosX, (int) _startPosY + 35),
};
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black);
g.FillPolygon(br, P);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 50, _startPosY + 1, 100, 34);
Pen pen = new(Color.Black);
//границы коробля
g.DrawRectangle(pen, _startPosX + 50, _startPosY + 1, 100, 34);
g.DrawLine(pen, _startPosX, _startPosY + 35, _startPosX + 170, _startPosY + 35);
g.DrawLine(pen, _startPosX, _startPosY + 35, _startPosX + 50, _startPosY + 65);
g.DrawLine(pen, _startPosX + 50, _startPosY + 65, _startPosX + 150, _startPosY + 65);
g.DrawLine(pen, _startPosX + 150, _startPosY + 65, _startPosX + 170, _startPosY + 35);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _shipWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _shipHeight;
}
}
}
}

View File

@ -1,170 +1,58 @@
namespace WarmlyShip
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class DrawningWarmlyShip
internal class DrawningWarmlyShip : DrawningShip
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityWarmlyShip Ship { get; private set; }
/// <summary>
/// Левая координата отрисовки коробля
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки коробля
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки коробля
/// </summary>
private readonly int _shipWidth = 170;
/// <summary>
/// Высота отрисовки коробля
/// </summary>
private readonly int _shipHeight = 65;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес коробля</param>
/// <param name="bodyColor">Цвет основной части</param>
public DrawningWarmlyShip(int speed, float weight, Color bodyColor)
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия труб</param>
/// <param name="fuelCompartment">Признак наличия топливного отсека</param>
public DrawningWarmlyShip(int speed, float weight, Color bodyColor, Color dopColor, bool pipes, bool fuelCompartment) :
base(speed, weight, bodyColor, 175, 65)
{
Ship = new EntityWarmlyShip(speed, weight, bodyColor);
}
/// <summary>
/// Установка позиции коробля
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0 || width < x + _shipWidth || height < y + _shipHeight)
{
_pictureHeight = null;
_pictureWidth = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
Ship = new EntityWarmlyShip(speed, weight, bodyColor, dopColor, pipes, fuelCompartment);
}
/// <summary>
/// Изменение направления пермещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
public override void DrawTransport(Graphics g)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
if (Ship is not EntityWarmlyShip warmlyShip)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _shipWidth + Ship.Step < _pictureWidth)
{
_startPosX += Ship.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Ship.Step > 0)
{
_startPosX -= Ship.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Ship.Step > 0)
{
_startPosY -= Ship.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _shipHeight + Ship.Step < _pictureHeight)
{
_startPosY += Ship.Step;
}
break;
}
}
/// <summary>
/// Отрисовка корабля
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
//заполнение коробля
PointF[] P =
{
new Point((int)(_startPosX),(int) _startPosY + 35),
new Point((int)_startPosX + 170, (int)_startPosY + 35),
new Point( (int) _startPosX + 150,(int) _startPosY + 65),
new Point((int) _startPosX + 50, (int) _startPosY + 65),
new Point((int) _startPosX, (int) _startPosY + 35),
};
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black);
g.FillPolygon(br, P);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX + 70, _startPosY + 1, 70, 34);
Pen pen = new(Color.Black);
//границы коробля
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 1 , 70, 34);
g.DrawLine(pen, _startPosX, _startPosY + 35, _startPosX + 170, _startPosY + 35);
g.DrawLine(pen, _startPosX, _startPosY + 35, _startPosX + 50, _startPosY + 65);
g.DrawLine(pen, _startPosX + 50, _startPosY + 65, _startPosX + 150, _startPosY + 65);
g.DrawLine(pen, _startPosX + 150, _startPosY + 65, _startPosX + 170, _startPosY + 35);
}
Brush dopBrush = new SolidBrush(warmlyShip.DopColor);
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
if (warmlyShip.Pipes)
{
_pictureWidth = null;
_pictureHeight = null;
return;
g.FillRectangle(dopBrush, _startPosX + 60, _startPosY + 10, 20, 26);
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 10, 20, 26);
g.FillRectangle(dopBrush, _startPosX + 90, _startPosY + 5, 20, 31);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 5, 20, 31);
g.FillRectangle(dopBrush, _startPosX + 120, _startPosY + 1, 20, 35);
g.DrawRectangle(pen, _startPosX + 120, _startPosY + 1, 20, 35);
}
if (_startPosX + _shipWidth > _pictureWidth)
//_startPosX += 10;
_startPosY += 30;
base.DrawTransport(g);
//_startPosX -= 10;
_startPosY -= 30;
if (warmlyShip.FuelCompartment)
{
_startPosX = _pictureWidth.Value - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _shipHeight;
g.FillRectangle(dopBrush, _startPosX + 135, _startPosY + 70, 10, 5);
g.DrawRectangle(pen, _startPosX + 135, _startPosY + 70, 10, 5);
}
}
}

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class EntityShip
{
/// <summary>
/// Скорость
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// Вес
/// </summary>
public float Weight { get; private set; }
/// <summary>
/// Цвет кузова
/// </summary>
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// </summary>
public float Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса автомобиля
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public EntityShip(int speed, float weight, Color bodyColor)
{
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
}
}

View File

@ -6,37 +6,35 @@ using System.Threading.Tasks;
namespace WarmlyShip
{
internal class EntityWarmlyShip
internal class EntityWarmlyShip : EntityShip
{
/// <summary>
/// Скорость
/// Дополнительный цвет
/// </summary>
public int Speed { get; private set; }
public Color DopColor { get; private set; }
/// <summary>
/// Вес
/// Признак наличия труб
/// </summary>
public float Weight { get; private set; }
public bool Pipes { get; private set; }
/// <summary>
/// Цвет кузова
/// Признак наличия топливного отсека
/// </summary>
public Color BodyColor { get; private set; }
public bool FuelCompartment { get; private set; }
/// <summary>
/// Шаг перемещения автомобиля
/// Инициализация свойств
/// </summary>
public float Step => Speed * 100 / Weight;
/// <summary>
/// Инициализация полей объекта-класса автомобиля
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public EntityWarmlyShip(int speed, float weight, Color bodyColor)
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="pipes">Признак наличия труб</param>
/// <param name="fuelCompartment">Признак наличия топливного отсека</param>
public EntityWarmlyShip(int speed, float weight, Color bodyColor, Color dopColor, bool pipes, bool fuelCompartment) :
base(speed, weight, bodyColor)
{
Random rnd = new();
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
DopColor = dopColor;
Pipes = pipes;
FuelCompartment = fuelCompartment;
}
}
}

View File

@ -38,6 +38,7 @@
this.buttonUp = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.ButtonCreateModif = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
@ -146,11 +147,23 @@
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.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(124, 369);
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);
//
// FormShip
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.ButtonCreateModif);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonUp);
@ -180,5 +193,6 @@
private Button buttonUp;
private Button buttonRight;
private Button buttonLeft;
private Button ButtonCreateModif;
}
}

View File

@ -2,11 +2,22 @@ namespace WarmlyShip
{
public partial class FormShip : Form
{
private DrawningWarmlyShip _ship;
private DrawningShip _ship;
public FormShip()
{
InitializeComponent();
}
/// <summary>
/// Ìåòîä óñòàíîâêè äàííûõ
/// </summary>
private void SetData()
{
Random rnd = new();
_ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_ship.Ship.BodyColor.Name}";
}
private void Draw()
{
Bitmap bmp = new(pictureBoxShip.Width, pictureBoxShip.Height);
@ -22,11 +33,9 @@ namespace WarmlyShip
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
_ship = new DrawningWarmlyShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_ship = new DrawningShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
_ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height);
toolStripStatusLabelSpeed.Text = $"Ñêîðîñòü: {_ship.Ship.Speed}";
toolStripStatusLabelWeight.Text = $"Âåñ: {_ship.Ship.Weight}";
toolStripStatusLabelBodyColor.Text = $"Öâåò: {_ship.Ship.BodyColor.Name}";
SetData();
Draw();
}
@ -51,5 +60,21 @@ namespace WarmlyShip
}
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ìîäèôèêàöèÿ"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
_ship = new DrawningWarmlyShip(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)));
SetData();
Draw();
}
}
}