diff --git a/Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozer.cs b/Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozer.cs index 3a1504b..6f623d8 100644 --- a/Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawingObjects/DrawingBulldozer.cs @@ -1,4 +1,5 @@ using Bulldozer.Entities; +using Bulldozer.MovementStrategy; namespace Bulldozer.DrawingObjects { @@ -46,32 +47,28 @@ namespace Bulldozer.DrawingObjects /// Высота картинки public DrawingBulldozer(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height) { - if (width < _bulldozerWidth || height < _bulldozerHeight) - { - width += _bulldozerWidth * 2; - height += _bulldozerHeight * 2; - } - else - { _pictureWidth = width; _pictureHeight = height; EntityBulldozer = new EntityBulldozer(speed, weight, bodyColor, additionalColor); - } } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки бульдозера + /// Высота прорисовки бульдозера protected DrawingBulldozer(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height, int bulldozerWidth, int bulldozerHeight) { - if (width < _bulldozerWidth || height < _bulldozerHeight) - { - width += _bulldozerWidth * 2; - height += _bulldozerHeight * 2; - } - else - { _pictureWidth = width; _pictureHeight = height; + _bulldozerWidth = bulldozerWidth; + _bulldozerHeight = bulldozerHeight; EntityBulldozer = new EntityBulldozer(speed, weight, bodyColor, additionalColor); - } } /// @@ -82,15 +79,14 @@ namespace Bulldozer.DrawingObjects public void SetPosition(int x, int y) { - if (x < 0 || y < 0) - { - _startPosX = 0; _startPosY = 0; - } - else - { - _startPosX = x; - _startPosY = y; - } + // Проверка границ + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x > _pictureWidth - _bulldozerWidth) x = _pictureWidth - _bulldozerWidth; + if (y > _pictureHeight - _bulldozerHeight) y = _pictureHeight - _bulldozerHeight; + + _startPosX = x; + _startPosY = y; } /// @@ -122,13 +118,13 @@ namespace Bulldozer.DrawingObjects } return direction switch { - //влево + // влево DirectionType.Left => _startPosX - EntityBulldozer.Step > 0, - //вверх + // вверх DirectionType.Up => _startPosY - EntityBulldozer.Step > 0, // вправо DirectionType.Right => _startPosX + EntityBulldozer.Step < _pictureWidth, - //вниз + // вниз DirectionType.Down => _startPosY + EntityBulldozer.Step < _pictureHeight, _ => false, }; @@ -137,7 +133,7 @@ namespace Bulldozer.DrawingObjects /// Изменение направления перемещения /// /// Направление - public virtual void MoveBulldozer(DirectionType direction) + public void MoveBulldozer(DirectionType direction) { if (!CanMove(direction) || EntityBulldozer == null) { @@ -176,29 +172,33 @@ namespace Bulldozer.DrawingObjects Pen pen = new(Color.Black); Brush bodyBrush = new SolidBrush(EntityBulldozer.BodyColor); Brush additionalBrush = new SolidBrush(EntityBulldozer.AdditionalColor); - /// /// Заливка гусеницы бульдозера /// - Brush brGray = new SolidBrush(Color.Gray); + + Brush brGray = new SolidBrush(Color.Gray); g.FillEllipse(brGray, _startPosX + 17, _startPosY + 24, 119, 40); //Гусеница g.FillEllipse(additionalBrush, _startPosX + 20, _startPosY + 35, 20, 20); // Левое колесо гусеницы g.FillEllipse(additionalBrush, _startPosX + 115, _startPosY + 35, 20, 20); // Правое колесо гусеницы g.FillEllipse(additionalBrush, _startPosX + 50, _startPosY + 45, 10, 10); // 1 центральное колесо гусеницы g.FillEllipse(additionalBrush, _startPosX + 70, _startPosY + 45, 10, 10); // 2 центральное колесо гусеницы g.FillEllipse(additionalBrush, _startPosX + 90, _startPosY + 45, 10, 10); // 3 центральное колесо гусеницы + + + /// /// Отрисовка границ бульдозера /// - + g.DrawEllipse(pen, _startPosX + 17, _startPosY + 24, 119, 40); g.DrawEllipse(pen, _startPosX + 20, _startPosY + 35, 20, 20); // Левое колесо гусеницы g.DrawEllipse(pen, _startPosX + 115, _startPosY + 35, 20, 20); // Правое колесо гусеницы g.DrawEllipse(pen, _startPosX + 50, _startPosY + 45, 10, 10); // 1 центральное колесо гусеницы g.DrawEllipse(pen, _startPosX + 70, _startPosY + 45, 10, 10); // 2 центральное колесо гусеницы g.DrawEllipse(pen, _startPosX + 90, _startPosY + 45, 10, 10); // 3 центральное колесо гусеницы + /// /// Кузов бульдозера /// @@ -212,7 +212,11 @@ namespace Bulldozer.DrawingObjects g.DrawRectangle(pen, _startPosX + 17, _startPosY + 24, 119, 18); // основная часть g.DrawRectangle(pen, _startPosX + 30, _startPosY, 10, 24); // выхлопная труба } + /// + /// Получение объекта IMoveableObject из объекта DrawingBulldozer + /// + public IMoveableObject GetMoveableObject => new DrawingObjectBulldozer(this); } - + } diff --git a/Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs b/Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs index f711196..eef24ea 100644 --- a/Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs +++ b/Bulldozer/Bulldozer/DrawingObjects/DrawingUpgradedBulldozer.cs @@ -11,13 +11,12 @@ namespace Bulldozer.DrawingObjects /// Вес /// Основной цвет /// Дополнительный цвет - /// Цвет обвесов /// Признак наличия рыхлителя /// Признак наличия отвала /// Ширина картинки /// Высота картинки - + public DrawingBulldozerUpgraded(int speed, double weight, Color bodyColor, Color additionalColor, Color dopColor, bool blade, bool ripper, int width, int height) : base(speed, weight, bodyColor, additionalColor, width, height) { diff --git a/Bulldozer/Bulldozer/Entities/EntityBulldozer.cs b/Bulldozer/Bulldozer/Entities/EntityBulldozer.cs index f230d16..c2e09fa 100644 --- a/Bulldozer/Bulldozer/Entities/EntityBulldozer.cs +++ b/Bulldozer/Bulldozer/Entities/EntityBulldozer.cs @@ -1,4 +1,6 @@ -namespace Bulldozer.Entities +using System.Windows.Forms.Design; + +namespace Bulldozer.Entities { /// /// Класс-сущность "Бульдозер" @@ -17,10 +19,10 @@ /// Основной цвет /// public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет - /// + public Color AdditionalColor { get; private set; } + + public Color DopColor { get; private set; } /// /// Шаг перемещения бульдозера /// @@ -31,7 +33,6 @@ /// Скорость /// Вес бульдозера /// Основной цвет - /// Дополнительный цвет public EntityBulldozer(int speed, double weight, Color bodyColor, Color additionalColor) { Speed = speed; @@ -40,5 +41,7 @@ AdditionalColor = additionalColor; } } + + } diff --git a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs index ea0e07e..6b8c6d5 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.Designer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.Designer.cs @@ -28,144 +28,168 @@ /// private void InitializeComponent() { - pictureBoxBulldozer = new PictureBox(); - buttonCreateBulldozer = new Button(); - buttonLeft = new Button(); - buttonDown = new Button(); - buttonRight = new Button(); - buttonUp = new Button(); - comboBoxStrategy = new ComboBox(); - buttonStep = new Button(); - buttonCreateUpgradedBulldozer = new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).BeginInit(); - SuspendLayout(); + this.pictureBoxBulldozer = new System.Windows.Forms.PictureBox(); + this.buttonCreateBulldozer = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); + this.buttonStep = new System.Windows.Forms.Button(); + this.buttonCreateUpgradedBulldozer = new System.Windows.Forms.Button(); + this.buttonSelectBulldozer = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBulldozer)).BeginInit(); + this.SuspendLayout(); // // pictureBoxBulldozer // - pictureBoxBulldozer.Dock = DockStyle.Fill; - pictureBoxBulldozer.Location = new Point(0, 0); - pictureBoxBulldozer.Name = "pictureBoxBulldozer"; - pictureBoxBulldozer.Size = new Size(884, 461); - pictureBoxBulldozer.SizeMode = PictureBoxSizeMode.AutoSize; - pictureBoxBulldozer.TabIndex = 0; - pictureBoxBulldozer.TabStop = false; + this.pictureBoxBulldozer.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxBulldozer.Location = new System.Drawing.Point(0, 0); + this.pictureBoxBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.pictureBoxBulldozer.Name = "pictureBoxBulldozer"; + this.pictureBoxBulldozer.Size = new System.Drawing.Size(1110, 793); + this.pictureBoxBulldozer.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBoxBulldozer.TabIndex = 0; + this.pictureBoxBulldozer.TabStop = false; // // buttonCreateBulldozer // - buttonCreateBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateBulldozer.Location = new Point(12, 426); - buttonCreateBulldozer.Name = "buttonCreateBulldozer"; - buttonCreateBulldozer.Size = new Size(132, 23); - buttonCreateBulldozer.TabIndex = 1; - buttonCreateBulldozer.Text = "Создать бульдозер"; - buttonCreateBulldozer.UseVisualStyleBackColor = true; - buttonCreateBulldozer.Click += buttonCreateBulldozer_Click; + this.buttonCreateBulldozer.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateBulldozer.Location = new System.Drawing.Point(14, 746); + this.buttonCreateBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonCreateBulldozer.Name = "buttonCreateBulldozer"; + this.buttonCreateBulldozer.Size = new System.Drawing.Size(151, 31); + this.buttonCreateBulldozer.TabIndex = 1; + this.buttonCreateBulldozer.Text = "Создать бульдозер"; + this.buttonCreateBulldozer.UseVisualStyleBackColor = true; + this.buttonCreateBulldozer.Click += new System.EventHandler(this.buttonCreateBulldozer_Click); // // buttonLeft // - buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; - buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Image = Properties.Resources.arrowLeft; - buttonLeft.Location = new Point(759, 422); - buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); - buttonLeft.TabIndex = 2; - buttonLeft.UseVisualStyleBackColor = true; - buttonLeft.Click += ButtonMove_Click; + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::Bulldozer.Properties.Resources.arrowLeft; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonLeft.Image = global::Bulldozer.Properties.Resources.arrowLeft; + this.buttonLeft.Location = new System.Drawing.Point(967, 741); + this.buttonLeft.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(34, 40); + this.buttonLeft.TabIndex = 2; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonDown // - buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonDown.BackgroundImage = Properties.Resources.arrowDown; - buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(795, 422); - buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); - buttonDown.TabIndex = 3; - buttonDown.UseVisualStyleBackColor = true; - buttonDown.Click += ButtonMove_Click; + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::Bulldozer.Properties.Resources.arrowDown; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonDown.Location = new System.Drawing.Point(1009, 741); + this.buttonDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(34, 40); + this.buttonDown.TabIndex = 3; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonRight // - buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonRight.BackgroundImage = Properties.Resources.arrowRight; - buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Image = Properties.Resources.arrowRight; - buttonRight.Location = new Point(831, 422); - buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); - buttonRight.TabIndex = 4; - buttonRight.UseVisualStyleBackColor = true; - buttonRight.Click += ButtonMove_Click; + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::Bulldozer.Properties.Resources.arrowRight; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonRight.Image = global::Bulldozer.Properties.Resources.arrowRight; + this.buttonRight.Location = new System.Drawing.Point(1050, 741); + this.buttonRight.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(34, 40); + this.buttonRight.TabIndex = 4; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); // // buttonUp // - buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonUp.BackgroundImage = Properties.Resources.arrowUp; - buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Image = Properties.Resources.arrowUp; - buttonUp.Location = new Point(795, 386); - buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); - buttonUp.TabIndex = 5; - buttonUp.UseVisualStyleBackColor = true; - buttonUp.Click += ButtonMove_Click; + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::Bulldozer.Properties.Resources.arrowUp; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.buttonUp.Image = global::Bulldozer.Properties.Resources.arrowUp; + this.buttonUp.Location = new System.Drawing.Point(1009, 693); + this.buttonUp.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(34, 40); + this.buttonUp.TabIndex = 5; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); // // comboBoxStrategy // - comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; - comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxStrategy.FormattingEnabled = true; - comboBoxStrategy.Items.AddRange(new object[] { "Движение к центру", "Движение к правой нижней границе" }); - comboBoxStrategy.Location = new Point(642, 9); - comboBoxStrategy.Margin = new Padding(3, 2, 3, 2); - comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(232, 23); - comboBoxStrategy.TabIndex = 6; + this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxStrategy.FormattingEnabled = true; + this.comboBoxStrategy.Items.AddRange(new object[] { + "Движение к центру", + "Движение к правой нижней границе"}); + this.comboBoxStrategy.Location = new System.Drawing.Point(834, 12); + this.comboBoxStrategy.Name = "comboBoxStrategy"; + this.comboBoxStrategy.Size = new System.Drawing.Size(264, 28); + this.comboBoxStrategy.TabIndex = 6; // // buttonStep // - buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonStep.Location = new Point(821, 35); - buttonStep.Name = "buttonStep"; - buttonStep.Size = new Size(52, 23); - buttonStep.TabIndex = 7; - buttonStep.Text = "Шаг"; - buttonStep.UseVisualStyleBackColor = true; - buttonStep.Click += buttonStep_Click; + this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonStep.Location = new System.Drawing.Point(1038, 47); + this.buttonStep.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonStep.Name = "buttonStep"; + this.buttonStep.Size = new System.Drawing.Size(60, 31); + this.buttonStep.TabIndex = 7; + this.buttonStep.Text = "Шаг"; + this.buttonStep.UseVisualStyleBackColor = true; + this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); // // buttonCreateUpgradedBulldozer // - buttonCreateUpgradedBulldozer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateUpgradedBulldozer.Location = new Point(150, 426); - buttonCreateUpgradedBulldozer.Name = "buttonCreateUpgradedBulldozer"; - buttonCreateUpgradedBulldozer.Size = new Size(206, 23); - buttonCreateUpgradedBulldozer.TabIndex = 8; - buttonCreateUpgradedBulldozer.Text = "Создать бульдозер с обвесами"; - buttonCreateUpgradedBulldozer.UseVisualStyleBackColor = true; - buttonCreateUpgradedBulldozer.Click += buttonCreateUpgradedBulldozer_Click; + this.buttonCreateUpgradedBulldozer.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonCreateUpgradedBulldozer.Location = new System.Drawing.Point(171, 746); + this.buttonCreateUpgradedBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonCreateUpgradedBulldozer.Name = "buttonCreateUpgradedBulldozer"; + this.buttonCreateUpgradedBulldozer.Size = new System.Drawing.Size(235, 31); + this.buttonCreateUpgradedBulldozer.TabIndex = 8; + this.buttonCreateUpgradedBulldozer.Text = "Создать бульдозер с обвесами"; + this.buttonCreateUpgradedBulldozer.UseVisualStyleBackColor = true; + this.buttonCreateUpgradedBulldozer.Click += new System.EventHandler(this.buttonCreateUpgradedBulldozer_Click); + // + // buttonSelectBulldozer + // + this.buttonSelectBulldozer.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.buttonSelectBulldozer.Location = new System.Drawing.Point(412, 746); + this.buttonSelectBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonSelectBulldozer.Name = "buttonSelectBulldozer"; + this.buttonSelectBulldozer.Size = new System.Drawing.Size(130, 31); + this.buttonSelectBulldozer.TabIndex = 9; + this.buttonSelectBulldozer.Text = "Выбрать объект"; + this.buttonSelectBulldozer.UseVisualStyleBackColor = true; + this.buttonSelectBulldozer.Click += new System.EventHandler(this.buttonSelectBulldozer_Click); // // FormBulldozer // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(884, 461); - Controls.Add(buttonCreateUpgradedBulldozer); - Controls.Add(buttonStep); - Controls.Add(comboBoxStrategy); - Controls.Add(buttonUp); - Controls.Add(buttonRight); - Controls.Add(buttonDown); - Controls.Add(buttonLeft); - Controls.Add(buttonCreateBulldozer); - Controls.Add(pictureBoxBulldozer); - Name = "FormBulldozer"; - StartPosition = FormStartPosition.CenterScreen; - Text = "Bulldozer"; - ((System.ComponentModel.ISupportInitialize)pictureBoxBulldozer).EndInit(); - ResumeLayout(false); - PerformLayout(); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1110, 793); + this.Controls.Add(this.buttonSelectBulldozer); + this.Controls.Add(this.buttonCreateUpgradedBulldozer); + this.Controls.Add(this.buttonStep); + this.Controls.Add(this.comboBoxStrategy); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonCreateBulldozer); + this.Controls.Add(this.pictureBoxBulldozer); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "FormBulldozer"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Bulldozer"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBulldozer)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + } #endregion @@ -179,5 +203,6 @@ private ComboBox comboBoxStrategy; private Button buttonStep; private Button buttonCreateUpgradedBulldozer; + private Button buttonSelectBulldozer; } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozer.cs b/Bulldozer/Bulldozer/FormBulldozer.cs index cb594fe..f235f47 100644 --- a/Bulldozer/Bulldozer/FormBulldozer.cs +++ b/Bulldozer/Bulldozer/FormBulldozer.cs @@ -5,19 +5,34 @@ namespace Bulldozer { public partial class FormBulldozer : Form { - + /// + /// - + /// private DrawingBulldozer? _drawingBulldozer; - public FormBulldozer() - { - InitializeComponent(); - } - /// /// /// private AbstractStrategy? _abstractStrategy; + /// + /// + /// + public DrawingBulldozer? SelectedBulldozer { get; private set; } + + /// + /// + /// + public FormBulldozer() + { + InitializeComponent(); + _abstractStrategy = null; + SelectedBulldozer = null; + } + + /// + /// + /// private void Draw() { if (_drawingBulldozer == null) @@ -35,7 +50,6 @@ namespace Bulldozer /// /// /// - private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingBulldozer == null) @@ -61,26 +75,58 @@ namespace Bulldozer Draw(); } + /// + /// "" + /// + /// + /// + private void buttonCreateUpgradedBulldozer_Click(object sender, EventArgs e) + { + Random random = new Random(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); // + Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); // . EntityUpgradedBulldozer + ColorDialog colorDialog = new(); + + if (colorDialog.ShowDialog() == DialogResult.OK) + bodyColor = colorDialog.Color; + if (colorDialog.ShowDialog() == DialogResult.OK) + dopColor = colorDialog.Color; + _drawingBulldozer = new DrawingBulldozerUpgraded( + random.Next(100, 300), // + random.Next(1000, 3000), // + bodyColor, additionalColor, dopColor, + Convert.ToBoolean(random.Next(2)), + Convert.ToBoolean(random.Next(2)), + pictureBoxBulldozer.Width, + pictureBoxBulldozer.Height + ); + + _drawingBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + + } + private void buttonCreateBulldozer_Click(object sender, EventArgs e) { Random random = new(); - _drawingBulldozer = new DrawingBulldozer(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)), - pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); - _drawingBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); - Draw(); - } + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog colorDialog = new ColorDialog(); + + + if (colorDialog.ShowDialog() == DialogResult.OK) + { + bodyColor = colorDialog.Color; + } + + Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + _drawingBulldozer = new DrawingBulldozer(random.Next(100, 300), // + random.Next(1000, 3000), // + bodyColor, additionalColor, + pictureBoxBulldozer.Width, + pictureBoxBulldozer.Height); - private void buttonCreateUpgradedBulldozer_Click(object sender, EventArgs e) - { - Random random = new(); - _drawingBulldozer = new DrawingBulldozerUpgraded(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.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - pictureBoxBulldozer.Width, pictureBoxBulldozer.Height); _drawingBulldozer.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -118,5 +164,17 @@ namespace Bulldozer _abstractStrategy = null; } } + + /// + /// + /// + /// + /// + + private void buttonSelectBulldozer_Click(object sender, EventArgs e) + { + SelectedBulldozer = _drawingBulldozer; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs new file mode 100644 index 0000000..e85df62 --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.Designer.cs @@ -0,0 +1,124 @@ +namespace Bulldozer +{ + partial class FormBulldozerCollection + { + /// + /// 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() + { + pictureBoxCollection = new PictureBox(); + panelTools = new Panel(); + textBox = new TextBox(); + buttonUpdateColletion = new Button(); + buttonDeleteBulldozer = new Button(); + buttonAddBulldozer = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panelTools.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Anchor = AnchorStyles.Left; + pictureBoxCollection.BackColor = SystemColors.Control; + pictureBoxCollection.Location = new Point(12, 12); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(711, 571); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; + // + // panelTools + // + panelTools.Controls.Add(textBox); + panelTools.Controls.Add(buttonUpdateColletion); + panelTools.Controls.Add(buttonDeleteBulldozer); + panelTools.Controls.Add(buttonAddBulldozer); + panelTools.Location = new Point(729, 12); + panelTools.Name = "panelTools"; + panelTools.Size = new Size(234, 571); + panelTools.TabIndex = 1; + panelTools.Tag = ""; + // + // textBox + // + textBox.Location = new Point(23, 164); + textBox.Name = "textBox"; + textBox.Size = new Size(193, 23); + textBox.TabIndex = 3; + // + // buttonUpdateColletion + // + buttonUpdateColletion.Location = new Point(23, 263); + buttonUpdateColletion.Name = "buttonUpdateColletion"; + buttonUpdateColletion.Size = new Size(193, 40); + buttonUpdateColletion.TabIndex = 2; + buttonUpdateColletion.Text = "Обновить коллекцию"; + buttonUpdateColletion.UseVisualStyleBackColor = true; + buttonUpdateColletion.Click += ButtonUpdateCollection_Click; + // + // buttonDeleteBulldozer + // + buttonDeleteBulldozer.Location = new Point(23, 193); + buttonDeleteBulldozer.Name = "buttonDeleteBulldozer"; + buttonDeleteBulldozer.Size = new Size(193, 40); + buttonDeleteBulldozer.TabIndex = 1; + buttonDeleteBulldozer.Text = "Удалить объект"; + buttonDeleteBulldozer.UseVisualStyleBackColor = true; + buttonDeleteBulldozer.Click += ButtonDeleteBulldozer_Click; + // + // buttonAddBulldozer + // + buttonAddBulldozer.Location = new Point(23, 32); + buttonAddBulldozer.Name = "buttonAddBulldozer"; + buttonAddBulldozer.Size = new Size(193, 40); + buttonAddBulldozer.TabIndex = 0; + buttonAddBulldozer.Text = "Добавить объект"; + buttonAddBulldozer.UseVisualStyleBackColor = true; + buttonAddBulldozer.Click += ButtonAddBulldozer_Click; + // + // FormBulldozerCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(971, 595); + Controls.Add(panelTools); + Controls.Add(pictureBoxCollection); + Name = "FormBulldozerCollection"; + Text = "Bulldozer Collection"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panelTools.ResumeLayout(false); + panelTools.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private Panel panelTools; + private TextBox textBox; + private Button buttonUpdateColletion; + private Button buttonDeleteBulldozer; + private Button buttonAddBulldozer; + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.cs b/Bulldozer/Bulldozer/FormBulldozerCollection.cs new file mode 100644 index 0000000..15c6f8f --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.cs @@ -0,0 +1,60 @@ +using Bulldozer.DrawingObjects; +using Bulldozer.Generics; +using Bulldozer.MovementStrategy; + + +namespace Bulldozer +{ + public partial class FormBulldozerCollection : Form + { + private readonly BulldozersGenericCollection _bulldozers; + public FormBulldozerCollection() + { + InitializeComponent(); + _bulldozers = new BulldozersGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + + } + private void ButtonAddBulldozer_Click(object sender, EventArgs e) + { + FormBulldozer form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_bulldozers + form.SelectedBulldozer != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + + } + } + + private void ButtonDeleteBulldozer_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + + { + return; + } + int pos = Convert.ToInt32(textBox.Text); + if (_bulldozers - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonUpdateCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); + } + } +} diff --git a/Bulldozer/Bulldozer/FormBulldozerCollection.resx b/Bulldozer/Bulldozer/FormBulldozerCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Bulldozer/Bulldozer/FormBulldozerCollection.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/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs new file mode 100644 index 0000000..a7b11a6 --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/BulldozersGenericCollection.cs @@ -0,0 +1,129 @@ +using Bulldozer.DrawingObjects; +using Bulldozer.MovementStrategy; + +namespace Bulldozer.Generics +{ + internal class BulldozersGenericCollection + where T : DrawingBulldozer + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 300; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 70; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public BulldozersGenericCollection(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 +(BulldozersGenericCollection collect, T? obj) + { + if (obj == null) + return -1; + return collect?._collection.Insert(obj) ?? -1; + } + + public static bool operator -(BulldozersGenericCollection collect, int +pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + return collect._collection.Remove(pos); + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowBulldozers() + { + 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, j * + _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int heightObjCount = _pictureHeight / _placeSizeHeight; + int widthObjCount = _pictureWidth / _placeSizeWidth; + int totalObjects = _collection.Count; + + for (int i = 0; i < totalObjects; i++) + { + T? type = _collection.Get(i); + if (type != null) + { + int col = widthObjCount - 1 - (i % widthObjCount); + int row = heightObjCount - 1 - (i / widthObjCount); + + type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + type?.DrawBulldozer(g); + } + } + } + } +} diff --git a/Bulldozer/Bulldozer/Generics/SetGeneric.cs b/Bulldozer/Bulldozer/Generics/SetGeneric.cs new file mode 100644 index 0000000..74ad077 --- /dev/null +++ b/Bulldozer/Bulldozer/Generics/SetGeneric.cs @@ -0,0 +1,87 @@ +namespace Bulldozer.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + 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 bulldozer) + { + if (_places[Count - 1] != null) + return -1; + return Insert(bulldozer, 0); + + } + + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T bulldozer, int position) + { + if (!(position >= 0 && position < Count)) + return -1; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return -1; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = bulldozer; + return position; + + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + } +} \ No newline at end of file diff --git a/Bulldozer/Bulldozer/MovementStrategy/DrawingObjectBulldozer.cs b/Bulldozer/Bulldozer/MovementStrategy/DrawingObjectBulldozer.cs index 1199a5d..9073766 100644 --- a/Bulldozer/Bulldozer/MovementStrategy/DrawingObjectBulldozer.cs +++ b/Bulldozer/Bulldozer/MovementStrategy/DrawingObjectBulldozer.cs @@ -1,5 +1,6 @@ using Bulldozer.DrawingObjects; + namespace Bulldozer.MovementStrategy { /// diff --git a/Bulldozer/Bulldozer/MovementStrategy/MoveToBorder.cs b/Bulldozer/Bulldozer/MovementStrategy/MoveToBorder.cs index 1133df6..f267d72 100644 --- a/Bulldozer/Bulldozer/MovementStrategy/MoveToBorder.cs +++ b/Bulldozer/Bulldozer/MovementStrategy/MoveToBorder.cs @@ -12,10 +12,10 @@ namespace Bulldozer.MovementStrategy var objParams = GetObjectParameters; return objParams != null - && objParams.RightBorder <= FieldWidth - && objParams.RightBorder + GetStep() >= FieldWidth - && objParams.TopBorder <= FieldHeight - && objParams.DownBorder + GetStep() >= FieldHeight; + && objParams.ObjectMiddleHorizontal >= FieldWidth + && objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth + && objParams.ObjectMiddleVertical >= FieldHeight + && objParams.ObjectMiddleVertical - GetStep() <= FieldHeight; } protected override void MoveToTarget() @@ -26,8 +26,8 @@ namespace Bulldozer.MovementStrategy return; } - var diffX = objParams.RightBorder - FieldWidth; - var diffY = objParams.DownBorder - FieldHeight; + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + var diffY = objParams.ObjectMiddleVertical - FieldHeight; if (Math.Abs(diffX) > GetStep()) { diff --git a/Bulldozer/Bulldozer/MovementStrategy/ObjectParameters.cs b/Bulldozer/Bulldozer/MovementStrategy/ObjectParameters.cs index fffbda4..eb3d12f 100644 --- a/Bulldozer/Bulldozer/MovementStrategy/ObjectParameters.cs +++ b/Bulldozer/Bulldozer/MovementStrategy/ObjectParameters.cs @@ -24,6 +24,7 @@ /// /// Нижняя граница /// + //public int DownBorder => _y + _height; public int DownBorder => _y + _height; /// /// Середина объекта diff --git a/Bulldozer/Bulldozer/Program.cs b/Bulldozer/Bulldozer/Program.cs index f7eefba..f68daeb 100644 --- a/Bulldozer/Bulldozer/Program.cs +++ b/Bulldozer/Bulldozer/Program.cs @@ -9,7 +9,7 @@ namespace Bulldozer.MovementStrategy static void Main() { ApplicationConfiguration.Initialize(); - Application.Run(new FormBulldozer()); + Application.Run(new FormBulldozerCollection()); } } } \ No newline at end of file