Potapov N.S. LabWork03 #5

Closed
ns.potapov wants to merge 2 commits from LabWork03 into LabWork02
12 changed files with 766 additions and 291 deletions

View File

@ -9,7 +9,7 @@ namespace Boats
/// <summary>
/// Направление перемещения
/// </summary>
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,

View File

@ -9,7 +9,7 @@ namespace Boats
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawingBoat
public class DrawingBoat
{
/// <summary>
/// Класс-сущность

View File

@ -9,7 +9,7 @@ namespace Boats
/// <summary>
/// Класс-сущность "Лодка"
/// </summary>
internal class EntityBoat
public class EntityBoat
{
/// <summary>
/// Скорость

View File

@ -39,6 +39,7 @@
this.ButtonRight = new System.Windows.Forms.Button();
this.ButtonDown = new System.Windows.Forms.Button();
this.ButtonCreateModificate = new System.Windows.Forms.Button();
this.ButtonSelectBoat = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.statusStrip.SuspendLayout();
this.SuspendLayout();
@ -155,11 +156,23 @@
this.ButtonCreateModificate.UseVisualStyleBackColor = true;
this.ButtonCreateModificate.Click += new System.EventHandler(this.ButtonCreateModificate_Click);
//
// ButtonSelectBoat
//
this.ButtonSelectBoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonSelectBoat.Location = new System.Drawing.Point(491, 381);
this.ButtonSelectBoat.Name = "ButtonSelectBoat";
this.ButtonSelectBoat.Size = new System.Drawing.Size(117, 29);
this.ButtonSelectBoat.TabIndex = 8;
this.ButtonSelectBoat.Text = "Выбрать";
this.ButtonSelectBoat.UseVisualStyleBackColor = true;
this.ButtonSelectBoat.Click += new System.EventHandler(this.ButtonSelectBoat_Click);
//
// FormBoat
//
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.ButtonSelectBoat);
this.Controls.Add(this.ButtonCreateModificate);
this.Controls.Add(this.ButtonDown);
this.Controls.Add(this.ButtonRight);
@ -191,5 +204,6 @@
private Button ButtonRight;
private Button ButtonDown;
private Button ButtonCreateModificate;
private Button ButtonSelectBoat;
}
}

View File

@ -12,7 +12,8 @@ namespace Boats
{
public partial class FormBoat : Form
{
private DrawingBoat _boat;
DrawingBoat _boat;
public DrawingBoat SelectedBoat { get; private set; }
public FormBoat()
{
InitializeComponent();
@ -44,18 +45,21 @@ namespace Boats
pictureBoxBoat.Image = bmp;
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// Обработчик нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
Random rnd = new Random();
Color color = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
ColorDialog dialog = new ColorDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_boat = new DrawingBoat(
rnd.Next(100, 300),
rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256),
rnd.Next(0, 256), rnd.Next(0, 256))
rnd.Next(1000, 3000),
color
);
SetData();
Draw();
@ -67,21 +71,34 @@ namespace Boats
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
if (_boat == null)
return;
string btnName = ((Button)sender).Name;
switch (btnName)
{
case "ButtonUp":
_boat?.MoveTransport(Direction.Up);
{
_boat.MoveTransport(Direction.Up);
}
break;
case "ButtonDown":
_boat?.MoveTransport(Direction.Down);
{
_boat.MoveTransport(Direction.Down);
}
break;
case "ButtonLeft":
_boat?.MoveTransport(Direction.Left);
{
_boat.MoveTransport(Direction.Left);
}
break;
case "ButtonRight":
_boat?.MoveTransport(Direction.Right);
{
_boat.MoveTransport(Direction.Right);
}
break;
default:
break;
}
Draw();
@ -104,16 +121,44 @@ namespace Boats
private void ButtonCreateModificate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
// Предлагаем установить свой основной цвет
Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
ColorDialog dialog = new ColorDialog();
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;
}
_boat = new DrawingCatamaran(
rnd.Next(100, 300),
rnd.Next(1000, 3000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)),
color,
dopColor,
Convert.ToBoolean(rnd.Next(0, 2)),
Convert.ToBoolean(rnd.Next(0, 2))
);
SetData();
Draw();
}
/// <summary>
/// Обработка нажатия кнопки "Выбрать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSelectBoat_Click(object sender, EventArgs e)
{
SelectedBoat = _boat;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -1,126 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Boats
{
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
/// <summary>
/// Метод установки данных
/// </summary>
private void SetData(DrawingBoat boat)
{
toolStripStatusLabelSpeed.Text = $"Скорость: {boat.Boat.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {boat.Boat.Weight}";
toolStripStatusLabelBodyColor.Text = $"Цвет: {boat.Boat.BodyColor.Name}";
pictureBoxMap.Image = _abstractMap.CreateMap(pictureBoxMap.Width, pictureBoxMap.Height,
new DrawingObjectBoat(boat));
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random rnd = new();
var boat = new DrawingBoat(
rnd.Next(100, 300),
rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256),
rnd.Next(0, 256), rnd.Next(0, 256))
);
SetData(boat);
}
/// <summary>
/// Обработчик нажатий кнопок передвижения
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
//получаем имя кнопки
string btnName = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (btnName)
{
case "ButtonUp":
{
dir = Direction.Up;
}
break;
case "ButtonDown":
{
dir = Direction.Down;
}
break;
case "ButtonLeft":
{
dir = Direction.Left;
}
break;
case "ButtonRight":
{
dir = Direction.Right;
}
break;
default:
break;
}
pictureBoxMap.Image = _abstractMap?.MoveObject(dir);
}
/// <summary>
/// Обработка нажатия кнопки "Модификация"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreateModificate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
var boat = new DrawingCatamaran(
rnd.Next(100, 300),
rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)),
Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)),
Convert.ToBoolean(rnd.Next(0, 2)),
Convert.ToBoolean(rnd.Next(0, 2))
);
SetData(boat);
}
/// <summary>
/// Смена карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Океан карта":
_abstractMap = new OceanMap();
break;
case "Линии карта":
_abstractMap = new LineMap();
break;
}
}
}
}

View File

@ -1,6 +1,6 @@
namespace Boats
{
partial class FormMap
partial class FormMapWithSetBoats
{
/// <summary>
/// Required designer variable.
@ -28,132 +28,137 @@
/// </summary>
private void InitializeComponent()
{
this.pictureBoxMap = new System.Windows.Forms.PictureBox();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabelBodyColor = 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();
this.ButtonRight = new System.Windows.Forms.Button();
this.groupBox = new System.Windows.Forms.GroupBox();
this.ButtonDown = new System.Windows.Forms.Button();
this.ButtonCreateModificate = new System.Windows.Forms.Button();
this.ButtonRight = new System.Windows.Forms.Button();
this.ButtonLeft = new System.Windows.Forms.Button();
this.ButtonUp = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.ButtonShowOnMap = new System.Windows.Forms.Button();
this.ButtonShowStorage = new System.Windows.Forms.Button();
this.ButtonRemoveBoat = new System.Windows.Forms.Button();
this.ButtonAddBoat = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxMap)).BeginInit();
this.statusStrip.SuspendLayout();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// pictureBoxMap
// groupBox
//
this.pictureBoxMap.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxMap.Location = new System.Drawing.Point(0, 0);
this.pictureBoxMap.Name = "pictureBoxMap";
this.pictureBoxMap.Size = new System.Drawing.Size(800, 424);
this.pictureBoxMap.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxMap.TabIndex = 0;
this.pictureBoxMap.TabStop = false;
//
// statusStrip
//
this.statusStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabelSpeed,
this.toolStripStatusLabelWeight,
this.toolStripStatusLabelBodyColor});
this.statusStrip.Location = new System.Drawing.Point(0, 424);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(800, 26);
this.statusStrip.TabIndex = 1;
this.statusStrip.Text = "statusStrip1";
//
// toolStripStatusLabelSpeed
//
this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed";
this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(76, 20);
this.toolStripStatusLabelSpeed.Text = "Скорость:";
//
// toolStripStatusLabelWeight
//
this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight";
this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(36, 20);
this.toolStripStatusLabelWeight.Text = "Вес:";
//
// toolStripStatusLabelBodyColor
//
this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor";
this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(45, 20);
this.toolStripStatusLabelBodyColor.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(12, 381);
this.ButtonCreate.Name = "ButtonCreate";
this.ButtonCreate.Size = new System.Drawing.Size(94, 29);
this.ButtonCreate.TabIndex = 2;
this.ButtonCreate.Text = "Создать";
this.ButtonCreate.UseVisualStyleBackColor = true;
this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click);
//
// ButtonUp
//
this.ButtonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonUp.BackgroundImage = global::Boats.Properties.Resources.arrow_up;
this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonUp.Location = new System.Drawing.Point(710, 344);
this.ButtonUp.Name = "ButtonUp";
this.ButtonUp.Size = new System.Drawing.Size(30, 30);
this.ButtonUp.TabIndex = 3;
this.ButtonUp.UseVisualStyleBackColor = true;
this.ButtonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// ButtonLeft
//
this.ButtonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonLeft.BackgroundImage = global::Boats.Properties.Resources.arrow_left;
this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonLeft.Location = new System.Drawing.Point(674, 380);
this.ButtonLeft.Name = "ButtonLeft";
this.ButtonLeft.Size = new System.Drawing.Size(30, 30);
this.ButtonLeft.TabIndex = 4;
this.ButtonLeft.UseVisualStyleBackColor = true;
this.ButtonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// ButtonRight
//
this.ButtonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRight.BackgroundImage = global::Boats.Properties.Resources.arrow_right;
this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonRight.Location = new System.Drawing.Point(746, 380);
this.ButtonRight.Name = "ButtonRight";
this.ButtonRight.Size = new System.Drawing.Size(30, 30);
this.ButtonRight.TabIndex = 5;
this.ButtonRight.UseVisualStyleBackColor = true;
this.ButtonRight.Click += new System.EventHandler(this.ButtonMove_Click);
this.groupBox.Controls.Add(this.ButtonDown);
this.groupBox.Controls.Add(this.ButtonRight);
this.groupBox.Controls.Add(this.ButtonLeft);
this.groupBox.Controls.Add(this.ButtonUp);
this.groupBox.Controls.Add(this.maskedTextBoxPosition);
this.groupBox.Controls.Add(this.ButtonShowOnMap);
this.groupBox.Controls.Add(this.ButtonShowStorage);
this.groupBox.Controls.Add(this.ButtonRemoveBoat);
this.groupBox.Controls.Add(this.ButtonAddBoat);
this.groupBox.Controls.Add(this.comboBoxSelectorMap);
this.groupBox.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox.Location = new System.Drawing.Point(901, 0);
this.groupBox.Name = "groupBox";
this.groupBox.Size = new System.Drawing.Size(250, 589);
this.groupBox.TabIndex = 0;
this.groupBox.TabStop = false;
this.groupBox.Text = "Инструменты";
//
// ButtonDown
//
this.ButtonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonDown.BackgroundImage = global::Boats.Properties.Resources.arrow_down;
this.ButtonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonDown.Location = new System.Drawing.Point(710, 380);
this.ButtonDown.Location = new System.Drawing.Point(99, 537);
this.ButtonDown.Name = "ButtonDown";
this.ButtonDown.Size = new System.Drawing.Size(30, 30);
this.ButtonDown.TabIndex = 6;
this.ButtonDown.TabIndex = 10;
this.ButtonDown.UseVisualStyleBackColor = true;
this.ButtonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// ButtonCreateModificate
// ButtonRight
//
this.ButtonCreateModificate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.ButtonCreateModificate.Location = new System.Drawing.Point(138, 381);
this.ButtonCreateModificate.Name = "ButtonCreateModificate";
this.ButtonCreateModificate.Size = new System.Drawing.Size(117, 29);
this.ButtonCreateModificate.TabIndex = 7;
this.ButtonCreateModificate.Text = "Модификация";
this.ButtonCreateModificate.UseVisualStyleBackColor = true;
this.ButtonCreateModificate.Click += new System.EventHandler(this.ButtonCreateModificate_Click);
this.ButtonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRight.BackgroundImage = global::Boats.Properties.Resources.arrow_right;
this.ButtonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonRight.Location = new System.Drawing.Point(135, 537);
this.ButtonRight.Name = "ButtonRight";
this.ButtonRight.Size = new System.Drawing.Size(30, 30);
this.ButtonRight.TabIndex = 9;
this.ButtonRight.UseVisualStyleBackColor = true;
this.ButtonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// ButtonLeft
//
this.ButtonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonLeft.BackgroundImage = global::Boats.Properties.Resources.arrow_left;
this.ButtonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonLeft.Location = new System.Drawing.Point(63, 537);
this.ButtonLeft.Name = "ButtonLeft";
this.ButtonLeft.Size = new System.Drawing.Size(30, 30);
this.ButtonLeft.TabIndex = 8;
this.ButtonLeft.UseVisualStyleBackColor = true;
this.ButtonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// ButtonUp
//
this.ButtonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonUp.BackgroundImage = global::Boats.Properties.Resources.arrow_up;
this.ButtonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ButtonUp.Location = new System.Drawing.Point(99, 501);
this.ButtonUp.Name = "ButtonUp";
this.ButtonUp.Size = new System.Drawing.Size(30, 30);
this.ButtonUp.TabIndex = 7;
this.ButtonUp.UseVisualStyleBackColor = true;
this.ButtonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 168);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27);
this.maskedTextBoxPosition.TabIndex = 5;
//
// ButtonShowOnMap
//
this.ButtonShowOnMap.Location = new System.Drawing.Point(6, 389);
this.ButtonShowOnMap.Name = "ButtonShowOnMap";
this.ButtonShowOnMap.Size = new System.Drawing.Size(232, 40);
this.ButtonShowOnMap.TabIndex = 4;
this.ButtonShowOnMap.Text = "Просмотреть карту";
this.ButtonShowOnMap.UseVisualStyleBackColor = true;
this.ButtonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// ButtonShowStorage
//
this.ButtonShowStorage.Location = new System.Drawing.Point(6, 303);
this.ButtonShowStorage.Name = "ButtonShowStorage";
this.ButtonShowStorage.Size = new System.Drawing.Size(232, 40);
this.ButtonShowStorage.TabIndex = 3;
this.ButtonShowStorage.Text = "Просмотреть хранилище";
this.ButtonShowStorage.UseVisualStyleBackColor = true;
this.ButtonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// ButtonRemoveBoat
//
this.ButtonRemoveBoat.Location = new System.Drawing.Point(6, 212);
this.ButtonRemoveBoat.Name = "ButtonRemoveBoat";
this.ButtonRemoveBoat.Size = new System.Drawing.Size(232, 40);
this.ButtonRemoveBoat.TabIndex = 2;
this.ButtonRemoveBoat.Text = "Удалить лодку";
this.ButtonRemoveBoat.UseVisualStyleBackColor = true;
this.ButtonRemoveBoat.Click += new System.EventHandler(this.ButtonRemoveBoat_Click);
//
// ButtonAddBoat
//
this.ButtonAddBoat.Location = new System.Drawing.Point(6, 97);
this.ButtonAddBoat.Name = "ButtonAddBoat";
this.ButtonAddBoat.Size = new System.Drawing.Size(232, 40);
this.ButtonAddBoat.TabIndex = 1;
this.ButtonAddBoat.Text = "Добавить лодку";
this.ButtonAddBoat.UseVisualStyleBackColor = true;
this.ButtonAddBoat.Click += new System.EventHandler(this.ButtonAddBoat_Click);
//
// comboBoxSelectorMap
//
@ -163,49 +168,50 @@
"Простая карта",
"Океан карта",
"Линии карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 26);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 28);
this.comboBoxSelectorMap.TabIndex = 8;
this.comboBoxSelectorMap.Size = new System.Drawing.Size(238, 28);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// FormMap
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(901, 589);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetBoats
//
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.comboBoxSelectorMap);
this.Controls.Add(this.ButtonCreateModificate);
this.Controls.Add(this.ButtonDown);
this.Controls.Add(this.ButtonRight);
this.Controls.Add(this.ButtonLeft);
this.Controls.Add(this.ButtonUp);
this.Controls.Add(this.ButtonCreate);
this.Controls.Add(this.pictureBoxMap);
this.Controls.Add(this.statusStrip);
this.Name = "FormMap";
this.Text = "Лодка";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxMap)).EndInit();
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.ClientSize = new System.Drawing.Size(1151, 589);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox);
this.Name = "FormMapWithSetBoats";
this.Text = "Карта с набором элементов";
this.groupBox.ResumeLayout(false);
this.groupBox.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private PictureBox pictureBoxMap;
private StatusStrip statusStrip;
private ToolStripStatusLabel toolStripStatusLabelSpeed;
private ToolStripStatusLabel toolStripStatusLabelWeight;
private ToolStripStatusLabel toolStripStatusLabelBodyColor;
private Button ButtonCreate;
private Button ButtonUp;
private Button ButtonLeft;
private Button ButtonRight;
private Button ButtonDown;
private Button ButtonCreateModificate;
private GroupBox groupBox;
private MaskedTextBox maskedTextBoxPosition;
private Button ButtonShowOnMap;
private Button ButtonShowStorage;
private Button ButtonRemoveBoat;
private Button ButtonAddBoat;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox;
private Button ButtonDown;
private Button ButtonRight;
private Button ButtonLeft;
private Button ButtonUp;
}
}

View File

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Boats
{
public partial class FormMapWithSetBoats : Form
{
/// <summary>
/// Объект от класса карты с набором объектов
/// </summary>
private MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
/// <summary>
/// Конструктор
/// </summary>
public FormMapWithSetBoats()
{
InitializeComponent();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Линии карта":
map = new LineMap();
break;
case "Океан карта":
map = new OceanMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddBoat_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
FormBoat form = new();
if (form.ShowDialog() == DialogResult.OK)
{
bool added = false;
if (form.SelectedBoat != null)
{
DrawingObjectBoat boat = new(form.SelectedBoat);
if (_mapBoatsCollectionGeneric + boat != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
added = true;
}
}
if (!added)
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveBoat_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);
pos -= 1;
if (_mapBoatsCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == 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;
}
pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -57,7 +57,4 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,245 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Boats
{
/// <summary>
/// Карта с набром объектов
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetBoatsGeneric<T, U>
where T : class, IDrawingObject
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 130;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 80;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetBoatsGeneric<T> _setBoats;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Массив точек установки лодок в гавани
/// </summary>
private Point[]? _placesPoints;
private readonly int _placesCount = 14;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(_placesCount);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
_placesPoints = null;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="boat"></param>
/// <returns>Возвращает позицию объекта в массиве или
/// -1, если установить объект не удплось</returns>
public static int operator +(MapWithSetBoatsGeneric<T, U> map, T boat)
{
return map._setBoats.Insert(boat);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns>Возвращает удаляемый объект или
/// null, если удалить не удалось</returns>
public static T operator -(MapWithSetBoatsGeneric<T, U> map, int position)
{
return map._setBoats.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns>Возвращает Bitmap с гаванью и лодками</returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics g = Graphics.FromImage(bmp);
DrawBackground(g);
DrawBoats(g);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns>Возвращает Bitmap с картой и объектом на ней</returns>
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBoats.Count; i++)
{
var boat = _setBoats.Get(i);
if (boat != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Перемещение объекта по карте
/// </summary>
/// <param name="direction"></param>
/// <returns>Возвращает Bitmap с картой и перемещенным объектом на ней</returns>
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setBoats.Count - 1;
for (int i = 0; i < _setBoats.Count; i++)
{
if (_setBoats.Get(i) == null)
{
for (; j > i; j--)
{
var boat = _setBoats.Get(j);
if (boat != null)
{
_setBoats.Insert(boat, i);
_setBoats.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
bool pointsInit = false;
// Если массив точек null, значит рисуем фон первый раз и
// инициализируем массив для его заполнения
if (_placesPoints == null)
{
_placesPoints = new Point[_placesCount];
pointsInit = true;
}
// рисуем фон
g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth * _placeSizeWidth,
_pictureHeight * _placeSizeHeight);
// рисуем основной пирс
g.FillRectangle(Brushes.Gray, 0, 0, _placeSizeWidth * 5 / 4, _placeSizeHeight * 3 / 2);
g.FillRectangle(Brushes.Gray, _pictureWidth - _placeSizeWidth * 5 / 4, 0,
_placeSizeWidth * 5 / 4, _placeSizeHeight * 3 / 2);
g.FillRectangle(Brushes.Gray, 0, _placeSizeHeight * 3 / 2,
_placeSizeWidth * 1 / 4, _pictureHeight - _placeSizeHeight * 3 / 2);
g.FillRectangle(Brushes.Gray, _pictureWidth - _placeSizeWidth * 1 / 4, _placeSizeHeight * 3 / 2,
_placeSizeWidth * 1 / 4, _pictureHeight - _placeSizeHeight * 3 / 2);
g.FillRectangle(Brushes.Gray, 0, 0, _pictureWidth, _placeSizeHeight * 1 / 2);
// рисуем плавучие пирсы
// горизонтальные
int w = _placeSizeWidth;
int h = _placeSizeHeight;
int x = w * 1 / 4;
int y = h * 5 / 2;
int pirsSize = h * 1 / 6;
int i = 0;
while (y + h + pirsSize < _pictureHeight)
{
g.FillRectangle(Brushes.Brown, x, y, w, pirsSize);
g.FillRectangle(Brushes.Brown, _pictureWidth - x - w, y, w, pirsSize);
if (pointsInit)
{
_placesPoints[9 + i] = new Point(x + 5, y - _placeSizeHeight + 5);
_placesPoints[4 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5);
}
y += h + pirsSize;
i++;
}
if (pointsInit)
{
_placesPoints[9 + i] = new Point(x + 5, y - _placeSizeHeight + 5);
_placesPoints[4 - i] = new Point(_pictureWidth - x - w + 5, y - _placeSizeHeight + 5);
}
// вертикальные
x = _placeSizeWidth * 5 / 4 + w;
y = _placeSizeHeight * 1 / 2;
h = _placeSizeHeight;
i = 0;
while (x + w + pirsSize < _pictureWidth - _placeSizeWidth * 5 / 4)
{
g.FillRectangle(Brushes.Brown, x, y, pirsSize, h);
if (pointsInit)
{
_placesPoints[8 - i] = new Point(x - w + 5, y + 5);
}
x += w + pirsSize;
i++;
}
if (pointsInit)
{
_placesPoints[8 - i] = new Point(x - w + 5, y + 5);
}
}
/// <summary>
/// Метод отрисовки лодок
/// </summary>
/// <param name="g"></param>
private void DrawBoats(Graphics g)
{
for (int i = 0; i < _setBoats.Count; i++)
{
// Установка позиции
_setBoats.Get(i)?.SetObject(_placesPoints[i].X, _placesPoints[i].Y,
_pictureWidth, _pictureHeight);
_setBoats.Get(i)?.DrawingObject(g);
}
}
}
}

View File

@ -11,7 +11,7 @@ namespace Boats
// 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 FormMapWithSetBoats());
}
}
}

View File

@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Boats
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetBoatsGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetBoatsGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <returns></returns>
public int Insert(T boat)
{
// Вставка в начало набора
return Insert(boat, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="boat">Добавляемая лодка</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T boat, int position)
{
// Проверка позиции
if (position < 0 || position >= _places.Length)
return -1;
// Проверка, что элемент массива по этой позиции пустой
if (_places[position] != null)
{
// Если нет, проверим, что после вставляемого элемента в массиве есть пустой элемент
int i = position + 1;
int nullIndex = -1;
while (i < _places.Length)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
i++;
}
// Если свободной нет, то выходим
if (nullIndex < 0)
{
return -1;
}
else
{
// Если есть, сдвигаем все объекты, находящиеся
// справа от позиции до первого пустого элемента
i = nullIndex - 1;
while (i >= position)
{
_places[i + 1] = _places[i];
i--;
}
}
}
// Вставка по позиции
_places[position] = boat;
return position;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns>Возвращает удаляемый объект или null, если не удалось удалить</returns>
public T Remove(int position)
{
// Проверка позиции
if (position < 0 || position >= _places.Length)
return null;
if (_places[position] == null)
{
return null;
}
// Удаление объекта из массива, присовив элементу массива значение null
T boat = _places[position];
_places[position] = null;
return boat;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns>Возвращает объект по позиции</returns>
public T Get(int position)
{
// Проверка позиции
if (position < 0 || position >= _places.Length)
return null;
return _places[position];
}
}
}