From 0f508fa568312f758824e7664d4f26740bf4fce9 Mon Sep 17 00:00:00 2001 From: Efi Date: Fri, 31 Mar 2023 23:16:35 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Monorail/Monorail/BushesMap.cs | 51 ++++++++ Monorail/Monorail/Direction.cs | 2 +- Monorail/Monorail/DrawingLocomotive.cs | 4 +- Monorail/Monorail/EntityLocomotive.cs | 2 +- Monorail/Monorail/FieldMap.cs | 47 ++++++++ Monorail/Monorail/FormLocomotive.Designer.cs | 26 ++++ Monorail/Monorail/FormLocomotive.cs | 80 ++++++++----- Monorail/Monorail/FormMap.Designer.cs | 2 +- Monorail/Monorail/FormMap.cs | 6 + .../FormMapWithSetLocomotive.Designer.cs | 15 ++- Monorail/Monorail/FormMapWithSetLocomotive.cs | 111 ++++++++++++++++++ .../Monorail/MapWithSetLocomotiveGeneric.cs | 8 +- Monorail/Monorail/Program.cs | 2 +- 13 files changed, 315 insertions(+), 41 deletions(-) create mode 100644 Monorail/Monorail/BushesMap.cs create mode 100644 Monorail/Monorail/FieldMap.cs diff --git a/Monorail/Monorail/BushesMap.cs b/Monorail/Monorail/BushesMap.cs new file mode 100644 index 0000000..781a1c7 --- /dev/null +++ b/Monorail/Monorail/BushesMap.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class BushesMap : AbstractMap + { + + private readonly Pen barrierColor = new Pen(Color.DarkGreen, 3); + private readonly Brush roadColor = new SolidBrush(Color.Brown); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1)), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1)) + 7)); + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1))), new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1)) + 7)); + g.DrawLine(barrierColor, new Point(Convert.ToInt32(i * (_size_x - 1) + 7), Convert.ToInt32(j * (_size_y - 1)) + 7), new Point(Convert.ToInt32(i * (_size_x - 1) + 14), Convert.ToInt32(j * (_size_y - 1)))); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + + } +} diff --git a/Monorail/Monorail/Direction.cs b/Monorail/Monorail/Direction.cs index de8df2e..8b9868a 100644 --- a/Monorail/Monorail/Direction.cs +++ b/Monorail/Monorail/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Monorail { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/Monorail/Monorail/DrawingLocomotive.cs b/Monorail/Monorail/DrawingLocomotive.cs index f1fbb71..80bd428 100644 --- a/Monorail/Monorail/DrawingLocomotive.cs +++ b/Monorail/Monorail/DrawingLocomotive.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Monorail { - internal class DrawingLocomotive + public class DrawingLocomotive { public EntityLocomotive Locomotive { get; protected set; } protected float _startPosX; @@ -98,7 +98,7 @@ namespace Monorail Locomotive = new EntityLocomotive(speed, weight, bodyColor); } - protected DrawingLocomotive(int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) : + public DrawingLocomotive(int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) : this(speed, weight, bodyColor) { _locomotiveWidth = locomotiveWidth; diff --git a/Monorail/Monorail/EntityLocomotive.cs b/Monorail/Monorail/EntityLocomotive.cs index 0ea9564..9b93dad 100644 --- a/Monorail/Monorail/EntityLocomotive.cs +++ b/Monorail/Monorail/EntityLocomotive.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Monorail { - internal class EntityLocomotive + public class EntityLocomotive { public int Speed { get; private set; } public float Weight { get; private set; } diff --git a/Monorail/Monorail/FieldMap.cs b/Monorail/Monorail/FieldMap.cs new file mode 100644 index 0000000..888b0d6 --- /dev/null +++ b/Monorail/Monorail/FieldMap.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class FieldMap : AbstractMap + { + private readonly Brush barrierColor = new SolidBrush(Color.Brown); + private readonly Brush roadColor = new SolidBrush(Color.Green); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillEllipse(barrierColor, i * (_size_x - 1), j * (_size_y - 1), 30, 15); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x), j * (_size_y)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 20) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} diff --git a/Monorail/Monorail/FormLocomotive.Designer.cs b/Monorail/Monorail/FormLocomotive.Designer.cs index 6f8638f..cab84d5 100644 --- a/Monorail/Monorail/FormLocomotive.Designer.cs +++ b/Monorail/Monorail/FormLocomotive.Designer.cs @@ -39,6 +39,8 @@ buttonLeft = new Button(); buttonRight = new Button(); buttonDown = new Button(); + buttonCreateModify = new Button(); + ButtonSelectLocomotive = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).BeginInit(); statusStrip.SuspendLayout(); SuspendLayout(); @@ -139,11 +141,33 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // + // buttonCreateModify + // + buttonCreateModify.Location = new Point(93, 390); + buttonCreateModify.Name = "buttonCreateModify"; + buttonCreateModify.Size = new Size(152, 23); + buttonCreateModify.TabIndex = 7; + buttonCreateModify.Text = "Модифицировать"; + buttonCreateModify.UseVisualStyleBackColor = true; + buttonCreateModify.Click += ButtonCreateModify_Click; + // + // ButtonSelectLocomotive + // + ButtonSelectLocomotive.Location = new Point(635, 12); + ButtonSelectLocomotive.Name = "ButtonSelectLocomotive"; + ButtonSelectLocomotive.Size = new Size(153, 33); + ButtonSelectLocomotive.TabIndex = 8; + ButtonSelectLocomotive.Text = "Выбрать"; + ButtonSelectLocomotive.UseVisualStyleBackColor = true; + ButtonSelectLocomotive.Click += ButtonSelectLocomotive_Click; + // // FormLocomotive // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(ButtonSelectLocomotive); + Controls.Add(buttonCreateModify); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonLeft); @@ -172,5 +196,7 @@ private Button buttonLeft; private Button buttonRight; private Button buttonDown; + private Button buttonCreateModify; + private Button ButtonSelectLocomotive; } } \ No newline at end of file diff --git a/Monorail/Monorail/FormLocomotive.cs b/Monorail/Monorail/FormLocomotive.cs index 255a672..44aa008 100644 --- a/Monorail/Monorail/FormLocomotive.cs +++ b/Monorail/Monorail/FormLocomotive.cs @@ -13,19 +13,11 @@ namespace Monorail public partial class FormLocomotive : Form { private DrawingLocomotive _locomotive; + public DrawingLocomotive SelectedLocomotive { get; private set; } public FormLocomotive() { InitializeComponent(); } - - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - _locomotive = new DrawingLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(); - Draw(); - } - private void Draw() { Bitmap bmp = new(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); @@ -33,16 +25,31 @@ namespace Monorail _locomotive?.DrawTransport(gr); pictureBoxLocomotive.Image = bmp; } - /// - /// "" - /// - /// - /// - /// - /// - /// - /// - /// + private void SetData() + { + Random rnd = new(); + _locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), + pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); + toolStripStatusLabelSpeed.Text = $": {_locomotive.Locomotive.Speed}"; + toolStripStatusLabelWeight.Text = $": {_locomotive.Locomotive.Weight}"; + toolStripStatusLabelBodyColor.Text = $": {_locomotive.Locomotive.BodyColor.Name} "; + } + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _locomotive = new DrawingLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), + color); + SetData(); + Draw(); + } + private void ButtonMove_Click(object sender, EventArgs e) { // @@ -64,25 +71,40 @@ namespace Monorail } Draw(); } - /// - /// - /// - /// - /// + private void PictureBoxLocomotive_Resize(object sender, EventArgs e) { _locomotive?.ChangeBorders(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); Draw(); } - private void SetData() + private void ButtonCreateModify_Click(object sender, EventArgs e) { Random rnd = new(); - _locomotive.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxLocomotive.Width, pictureBoxLocomotive.Height); - toolStripStatusLabelSpeed.Text = $": {_locomotive.Locomotive.Speed}"; - toolStripStatusLabelWeight.Text = $": {_locomotive.Locomotive.Weight}"; - toolStripStatusLabelBodyColor.Text = $": {_locomotive.Locomotive.BodyColor.Name}"; + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), + rnd.Next(0, 256)); + ColorDialog dialogDop = new(); + if (dialogDop.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDop.Color; + } + _locomotive = new DrawingMonorailLocomotive(rnd.Next(100, 300), rnd.Next(1000, 2000), + color, dopColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + SetData(); + Draw(); } + private void ButtonSelectLocomotive_Click(object sender, EventArgs e) + { + SelectedLocomotive = _locomotive; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/Monorail/Monorail/FormMap.Designer.cs b/Monorail/Monorail/FormMap.Designer.cs index 80ec9b9..4886524 100644 --- a/Monorail/Monorail/FormMap.Designer.cs +++ b/Monorail/Monorail/FormMap.Designer.cs @@ -154,7 +154,7 @@ // comboBoxSelectorMap.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorMap.FormattingEnabled = true; - comboBoxSelectorMap.Items.AddRange(new object[] { "SimpleMap" }); + comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Поле с грязью", "Кусты на карте" }); comboBoxSelectorMap.Location = new Point(12, 12); comboBoxSelectorMap.Name = "comboBoxSelectorMap"; comboBoxSelectorMap.Size = new Size(121, 23); diff --git a/Monorail/Monorail/FormMap.cs b/Monorail/Monorail/FormMap.cs index 91bb925..61b09fd 100644 --- a/Monorail/Monorail/FormMap.cs +++ b/Monorail/Monorail/FormMap.cs @@ -91,6 +91,12 @@ namespace Monorail case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Поле с грязью": + _abstractMap = new FieldMap(); + break; + case "Кусты на карте": + _abstractMap = new BushesMap(); + break; } } diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs index 565cc16..0a86260 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs @@ -55,11 +55,14 @@ // // comboBoxSelectorMap // + comboBoxSelectorMap.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorMap.FormattingEnabled = true; + comboBoxSelectorMap.Items.AddRange(new object[] { "Простая карта", "Карта с грязью", "Карта с кустами" }); comboBoxSelectorMap.Location = new Point(26, 22); comboBoxSelectorMap.Name = "comboBoxSelectorMap"; comboBoxSelectorMap.Size = new Size(168, 23); comboBoxSelectorMap.TabIndex = 2; + comboBoxSelectorMap.SelectedIndexChanged += ComboBoxSelectorMap_SelectedIndexChanged; // // buttonAddCar // @@ -67,8 +70,9 @@ buttonAddCar.Name = "buttonAddCar"; buttonAddCar.Size = new Size(168, 28); buttonAddCar.TabIndex = 3; - buttonAddCar.Text = "Добавить автомобиль"; + buttonAddCar.Text = "Добавить локомотив"; buttonAddCar.UseVisualStyleBackColor = true; + buttonAddCar.Click += ButtonAddLocomotive_Click; // // buttonRemoveCar // @@ -76,8 +80,9 @@ buttonRemoveCar.Name = "buttonRemoveCar"; buttonRemoveCar.Size = new Size(169, 30); buttonRemoveCar.TabIndex = 4; - buttonRemoveCar.Text = "Удалить автомобиль"; + buttonRemoveCar.Text = "Удалить локомотив"; buttonRemoveCar.UseVisualStyleBackColor = true; + buttonRemoveCar.Click += ButtonRemoveLocomotive_Click; // // buttonShowStorage // @@ -87,6 +92,7 @@ buttonShowStorage.TabIndex = 5; buttonShowStorage.Text = "Посмотреть хранилище"; buttonShowStorage.UseVisualStyleBackColor = true; + buttonShowStorage.Click += ButtonShowStorage_Click; // // buttonShowOnMap // @@ -96,6 +102,7 @@ buttonShowOnMap.TabIndex = 6; buttonShowOnMap.Text = "Посмотреть карту"; buttonShowOnMap.UseVisualStyleBackColor = true; + buttonShowOnMap.Click += ButtonShowOnMap_Click; // // maskedTextBoxPosition // @@ -113,6 +120,7 @@ buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 8; buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += ButtonMove_Click; // // buttonLeft // @@ -123,6 +131,7 @@ buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 9; buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += ButtonMove_Click; // // buttonDown // @@ -133,6 +142,7 @@ buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 10; buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += ButtonMove_Click; // // buttonRight // @@ -143,6 +153,7 @@ buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 11; buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += ButtonMove_Click; // // groupBoxTools // diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.cs b/Monorail/Monorail/FormMapWithSetLocomotive.cs index 2f7f69a..e174b78 100644 --- a/Monorail/Monorail/FormMapWithSetLocomotive.cs +++ b/Monorail/Monorail/FormMapWithSetLocomotive.cs @@ -12,9 +12,120 @@ namespace Monorail { public partial class FormMapWithSetLocomotive : Form { + private MapWithSetLocomotiveGeneric _mapLocomotivesCollectionGeneric; public FormMapWithSetLocomotive() { InitializeComponent(); } + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, + EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Карта с грязью": + map = new FieldMap(); + break; + case "Карта с кустами": + map = new BushesMap(); + break; + } + if (map != null) + { + _mapLocomotivesCollectionGeneric = new MapWithSetLocomotiveGeneric(pictureBoxLocomotive.Width, pictureBoxLocomotive.Height, map); + } + else + { + _mapLocomotivesCollectionGeneric = null; + } + } + private void ButtonAddLocomotive_Click(object sender, EventArgs e) + { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + FormLocomotive form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectLocomotive Locomotive = new(form.SelectedLocomotive); + if (_mapLocomotivesCollectionGeneric + Locomotive != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxLocomotive.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapLocomotivesCollectionGeneric - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxLocomotive.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + pictureBoxLocomotive.Image = _mapLocomotivesCollectionGeneric.ShowSet(); + } + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + pictureBoxLocomotive.Image = _mapLocomotivesCollectionGeneric.ShowOnMap(); + } + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBoxLocomotive.Image = _mapLocomotivesCollectionGeneric.MoveObject(dir); + } } } diff --git a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs index d5e684f..0331943 100644 --- a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs +++ b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Monorail { - internal class MapWithSetLocomotivGeneric + internal class MapWithSetLocomotiveGeneric where T : class, IDrawingObject where U : AbstractMap { @@ -25,7 +25,7 @@ where T : class, IDrawingObject private readonly T[] _places; // Конструктор - public MapWithSetLocomotivGeneric(int picWidth, int picHeight, U map) + public MapWithSetLocomotiveGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; @@ -35,12 +35,12 @@ where T : class, IDrawingObject _map = map; } // Перегрузка оператора сложения - public static int operator +(MapWithSetLocomotivGeneric map, T Locomotive) + public static int operator +(MapWithSetLocomotiveGeneric map, T Locomotive) { return map._setLocomotive.Insert(Locomotive); } // Перегрузка оператора вычитания - public static T operator -(MapWithSetLocomotivGeneric map, int position) + public static T operator -(MapWithSetLocomotiveGeneric map, int position) { return map._setLocomotive.Remove(position); diff --git a/Monorail/Monorail/Program.cs b/Monorail/Monorail/Program.cs index e71a664..344f9de 100644 --- a/Monorail/Monorail/Program.cs +++ b/Monorail/Monorail/Program.cs @@ -11,7 +11,7 @@ namespace Monorail // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetLocomotive()); } } } \ No newline at end of file