From 160e1ae53bd9136f4c0fb46acf50281d8112d5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=A7=D0=B5=D0=B3=D0=BE=D0=B4=D0=B0=D0=B5=D0=B2?= Date: Thu, 14 Dec 2023 00:48:20 +0400 Subject: [PATCH] lab3 --- Cruiser/Cruiser/AbstractStrategy.cs | 2 +- Cruiser/Cruiser/CruiserGenericCollection.cs | 102 ++++++++++++++ Cruiser/Cruiser/DirectionType.cs | 1 - Cruiser/Cruiser/DrawningCruiser.cs | 50 ++++--- Cruiser/Cruiser/DrawningCruiserDou.cs | 19 ++- Cruiser/Cruiser/EntityCruiser.cs | 8 +- Cruiser/Cruiser/EntityCruiserDou.cs | 3 +- Cruiser/Cruiser/FormCruiser.Designer.cs | 19 ++- Cruiser/Cruiser/FormCruiser.cs | 55 ++++++-- .../Cruiser/FormCruiserCollection.Designer.cs | 126 ++++++++++++++++++ Cruiser/Cruiser/FormCruiserCollection.cs | 65 +++++++++ Cruiser/Cruiser/FormCruiserCollection.resx | 120 +++++++++++++++++ Cruiser/Cruiser/IMoveableObject.cs | 5 + Cruiser/Cruiser/IMoveableObject_Realise.cs | 18 ++- Cruiser/Cruiser/MoveToCenter.cs | 1 - Cruiser/Cruiser/ObjectParameters.cs | 6 +- Cruiser/Cruiser/Program.cs | 2 +- Cruiser/Cruiser/SetGeneric.cs | 76 +++++++++++ 18 files changed, 615 insertions(+), 63 deletions(-) create mode 100644 Cruiser/Cruiser/CruiserGenericCollection.cs create mode 100644 Cruiser/Cruiser/FormCruiserCollection.Designer.cs create mode 100644 Cruiser/Cruiser/FormCruiserCollection.cs create mode 100644 Cruiser/Cruiser/FormCruiserCollection.resx create mode 100644 Cruiser/Cruiser/SetGeneric.cs diff --git a/Cruiser/Cruiser/AbstractStrategy.cs b/Cruiser/Cruiser/AbstractStrategy.cs index 9d40e09..966de1f 100644 --- a/Cruiser/Cruiser/AbstractStrategy.cs +++ b/Cruiser/Cruiser/AbstractStrategy.cs @@ -46,7 +46,7 @@ namespace ProjectCruiser.MovementStrategy } MoveToTarget(); } - + protected bool MoveLeft() => MoveTo(DirectionType.Left); protected bool MoveRight() => MoveTo(DirectionType.Right); diff --git a/Cruiser/Cruiser/CruiserGenericCollection.cs b/Cruiser/Cruiser/CruiserGenericCollection.cs new file mode 100644 index 0000000..0f2fb48 --- /dev/null +++ b/Cruiser/Cruiser/CruiserGenericCollection.cs @@ -0,0 +1,102 @@ +using ProjectCruiser.DrawningObjects; +using ProjectCruiser.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser +{ + + internal class CruiserGenericCollection + where T : DrawningCruiser + where U : IMoveableObject + { + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 210; + + private readonly int _placeSizeHeight = 100; + + private readonly SetGeneric _collection; + + public CruiserGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public static int? operator +(CruiserGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + + public static bool operator -(CruiserGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + public Bitmap ShowCruiser() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + 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 + 2, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics g) + { + int Ix = 3; + int Iy = 15; + for (int i = 0; i < _collection.Count - 1; i++) + { + _collection.Get(i)?.SetPosition(Ix, Iy); + _collection.Get(i)?.DrawTransport(g); + Ix += _placeSizeWidth; + if (Ix + _placeSizeHeight > _pictureWidth) + { + Ix = 0; + Iy = _placeSizeHeight; + } + } + } + } +} diff --git a/Cruiser/Cruiser/DirectionType.cs b/Cruiser/Cruiser/DirectionType.cs index 5c3b79f..ce0c50f 100644 --- a/Cruiser/Cruiser/DirectionType.cs +++ b/Cruiser/Cruiser/DirectionType.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; namespace ProjectCruiser.Drawnings { - public enum DirectionType { Up = 1, diff --git a/Cruiser/Cruiser/DrawningCruiser.cs b/Cruiser/Cruiser/DrawningCruiser.cs index 52ee43f..bcbea50 100644 --- a/Cruiser/Cruiser/DrawningCruiser.cs +++ b/Cruiser/Cruiser/DrawningCruiser.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using ProjectCruiser.Entities; using ProjectCruiser.Drawnings; using System.Drawing.Drawing2D; +using ProjectCruiser.MovementStrategy; namespace ProjectCruiser.DrawningObjects { @@ -21,36 +22,38 @@ namespace ProjectCruiser.DrawningObjects protected int _startPosX; protected int _startPosY; - - protected readonly int _cruiserWidth = 145; - - protected readonly int _cruiserHeight = 45; - + + protected readonly int _carWidth = 145; + + protected readonly int _carHeight = 45; + public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height) { _pictureWidth = width; _pictureHeight = height; EntityCruiser = new EntityCruiser(speed, weight, bodyColor); + } - + protected DrawningCruiser(int speed, double weight, Color bodyColor, int - width, int height, int cruiserWidth, int cruiserHeight) + width, int height, int carWidth, int carHeight) { if (width <= _pictureWidth || height <= _pictureHeight) { return; } + _pictureWidth = width; _pictureHeight = height; - _cruiserWidth = cruiserWidth; - _cruiserHeight = cruiserHeight; + _carWidth = carWidth; + _carHeight = carHeight; EntityCruiser = new EntityCruiser(speed, weight, bodyColor); } public void SetPosition(int x, int y) { - if (x < 0 || y < 0 || x + _cruiserWidth > _pictureWidth || y + _cruiserHeight > _pictureHeight) + if (x < 0 || y < 0 || x + _carWidth > _pictureWidth || y + _carHeight > _pictureHeight) { x = 10; y = 10; @@ -59,6 +62,8 @@ namespace ProjectCruiser.DrawningObjects _startPosY = y; } + public IMoveableObject GetMoveableObject => new DrawningObjectCruiser(this); + protected int PictureWidth { get { return _pictureWidth; } @@ -68,14 +73,14 @@ namespace ProjectCruiser.DrawningObjects { get { return _pictureHeight; } } - + public int GetPosX => _startPosX; - + public int GetPosY => _startPosY; - - public int GetWidth => _cruiserWidth; - - public int GetHeight => _cruiserHeight; + + public int GetWidth => _carWidth; + + public int GetHeight => _carHeight; public virtual bool CanMove(DirectionType direction) { @@ -86,16 +91,16 @@ namespace ProjectCruiser.DrawningObjects return direction switch { DirectionType.Left => _startPosX - EntityCruiser.Step > 0, - + DirectionType.Up => _startPosY - EntityCruiser.Step > 7, - - DirectionType.Right => _startPosX + EntityCruiser.Step + _cruiserWidth <= _pictureWidth, - - DirectionType.Down => _startPosY + EntityCruiser.Step + _cruiserHeight <= _pictureHeight, + + DirectionType.Right => _startPosX + EntityCruiser.Step + _carWidth <= _pictureWidth, + + DirectionType.Down => _startPosY + EntityCruiser.Step + _carHeight <= _pictureHeight, _ => false, }; } - + public virtual void MoveTransport(DirectionType direction) { if (!CanMove(direction) || EntityCruiser == null) @@ -126,6 +131,7 @@ namespace ProjectCruiser.DrawningObjects return; } Pen pen = new(Color.Black); + Brush BodyColor = new SolidBrush(EntityCruiser.BodyColor); GraphicsPath path1 = new GraphicsPath(); diff --git a/Cruiser/Cruiser/DrawningCruiserDou.cs b/Cruiser/Cruiser/DrawningCruiserDou.cs index 6632aa2..d4ac836 100644 --- a/Cruiser/Cruiser/DrawningCruiserDou.cs +++ b/Cruiser/Cruiser/DrawningCruiserDou.cs @@ -24,36 +24,33 @@ namespace ProjectCruiser.DrawningObjects public override void DrawTransport(Graphics g) { - if (EntityCruiser is not EntityCruiserDou cruiserWith) + if (EntityCruiser is not EntityCruiserDou cruiserDou) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(cruiserWith.AdditionalColor); - Brush BodyColor = new SolidBrush(EntityCruiser.BodyColor); + Brush additionalBrush = new SolidBrush(cruiserDou.AdditionalColor); Brush brBlack = new SolidBrush(Color.Black); base.DrawTransport(g); - if (cruiserWith.Vert) + if (cruiserDou.Vert) { Brush brRed = new SolidBrush(Color.Red); - g.FillEllipse(brRed, _startPosX + 95, _startPosY + 15, 20, 20); + g.FillEllipse(additionalBrush, _startPosX + 95, _startPosY + 15, 20, 20); g.DrawEllipse(pen, _startPosX + 95, _startPosY + 15, 20, 20); } - if (cruiserWith.Rocket) + if (cruiserDou.Rocket) { g.DrawEllipse(pen, _startPosX + 8, _startPosY + 3, 15, 12); - g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 3, 15, 12); + g.FillEllipse(additionalBrush, _startPosX + 8, _startPosY + 3, 15, 12); g.DrawEllipse(pen, _startPosX + 8, _startPosY + 18, 15, 12); - g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 18, 15, 12); + g.FillEllipse(additionalBrush, _startPosX + 8, _startPosY + 18, 15, 12); g.DrawEllipse(pen, _startPosX + 8, _startPosY + 33, 15, 12); - g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 33, 15, 12); - + g.FillEllipse(additionalBrush, _startPosX + 8, _startPosY + 33, 15, 12); } - } } } diff --git a/Cruiser/Cruiser/EntityCruiser.cs b/Cruiser/Cruiser/EntityCruiser.cs index 1940d82..b7a26f4 100644 --- a/Cruiser/Cruiser/EntityCruiser.cs +++ b/Cruiser/Cruiser/EntityCruiser.cs @@ -9,13 +9,13 @@ namespace ProjectCruiser.Entities public class EntityCruiser { public int Speed { get; private set; } - + public double Weight { get; private set; } - + public Color BodyColor { get; private set; } - + public double Step => (double)Speed * 100 / Weight; - + public EntityCruiser(int speed, double weight, Color bodyColor) { Speed = speed; diff --git a/Cruiser/Cruiser/EntityCruiserDou.cs b/Cruiser/Cruiser/EntityCruiserDou.cs index fb514cf..37f8a2f 100644 --- a/Cruiser/Cruiser/EntityCruiserDou.cs +++ b/Cruiser/Cruiser/EntityCruiserDou.cs @@ -4,12 +4,13 @@ namespace ProjectCruiser.Entities { public class EntityCruiserDou : EntityCruiser { + public Color AdditionalColor { get; private set; } public bool Vert { get; private set; } public bool Rocket { get; private set; } - + public EntityCruiserDou(int speed, double weight, Color bodyColor, Color additionalColor, bool vert, bool rocket) : base(speed, weight, bodyColor) { diff --git a/Cruiser/Cruiser/FormCruiser.Designer.cs b/Cruiser/Cruiser/FormCruiser.Designer.cs index 2cc1a0c..8540a3a 100644 --- a/Cruiser/Cruiser/FormCruiser.Designer.cs +++ b/Cruiser/Cruiser/FormCruiser.Designer.cs @@ -38,6 +38,7 @@ this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.pictureBoxCruiser = new System.Windows.Forms.PictureBox(); + this.ButtonSelectCruiser = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit(); this.SuspendLayout(); // @@ -62,17 +63,17 @@ this.ButtonCreateCruiserBat.TabIndex = 1; this.ButtonCreateCruiserBat.Text = "Создать крейсер с ракетными шахтами и площадкой под вертолет"; this.ButtonCreateCruiserBat.UseVisualStyleBackColor = true; - this.ButtonCreateCruiserBat.Click += new System.EventHandler(this.ButtonCreateUstaBat_Click); + this.ButtonCreateCruiserBat.Click += new System.EventHandler(this.ButtonCreateCruiserBat_Click); // // ButtonCreateCruiser // this.ButtonCreateCruiser.Location = new System.Drawing.Point(185, 386); this.ButtonCreateCruiser.Name = "ButtonCreateCruiser"; - this.ButtonCreateCruiser.Size = new System.Drawing.Size(157, 55); + this.ButtonCreateCruiser.Size = new System.Drawing.Size(160, 57); this.ButtonCreateCruiser.TabIndex = 2; this.ButtonCreateCruiser.Text = "Создать крейсер"; this.ButtonCreateCruiser.UseVisualStyleBackColor = true; - this.ButtonCreateCruiser.Click += new System.EventHandler(this.ButtonCreateUsta_Click); + this.ButtonCreateCruiser.Click += new System.EventHandler(this.ButtonCreateCruiser_Click); // // ButtonStep // @@ -138,11 +139,22 @@ this.pictureBoxCruiser.TabIndex = 8; this.pictureBoxCruiser.TabStop = false; // + // ButtonSelectCruiser + // + this.ButtonSelectCruiser.Location = new System.Drawing.Point(351, 386); + this.ButtonSelectCruiser.Name = "ButtonSelectCruiser"; + this.ButtonSelectCruiser.Size = new System.Drawing.Size(160, 57); + this.ButtonSelectCruiser.TabIndex = 9; + this.ButtonSelectCruiser.Text = "Выбор"; + this.ButtonSelectCruiser.UseVisualStyleBackColor = true; + this.ButtonSelectCruiser.Click += new System.EventHandler(this.ButtonSelectCruiser_Click); + // // FormCruiser // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonSelectCruiser); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonDown); @@ -170,5 +182,6 @@ private Button buttonLeft; private Button buttonRight; private PictureBox pictureBoxCruiser; + private Button ButtonSelectCruiser; } } \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiser.cs b/Cruiser/Cruiser/FormCruiser.cs index 155264c..3832fa2 100644 --- a/Cruiser/Cruiser/FormCruiser.cs +++ b/Cruiser/Cruiser/FormCruiser.cs @@ -1,18 +1,22 @@ using ProjectCruiser.DrawningObjects; using ProjectCruiser.Drawnings; using ProjectCruiser.MovementStrategy; - namespace ProjectCruiser { public partial class FormCruiser : Form { + private DrawningCruiser? _drawningCruiser; private AbstractStrategy? _abstractStrategy; + public DrawningCruiser? SelectedCruiser { get; private set; } + public FormCruiser() { InitializeComponent(); + _abstractStrategy = null; + SelectedCruiser = null; } private void Draw() @@ -28,32 +32,50 @@ namespace ProjectCruiser pictureBoxCruiser.Image = bmp; } - private void ButtonCreateUstaBat_Click(object sender, EventArgs e) + private void ButtonCreateCruiserBat_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + ColorDialog dialog_dop = new(); + if (dialog_dop.ShowDialog() == DialogResult.OK) + { + dopColor = dialog_dop.Color; + } + _drawningCruiser = new DrawningCruiserDou(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxCruiser.Width, pictureBoxCruiser.Height); - _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,100)); Draw(); } - - private void ButtonCreateUsta_Click(object sender, EventArgs e) + + private void ButtonCreateCruiser_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _drawningCruiser = new DrawningCruiser(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, pictureBoxCruiser.Width, pictureBoxCruiser.Height); - _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,100)); Draw(); } @@ -119,5 +141,10 @@ namespace ProjectCruiser } } + private void ButtonSelectCruiser_Click(object sender, EventArgs e) + { + SelectedCruiser = _drawningCruiser; + DialogResult = DialogResult.OK; + } } } diff --git a/Cruiser/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs new file mode 100644 index 0000000..3c68c72 --- /dev/null +++ b/Cruiser/Cruiser/FormCruiserCollection.Designer.cs @@ -0,0 +1,126 @@ +namespace ProjectCruiser +{ + partial class FormCruiserCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + groupBox1 = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + ButtonRefreshCollection = new Button(); + ButtonRemoveCruiser = new Button(); + buttonAddCruiser = new Button(); + pictureBoxCollection = new PictureBox(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBox1 + // + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(ButtonRefreshCollection); + groupBox1.Controls.Add(ButtonRemoveCruiser); + groupBox1.Controls.Add(buttonAddCruiser); + groupBox1.Location = new Point(844, 12); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(196, 319); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Font = new Font("Lucida Sans Unicode", 9F, FontStyle.Regular, GraphicsUnit.Point); + maskedTextBoxNumber.Location = new Point(29, 78); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(130, 26); + maskedTextBoxNumber.TabIndex = 3; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(6, 195); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(184, 33); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обовить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // ButtonRemoveCruiser + // + ButtonRemoveCruiser.Location = new Point(6, 136); + ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; + ButtonRemoveCruiser.Size = new Size(184, 33); + ButtonRemoveCruiser.TabIndex = 1; + ButtonRemoveCruiser.Text = "Удалить Крейсер"; + ButtonRemoveCruiser.UseVisualStyleBackColor = true; + ButtonRemoveCruiser.Click += ButtonRemoveCruiser_Click; + // + // buttonAddCruiser + // + buttonAddCruiser.Location = new Point(6, 22); + buttonAddCruiser.Name = "buttonAddCruiser"; + buttonAddCruiser.Size = new Size(184, 33); + buttonAddCruiser.TabIndex = 0; + buttonAddCruiser.Text = "Добавить Крейсер"; + buttonAddCruiser.UseVisualStyleBackColor = true; + buttonAddCruiser.Click += buttonAddCruiser_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, -1); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(838, 432); + pictureBoxCollection.SizeMode = PictureBoxSizeMode.Zoom; + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormCruiserCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1062, 489); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBox1); + Name = "FormCruiserCollection"; + Text = "FormCruiserCollection"; + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBox1; + private MaskedTextBox maskedTextBoxNumber; + private Button ButtonRefreshCollection; + private Button ButtonRemoveCruiser; + private Button buttonAddCruiser; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/Cruiser/Cruiser/FormCruiserCollection.cs b/Cruiser/Cruiser/FormCruiserCollection.cs new file mode 100644 index 0000000..84bf114 --- /dev/null +++ b/Cruiser/Cruiser/FormCruiserCollection.cs @@ -0,0 +1,65 @@ +using ProjectCruiser.DrawningObjects; +using ProjectCruiser.MovementStrategy; +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 ProjectCruiser +{ + public partial class FormCruiserCollection : Form + { + private readonly CruiserGenericCollection _cruiser; + + public FormCruiserCollection() + { + InitializeComponent(); + _cruiser = new CruiserGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void buttonAddCruiser_Click(object sender, EventArgs e) + { + FormCruiser form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_cruiser + form.SelectedCruiser > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _cruiser.ShowCruiser(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveCruiser_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_cruiser - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _cruiser.ShowCruiser(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _cruiser.ShowCruiser(); + } + } +} diff --git a/Cruiser/Cruiser/FormCruiserCollection.resx b/Cruiser/Cruiser/FormCruiserCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Cruiser/Cruiser/FormCruiserCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Cruiser/Cruiser/IMoveableObject.cs b/Cruiser/Cruiser/IMoveableObject.cs index e7aafaf..c91449a 100644 --- a/Cruiser/Cruiser/IMoveableObject.cs +++ b/Cruiser/Cruiser/IMoveableObject.cs @@ -9,6 +9,7 @@ namespace ProjectCruiser.MovementStrategy { public interface IMoveableObject { + ObjectParameters? GetObjectPosition { get; } int GetStep { get; } @@ -16,6 +17,10 @@ namespace ProjectCruiser.MovementStrategy bool CheckCanMove(DirectionType direction); void MoveObject(DirectionType direction); + + void SetPosition(int x, int y); + + void Draw(Graphics g); } } diff --git a/Cruiser/Cruiser/IMoveableObject_Realise.cs b/Cruiser/Cruiser/IMoveableObject_Realise.cs index 112131f..5b463a8 100644 --- a/Cruiser/Cruiser/IMoveableObject_Realise.cs +++ b/Cruiser/Cruiser/IMoveableObject_Realise.cs @@ -9,6 +9,7 @@ using ProjectCruiser.Drawnings; namespace ProjectCruiser.MovementStrategy { + public class DrawningObjectCruiser : IMoveableObject { private readonly DrawningCruiser? _drawningCruiser = null; @@ -27,12 +28,27 @@ namespace ProjectCruiser.MovementStrategy } return new ObjectParameters(_drawningCruiser.GetPosX, _drawningCruiser.GetPosY, _drawningCruiser.GetWidth, _drawningCruiser.GetHeight); - } + } } public int GetStep => (int)(_drawningCruiser?.EntityCruiser?.Step ?? 0); public bool CheckCanMove(DirectionType direction) => _drawningCruiser?.CanMove(direction) ?? false; public void MoveObject(DirectionType direction) => _drawningCruiser?.MoveTransport(direction); + public void SetPosition(int x, int y) + { + if (_drawningCruiser != null) + { + _drawningCruiser.SetPosition(x, y); + } + } + + public void Draw(Graphics g) + { + if (_drawningCruiser != null) + { + _drawningCruiser.DrawTransport(g); + } + } } } diff --git a/Cruiser/Cruiser/MoveToCenter.cs b/Cruiser/Cruiser/MoveToCenter.cs index 96ecab0..056010a 100644 --- a/Cruiser/Cruiser/MoveToCenter.cs +++ b/Cruiser/Cruiser/MoveToCenter.cs @@ -58,7 +58,6 @@ namespace ProjectCruiser.MovementStrategy } } } - } } diff --git a/Cruiser/Cruiser/ObjectParameters.cs b/Cruiser/Cruiser/ObjectParameters.cs index 7eac0cb..a0605ff 100644 --- a/Cruiser/Cruiser/ObjectParameters.cs +++ b/Cruiser/Cruiser/ObjectParameters.cs @@ -16,15 +16,15 @@ namespace ProjectCruiser.MovementStrategy public int LeftBorder => _x; public int TopBorder => _y; - + public int RightBorder => _x + _width; public int DownBorder => _y + _height; public int ObjectMiddleHorizontal => _x + _width / 2; - + public int ObjectMiddleVertical => _y + _height / 2; - + public ObjectParameters(int x, int y, int width, int height) { _x = x; diff --git a/Cruiser/Cruiser/Program.cs b/Cruiser/Cruiser/Program.cs index 4e00cd8..f66a30c 100644 --- a/Cruiser/Cruiser/Program.cs +++ b/Cruiser/Cruiser/Program.cs @@ -11,7 +11,7 @@ namespace ProjectCruiser // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormCruiser()); + Application.Run(new FormCruiserCollection()); } } } \ No newline at end of file diff --git a/Cruiser/Cruiser/SetGeneric.cs b/Cruiser/Cruiser/SetGeneric.cs new file mode 100644 index 0000000..6f5a3c4 --- /dev/null +++ b/Cruiser/Cruiser/SetGeneric.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectCruiser +{ + internal class SetGeneric + where T : class + { + + private readonly T?[] _places; + + public int Count => _places.Length; + + public SetGeneric(int count) + { + _places = new T?[count]; + } + + public int Insert(T cruiser) + { + return Insert(cruiser, 0); + } + + public int Insert(T cruiser, int position) + { + if (position < 0 && position > Count) + { + return -1; + } + if (_places[position] != null) + { + int d = 0; + for (int j = 1; j < Count - position; j++) + { + if (_places[position + j] == null) + { + d = position + j; + break; + } + } + if (d == 0) + { + return -1; + } + for (int j = d; j > position; j--) + { + _places[j] = _places[j - 1]; + } + } + _places[position] = cruiser; + return position; + } + + public bool Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return false; + } + _places[position] = null; + return true; + } + + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +}