This commit is contained in:
Valitov.Damir 2023-04-14 14:36:46 +04:00
parent 95676d3037
commit b00edd8ae9
14 changed files with 631 additions and 318 deletions

View File

@ -3,7 +3,7 @@ using System.Drawing;
namespace Sailboat
{
internal class Boat
public class Boat
{
/// <summary>
/// Скорость

View File

@ -40,6 +40,7 @@ namespace Sailboat
this.btn_up = new System.Windows.Forms.Button();
this.btn_left = new System.Windows.Forms.Button();
this.btn_create_sailboat = new System.Windows.Forms.Button();
this.btn_select = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.SuspendLayout();
@ -155,11 +156,22 @@ namespace Sailboat
this.btn_create_sailboat.UseVisualStyleBackColor = true;
this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click);
//
// btn_select
//
this.btn_select.Location = new System.Drawing.Point(13, 391);
this.btn_select.Name = "btn_select";
this.btn_select.Size = new System.Drawing.Size(94, 29);
this.btn_select.TabIndex = 9;
this.btn_select.Text = "Выбрать";
this.btn_select.UseVisualStyleBackColor = true;
this.btn_select.Click += new System.EventHandler(this.btn_select_Click);
//
// BoatForm
//
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.btn_select);
this.Controls.Add(this.btn_create_sailboat);
this.Controls.Add(this.btn_left);
this.Controls.Add(this.btn_up);
@ -193,6 +205,7 @@ namespace Sailboat
private System.Windows.Forms.Button btn_up;
private System.Windows.Forms.Button btn_left;
private System.Windows.Forms.Button btn_create_sailboat;
private System.Windows.Forms.Button btn_select;
}
}

View File

@ -13,6 +13,10 @@ namespace Sailboat
public partial class BoatForm : Form
{
private DrawingBoat _boat;
/// <summary>
/// Выбранный объект
/// </summary>
public DrawingBoat SelectedBoat { get; private set; }
public BoatForm()
{
InitializeComponent();
@ -94,5 +98,11 @@ namespace Sailboat
toolStripLabel_weight.Text = $"Вес: {_boat.Boat.Weight}";
toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}";
}
private void btn_select_Click(object sender, EventArgs e)
{
SelectedBoat = _boat;
DialogResult = DialogResult.OK;
}
}
}

View File

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

View File

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

View File

@ -1,211 +0,0 @@

namespace Sailboat
{
partial class FormMap
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripLabel_speed = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripLabel_weight = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripLabel_color = new System.Windows.Forms.ToolStripStatusLabel();
this.pictureBoxBoat = new System.Windows.Forms.PictureBox();
this.btn_create = new System.Windows.Forms.Button();
this.btn_right = new System.Windows.Forms.Button();
this.btn_down = new System.Windows.Forms.Button();
this.btn_up = new System.Windows.Forms.Button();
this.btn_left = new System.Windows.Forms.Button();
this.btn_create_sailboat = new System.Windows.Forms.Button();
this.comboBoxMapSelector = new System.Windows.Forms.ComboBox();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripLabel_speed,
this.toolStripLabel_weight,
this.toolStripLabel_color});
this.statusStrip1.Location = new System.Drawing.Point(0, 424);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 26);
this.statusStrip1.TabIndex = 0;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripLabel_speed
//
this.toolStripLabel_speed.Name = "toolStripLabel_speed";
this.toolStripLabel_speed.Size = new System.Drawing.Size(76, 20);
this.toolStripLabel_speed.Text = "Скорость:";
//
// toolStripLabel_weight
//
this.toolStripLabel_weight.Name = "toolStripLabel_weight";
this.toolStripLabel_weight.Size = new System.Drawing.Size(36, 20);
this.toolStripLabel_weight.Text = "Вес:";
//
// toolStripLabel_color
//
this.toolStripLabel_color.Name = "toolStripLabel_color";
this.toolStripLabel_color.Size = new System.Drawing.Size(45, 20);
this.toolStripLabel_color.Text = "Цвет:";
//
// pictureBoxBoat
//
this.pictureBoxBoat.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxBoat.Location = new System.Drawing.Point(0, 0);
this.pictureBoxBoat.Name = "pictureBoxBoat";
this.pictureBoxBoat.Size = new System.Drawing.Size(800, 424);
this.pictureBoxBoat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxBoat.TabIndex = 1;
this.pictureBoxBoat.TabStop = false;
this.pictureBoxBoat.Resize += new System.EventHandler(this.btn_move_Click);
//
// btn_create
//
this.btn_create.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_create.Location = new System.Drawing.Point(13, 13);
this.btn_create.Name = "btn_create";
this.btn_create.Size = new System.Drawing.Size(94, 29);
this.btn_create.TabIndex = 2;
this.btn_create.Text = "Создать";
this.btn_create.UseVisualStyleBackColor = true;
this.btn_create.Click += new System.EventHandler(this.btn_create_Click);
//
// btn_right
//
this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right;
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_right.Location = new System.Drawing.Point(694, 392);
this.btn_right.Name = "btn_right";
this.btn_right.Size = new System.Drawing.Size(94, 29);
this.btn_right.TabIndex = 4;
this.btn_right.UseVisualStyleBackColor = true;
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_down
//
this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down;
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_down.Location = new System.Drawing.Point(594, 392);
this.btn_down.Name = "btn_down";
this.btn_down.Size = new System.Drawing.Size(94, 29);
this.btn_down.TabIndex = 5;
this.btn_down.UseVisualStyleBackColor = true;
this.btn_down.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_up
//
this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up;
this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_up.Location = new System.Drawing.Point(594, 357);
this.btn_up.Name = "btn_up";
this.btn_up.Size = new System.Drawing.Size(94, 29);
this.btn_up.TabIndex = 6;
this.btn_up.UseVisualStyleBackColor = true;
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_left
//
this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left;
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_left.Location = new System.Drawing.Point(494, 391);
this.btn_left.Name = "btn_left";
this.btn_left.Size = new System.Drawing.Size(94, 29);
this.btn_left.TabIndex = 7;
this.btn_left.UseVisualStyleBackColor = true;
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_create_sailboat
//
this.btn_create_sailboat.Location = new System.Drawing.Point(113, 13);
this.btn_create_sailboat.Name = "btn_create_sailboat";
this.btn_create_sailboat.Size = new System.Drawing.Size(150, 29);
this.btn_create_sailboat.TabIndex = 8;
this.btn_create_sailboat.Text = "Создать парусник";
this.btn_create_sailboat.UseVisualStyleBackColor = true;
this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click);
//
// comboBoxMapSelector
//
this.comboBoxMapSelector.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxMapSelector.FormattingEnabled = true;
this.comboBoxMapSelector.Items.AddRange(new object[] {
"Простая карта",
"Водная карта"});
this.comboBoxMapSelector.Location = new System.Drawing.Point(269, 14);
this.comboBoxMapSelector.Name = "comboBoxMapSelector";
this.comboBoxMapSelector.Size = new System.Drawing.Size(151, 28);
this.comboBoxMapSelector.TabIndex = 9;
this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_SelectedIndexChanged);
//
// FormMap
//
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.comboBoxMapSelector);
this.Controls.Add(this.btn_create_sailboat);
this.Controls.Add(this.btn_left);
this.Controls.Add(this.btn_up);
this.Controls.Add(this.btn_down);
this.Controls.Add(this.btn_right);
this.Controls.Add(this.btn_create);
this.Controls.Add(this.pictureBoxBoat);
this.Controls.Add(this.statusStrip1);
this.Name = "FormMap";
this.Click += new System.EventHandler(this.btn_move_Click);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_speed;
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_weight;
private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_color;
private System.Windows.Forms.PictureBox pictureBoxBoat;
private System.Windows.Forms.Button btn_create;
private System.Windows.Forms.Button btn_right;
private System.Windows.Forms.Button btn_down;
private System.Windows.Forms.Button btn_up;
private System.Windows.Forms.Button btn_left;
private System.Windows.Forms.Button btn_create_sailboat;
private System.Windows.Forms.ComboBox comboBoxMapSelector;
}
}

View File

@ -1,99 +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 Sailboat
{
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
}
/// <summary>
/// Обработка нажатия кнопки "Создать"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_create_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 btn_create_sailboat_Click(object sender, EventArgs e)
{
Random rnd = new();
var boat = new DrawingSailboat(rnd.Next(100, 300), rnd.Next(1000, 2000),
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(boat);
}
/// <summary>
/// Обработка нажатия стрелок
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_move_Click(object sender, EventArgs e)
{
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "btn_up":
dir = Direction.Up;
break;
case "btn_down":
dir = Direction.Down;
break;
case "btn_left":
dir = Direction.Left;
break;
case "btn_right":
dir = Direction.Right;
break;
}
pictureBoxBoat.Image = _abstractMap?.MoveObject(dir);
}
/// <summary>
/// Метод прорисовки машины
/// </summary>
private void SetData(DrawingBoat boat)
{
Random rnd = new();
toolStripLabel_color.Text = $"Скорость: {boat?.Boat.Speed}";
toolStripLabel_weight.Text = $"Вес: {boat?.Boat.Weight}";
toolStripLabel_color.Text = $"Цвет: { boat?.Boat.BodyColor.Name}";
pictureBoxBoat.Image = _abstractMap.CreateMap(pictureBoxBoat.Width, pictureBoxBoat.Height,
new DrawingObjectBoat(boat));
}
private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxMapSelector.Text)
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Водная карта":
_abstractMap = new WaterMap();
break;
}
}
}
}

216
Sailboat/FormMapWithSetBoats.Designer.cs generated Normal file
View File

@ -0,0 +1,216 @@

namespace Sailboat
{
partial class FormMapWithSetBoats
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btn_left = new System.Windows.Forms.Button();
this.btn_up = new System.Windows.Forms.Button();
this.btn_down = new System.Windows.Forms.Button();
this.btn_right = new System.Windows.Forms.Button();
this.btn_show_map = new System.Windows.Forms.Button();
this.btn_show_storage = new System.Windows.Forms.Button();
this.btn_remove_boat = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.btn_add_boat = new System.Windows.Forms.Button();
this.comboBoxMapSelector = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btn_left);
this.groupBox1.Controls.Add(this.btn_up);
this.groupBox1.Controls.Add(this.btn_down);
this.groupBox1.Controls.Add(this.btn_right);
this.groupBox1.Controls.Add(this.btn_show_map);
this.groupBox1.Controls.Add(this.btn_show_storage);
this.groupBox1.Controls.Add(this.btn_remove_boat);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.btn_add_boat);
this.groupBox1.Controls.Add(this.comboBoxMapSelector);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(704, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 501);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// btn_left
//
this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left;
this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_left.Location = new System.Drawing.Point(19, 444);
this.btn_left.Name = "btn_left";
this.btn_left.Size = new System.Drawing.Size(70, 40);
this.btn_left.TabIndex = 11;
this.btn_left.UseVisualStyleBackColor = true;
this.btn_left.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_up
//
this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up;
this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_up.Location = new System.Drawing.Point(95, 398);
this.btn_up.Name = "btn_up";
this.btn_up.Size = new System.Drawing.Size(75, 40);
this.btn_up.TabIndex = 10;
this.btn_up.UseVisualStyleBackColor = true;
this.btn_up.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_down
//
this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down;
this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_down.Location = new System.Drawing.Point(95, 444);
this.btn_down.Name = "btn_down";
this.btn_down.Size = new System.Drawing.Size(75, 40);
this.btn_down.TabIndex = 9;
this.btn_down.UseVisualStyleBackColor = true;
this.btn_down.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_right
//
this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right;
this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.btn_right.Location = new System.Drawing.Point(174, 444);
this.btn_right.Name = "btn_right";
this.btn_right.Size = new System.Drawing.Size(70, 40);
this.btn_right.TabIndex = 8;
this.btn_right.UseVisualStyleBackColor = true;
this.btn_right.Click += new System.EventHandler(this.btn_move_Click);
//
// btn_show_map
//
this.btn_show_map.Location = new System.Drawing.Point(6, 339);
this.btn_show_map.Name = "btn_show_map";
this.btn_show_map.Size = new System.Drawing.Size(232, 29);
this.btn_show_map.TabIndex = 6;
this.btn_show_map.Text = "Посмотреть карту";
this.btn_show_map.UseVisualStyleBackColor = true;
this.btn_show_map.Click += new System.EventHandler(this.btn_show_map_Click);
//
// btn_show_storage
//
this.btn_show_storage.Location = new System.Drawing.Point(6, 284);
this.btn_show_storage.Name = "btn_show_storage";
this.btn_show_storage.Size = new System.Drawing.Size(232, 29);
this.btn_show_storage.TabIndex = 5;
this.btn_show_storage.Text = "Посмотреть хранилище";
this.btn_show_storage.UseVisualStyleBackColor = true;
this.btn_show_storage.Click += new System.EventHandler(this.btn_show_storage_Click);
//
// btn_remove_boat
//
this.btn_remove_boat.Location = new System.Drawing.Point(6, 184);
this.btn_remove_boat.Name = "btn_remove_boat";
this.btn_remove_boat.Size = new System.Drawing.Size(232, 29);
this.btn_remove_boat.TabIndex = 4;
this.btn_remove_boat.Text = "Удалить лодку";
this.btn_remove_boat.UseVisualStyleBackColor = true;
this.btn_remove_boat.Click += new System.EventHandler(this.btn_remove_boat_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 151);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27);
this.maskedTextBoxPosition.TabIndex = 3;
//
// btn_add_boat
//
this.btn_add_boat.Location = new System.Drawing.Point(6, 89);
this.btn_add_boat.Name = "btn_add_boat";
this.btn_add_boat.Size = new System.Drawing.Size(232, 29);
this.btn_add_boat.TabIndex = 2;
this.btn_add_boat.Text = "Добавить лодку";
this.btn_add_boat.UseVisualStyleBackColor = true;
this.btn_add_boat.Click += new System.EventHandler(this.btn_add_boat_Click);
//
// comboBoxMapSelector
//
this.comboBoxMapSelector.FormattingEnabled = true;
this.comboBoxMapSelector.Items.AddRange(new object[] {
"Простая карта",
"Водная карта"});
this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 26);
this.comboBoxMapSelector.Name = "comboBoxMapSelector";
this.comboBoxMapSelector.Size = new System.Drawing.Size(232, 28);
this.comboBoxMapSelector.TabIndex = 1;
this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_SelectedIndexChanged);
//
// 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(704, 501);
this.pictureBox.TabIndex = 0;
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(954, 501);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetBoats";
this.Text = "FormMapWithSetBoats";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btn_show_map;
private System.Windows.Forms.Button btn_show_storage;
private System.Windows.Forms.Button btn_remove_boat;
private System.Windows.Forms.MaskedTextBox maskedTextBoxPosition;
private System.Windows.Forms.Button btn_add_boat;
private System.Windows.Forms.ComboBox comboBoxMapSelector;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Button btn_left;
private System.Windows.Forms.Button btn_up;
private System.Windows.Forms.Button btn_down;
private System.Windows.Forms.Button btn_right;
}
}

View File

@ -0,0 +1,144 @@
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 Sailboat
{
public partial class FormMapWithSetBoats : Form
{
private MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
public FormMapWithSetBoats()
{
InitializeComponent();
AbstractMap map = new SimpleMap();
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxMapSelector.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Водная карта":
map = new WaterMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawingObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
}
private void btn_add_boat_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
BoatForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (form.SelectedBoat == null)
{
MessageBox.Show("Вы не создали объект");
return;
}
DrawingObjectBoat bus = new(form.SelectedBoat);
if (_mapBoatsCollectionGeneric + bus != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void btn_remove_boat_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 (_mapBoatsCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void btn_show_storage_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
}
private void btn_show_map_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap();
}
private void btn_move_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "btn_up":
dir = Direction.Up;
break;
case "btn_down":
dir = Direction.Down;
break;
case "btn_left":
dir = Direction.Left;
break;
case "btn_right":
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="statusStrip1.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,140 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
class MapWithSetBoatsGeneric<T, U> where T : class, IDrawingObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 210;
private readonly int _placeSizeHeight = 90;
private readonly SetBoatsGeneric<T> _setBoats;
private readonly U _map;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetBoatsGeneric<T, U> map, T boat)
{
return map._setBoats.Insert(boat);
}
public static T operator -(MapWithSetBoatsGeneric<T, U> map, int position)
{
return map._setBoats.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawBoats(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
foreach (var boat in _setBoats.GetBoats())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
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;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Brown, 3);
g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawBoats(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int currentWidth = 0;
int currentHeight = 0;
foreach (var machine in _setBoats.GetBoats())
{
int dop = 0;
if (currentWidth > 0 && currentHeight > 0)
{
dop = 30;
}
machine.SetObject(currentWidth * _placeSizeWidth + dop,
currentHeight * _placeSizeHeight + dop,
_pictureWidth, _pictureHeight);
machine?.DrawingObject(g);
if (currentWidth < width - 1)
{
currentWidth++;
}
else
{
currentWidth = 0;
currentHeight++;
}
if (currentHeight > height) return;
}
}
}
}

View File

@ -17,7 +17,7 @@ namespace Sailboat
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMap());
Application.Run(new FormMapWithSetBoats());
}
}
}

103
Sailboat/SetBoatsGeneric.cs Normal file
View File

@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sailboat
{
class SetBoatsGeneric<T> where T : class
{
private readonly List<T> _places;
private readonly int _maxCount;
public int Count => _places.Count;
private int BusyPlaces = 0;
public SetBoatsGeneric(int count)
{
_maxCount = count;
_places = new List<T>();
}
public int Insert(T boat)
{
if (Count + 1 <= _maxCount) return Insert(boat, 0);
else return -1;
}
public int Insert(T boat, int position)
{
if (position >= _maxCount && position < 0)
{
return -1;
}
_places.Insert(position, boat);
return position;
}
public T Remove(int position)
{
if (position < _maxCount && position >= 0)
{
if (_places.ElementAt(position) != null)
{
T result = _places.ElementAt(position);
_places.RemoveAt(position);
return result;
}
return null;
}
return null;
}
public T Get(int position)
{
if (position < 0 || position >= _places.Count) return null;
return _places[position];
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T this[int position]
{
get
{
if (position < _maxCount && position >= 0)
{
return _places.ElementAt(position);
}
return null;
}
set
{
if (position < _maxCount && position >= 0)
{
Insert(this[position], position);
}
}
}
public IEnumerable<T> GetBoats()
{
foreach (var boat in _places)
{
if (boat != null)
{
yield return boat;
}
else
{
yield break;
}
}
}
}
}