laba05.fixed

This commit is contained in:
Илья Федотов 2024-02-18 12:11:49 +04:00
parent f1f1ee5917
commit 84c4b43dd4
8 changed files with 75 additions and 66 deletions

View File

@ -11,7 +11,7 @@ namespace Bulldoser.Drawings
public class DrawingBulldoser : DrawingTractor
{
public DrawingBulldoser(int speed, double weight, Color bodyColor, Color additionalColor,
bool otval, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 180, 120)
bool otval, bool seifBatteries, int width, int height) : base(speed, weight, bodyColor, width, height, 140, 123)
{
if (EntityTractor != null)
{

View File

@ -33,42 +33,39 @@ namespace Bulldoser.Drawings
public DrawingTractor(int speed, double weight, Color bodyColor, int width, int heigth)
{
if (width < _tractWidth || heigth < _tractHeight)
if ((heigth > _tractHeight) && (width > _pictureWidth))
{
return;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
_pictureWidth = width;
_pictureHeight = heigth;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
protected DrawingTractor(int speed, double weight, Color bodyColor, int width,
int height, int tractWidth, int tractHeight)
{
if (width < _tractWidth || height < _tractHeight)
if ((height > tractHeight) && (width > tractWidth))
{
return;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
_pictureWidth = width;
_pictureHeight = height;
_tractWidth = tractWidth;
_tractHeight = tractHeight;
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
//Установка позиции
public void SetPosition(int x, int y)
{
if (x < 0 || x + _tractWidth > _pictureWidth)
if ((x < 0 || y < 0) || (x + _tractWidth > _pictureWidth || y + _tractHeight > _pictureHeight))
{
x = _pictureWidth - tractWidth;
x = 0;
y = 0;
}
if (y < 0 || y + _tractHeight > _pictureHeight)
{
y = _pictureHeight - _tractHeight;
else {
_startPosX = x;
_startPosY = y;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)

View File

@ -41,8 +41,8 @@ namespace Bulldoser
dopColor = colorDialog.Color;
}
_drawingTractor = new DrawingBulldoser(random.Next(100, 300), random.Next(1000, 3000), color, dopColor,
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBulldoser.Width, pictureBoxBulldoser.Height);
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),
pictureBoxBulldoser.Width, pictureBoxBulldoser.Height);
_drawingTractor.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}

View File

@ -46,9 +46,11 @@
//
// pictureBoxCollections
//
pictureBoxCollections.Location = new Point(12, 6);
pictureBoxCollections.Dock = DockStyle.Left;
pictureBoxCollections.Location = new Point(0, 0);
pictureBoxCollections.Name = "pictureBoxCollections";
pictureBoxCollections.Size = new Size(570, 432);
pictureBoxCollections.Size = new Size(588, 450);
pictureBoxCollections.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollections.TabIndex = 1;
pictureBoxCollections.TabStop = false;
//
@ -59,7 +61,7 @@
groupBoxTools.Controls.Add(buttonUpdateCollection);
groupBoxTools.Controls.Add(buttonRemoveTransport);
groupBoxTools.Controls.Add(buttonAddTransport);
groupBoxTools.Location = new Point(588, 6);
groupBoxTools.Location = new Point(594, 6);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(200, 432);
groupBoxTools.TabIndex = 2;
@ -172,6 +174,7 @@
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
#endregion

View File

@ -74,27 +74,27 @@ namespace Bulldoser
{
return;
}
var formBulldozerConfig = new FormBulldoserConfig();
formBulldozerConfig.AddEvent(usta =>
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
if (listBoxStorage.SelectedIndex != -1)
return;
}
FormBulldoserConfig form = new(pictureBoxCollections.Width, pictureBoxCollections.Height);
form.Show();
Action<DrawingTractor>? TractorDelegate = new((m) =>
{
bool q = (obj + m);
if (q)
{
var obj = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty];
if (obj != null)
{
if (obj + usta != 1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollections.Image = obj.ShowTractors();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
MessageBox.Show("Объект добавлен");
pictureBoxCollections.Image = obj.ShowTractors();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
});
formBulldozerConfig.Show();
form.AddEvent(TractorDelegate);
}
private void ButtonRemoveTractor_Click(object sender, EventArgs e)

View File

@ -25,7 +25,10 @@ namespace Bulldoser
/// <summary>
/// Конструктор
/// </summary>
public FormBulldoserConfig()
///
private readonly int PictureWidth;
private readonly int PictureHeight;
public FormBulldoserConfig(int width, int height)
{
InitializeComponent();
panelBlack.MouseDown += panelColor_MouseDown;
@ -38,6 +41,8 @@ namespace Bulldoser
panelBlue.MouseDown += panelColor_MouseDown;
// TODO buttonCancel.Click with lambda
buttonCancel.Click += (sender, e) => Close();
PictureWidth = width;
PictureHeight = height;
}
private void DrawBulldozer()
{
@ -82,12 +87,13 @@ namespace Bulldoser
switch (e.Data?.GetData(DataFormats.Text).ToString())
{
case "labelModifiedObject":
_bulldozer = new DrawingBulldoser((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White, Color.Black, checkBoxSeifBatteries.Checked,
checkBoxOtval.Checked, pictureBoxObject.Width, pictureBoxObject.Height);
_bulldozer = new DrawingBulldoser((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White,
Color.Black, checkBoxSeifBatteries.Checked,checkBoxOtval.Checked,
PictureWidth, PictureHeight);
break;
case "labelSimpleObject":
_bulldozer = new DrawingTractor((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White, pictureBoxObject.Width,
pictureBoxObject.Height);
_bulldozer = new DrawingTractor((int)numericUpDownSpeed.Value, (double)numericUpDownWeight.Value, Color.White,
PictureWidth, PictureHeight);
break;
}
DrawBulldozer();

View File

@ -1,5 +1,6 @@
using Bulldoser.Drawings;
using Bulldoser.MovementStrategy;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
@ -20,7 +21,6 @@ namespace Bulldoser.Generic
private readonly SetGeneric<T> _collection;
public TractorGenericCollection(int picWidth, int picHeight)
{
// высчитываем размер массива для setgeneric
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
@ -29,13 +29,13 @@ namespace Bulldoser.Generic
_collection = new SetGeneric<T>(width * height);
}
/// Перегрузка оператора сложения
public static int operator +(TractorGenericCollection<T, U> collect, T tract)
public static bool operator +(TractorGenericCollection<T, U> collect, T tract)
{
if (tract == null)
{
return -1;
return false;
}
return collect._collection.Insert(tract);
return collect?._collection.Insert(tract)??false;
}
public static T? operator -(TractorGenericCollection<T, U> collect, int
pos)
@ -78,18 +78,18 @@ namespace Bulldoser.Generic
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
// Получение объекта
var obj = _collection[i];
// Установка позиции
obj?.SetPosition(
//переделать позиции
(int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)),
(int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight))
) ;
// Прорисовка объекта
obj?.DrawTransport(g);
int i = 0;
foreach (var tractor in _collection.GetTractors()) {
if (tractor != null) {
// Получение объекта
// Установка позиции
int row = height - 1 - (i / width);
int col = width - 1 - (i % width);
tractor.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight);
// Прорисовка объекта
tractor?.DrawTransport(g);
}
i++;
}
}
}

View File

@ -16,15 +16,17 @@ namespace Bulldoser.Generic
_places = new List<T?>(count);
_maxCount = count;
}
public int Insert(T tract)
public bool Insert(T tract)
{
return Insert(tract, 0);
if (_places.Count == _maxCount) return false;
Insert(tract, 0 );
return true;
}
public int Insert(T tract, int position)
public bool Insert(T tract, int position)
{
if (position < 0 || position >= _maxCount) return -1;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) return false;
_places.Insert(position, tract);
return position;
return true;
}
public T? Remove(int position)
@ -41,15 +43,16 @@ namespace Bulldoser.Generic
{
get
{
if (position < 0 || position >= Count) return null;
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
public IEnumerable<T?> GetTractors(int? maxTracts = null)