LAB4_DONE

This commit is contained in:
Itos 2024-10-03 00:21:27 +03:00
parent 3893cc472d
commit c06d9588bf
13 changed files with 395 additions and 201 deletions

View File

@ -11,7 +11,6 @@ namespace ProjectGasolineTanker.Drawings
public class DrawingGasolineTanker : DrawingTruck
{
public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, bool tank, bool wheel, int width, int height) : base(speed, weight, bodyColor, width, height, 130, 70)
{
if (EntityTruck != null)

View File

@ -50,7 +50,7 @@ namespace ProjectGasolineTanker.Drawings
protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int tankerWidth, int tankerHeight)
{
// TODO: Продумать проверки
if (width < _tankerWidth || height < _tankerHeight)
{
return;
@ -117,11 +117,11 @@ namespace ProjectGasolineTanker.Drawings
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityTruck.BodyColor);
g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 11, 71, 28); //канистра-бак
g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 20, 71, 9); //полосы
g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 11, 71, 28);
g.FillRectangle(additionalBrush, _startPosX + 29, _startPosY + 20, 71, 9);
g.DrawRectangle(pen, _startPosX + 29, _startPosY + 11, 71, 28); //канистра-бак
g.DrawRectangle(pen, _startPosX + 29, _startPosY + 20, 71, 9); //полосы
g.DrawRectangle(pen, _startPosX + 29, _startPosY + 11, 71, 28);
g.DrawRectangle(pen, _startPosX + 29, _startPosY + 20, 71, 9);
Brush additionalBrush1 = new
@ -145,7 +145,6 @@ namespace ProjectGasolineTanker.Drawings
g.FillRectangle(additionalBrush2, _startPosX + 4, _startPosY + 40, 130, 25);
g.DrawLine(pen, _startPosX + 4, _startPosY + 65, _startPosX + 25, _startPosY + 40);
//колёса
Brush gr = new SolidBrush(Color.Gray);
g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 50, 26, 26);
@ -171,9 +170,9 @@ namespace ProjectGasolineTanker.Drawings
//вверх
DirectionType.Up => _startPosY - EntityTruck.Step > 0,
// вправо
DirectionType.Right => _startPosX + _tankerWidth + EntityTruck.Step < _pictureWidth,// TODO: Продумать логику
DirectionType.Right => _startPosX + _tankerWidth + EntityTruck.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + _tankerHeight + EntityTruck.Step < _pictureHeight,// TODO: Продумать логику
DirectionType.Down => _startPosY + _tankerHeight + EntityTruck.Step < _pictureHeight,
_ => false,
};
}

View File

@ -4,14 +4,23 @@ using ProjectGasolineTanker.MovementStratg;
namespace ProjectGasolineTanker
{
/// <summary>
/// Ôîðìà ðàáîòû ñ îáúåêòîì Ãàçîâîç
/// </summary>
public partial class FormGasolineTanker : Form
{
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingTruck? _drawingTruck;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Âûáðàííûé àâòîìîáèëü
/// </summary>
public DrawingTruck? SelectedTruck { get; private set; }
public FormGasolineTanker()
@ -20,7 +29,9 @@ namespace ProjectGasolineTanker
_abstractStrategy = null;
SelectedTruck = null;
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ãðóçîâèêà
/// </summary>
private void Draw()
{
if (_drawingTruck == null)
@ -33,7 +44,11 @@ namespace ProjectGasolineTanker
pictureBoxGasolineTanker.Image = bmp;
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ãàçîâîç"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateProjectGasolineTanker_Click(object sender, EventArgs e)
{
Random random = new();
@ -56,7 +71,11 @@ namespace ProjectGasolineTanker
_drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Ñîçäàòü ProjectGasolineTanker"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateTruck_Click(object sender, EventArgs e)
{
Random random = new();
@ -75,7 +94,11 @@ namespace ProjectGasolineTanker
Draw();
}
/// <summary>
/// Èçìåíåíèå ïîëîæåíèÿ îáúåêòà
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)
@ -100,7 +123,11 @@ namespace ProjectGasolineTanker
}
Draw();
}
/// <summary>
/// Îáðàáîòêà íàæàòèÿ êíîïêè "Øàã"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStep_Click(object sender, EventArgs e)
{
if (_drawingTruck == null)

View File

@ -28,88 +28,155 @@
/// </summary>
private void InitializeComponent()
{
this.buttonAddTruck = new System.Windows.Forms.Button();
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.labelInstruments = new System.Windows.Forms.Label();
this.buttonAdd = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonUpdate = new System.Windows.Forms.Button();
this.buttonDeleteTruck = new System.Windows.Forms.Button();
this.colorDialog = new System.Windows.Forms.ColorDialog();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.label1 = new System.Windows.Forms.Label();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonAddStorage = new System.Windows.Forms.Button();
this.buttonDeleteStorage = new System.Windows.Forms.Button();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
// buttonAddTruck
//
this.buttonAddTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddTruck.Location = new System.Drawing.Point(734, 420);
this.buttonAddTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddTruck.Name = "buttonAddTruck";
this.buttonAddTruck.Size = new System.Drawing.Size(157, 37);
this.buttonAddTruck.TabIndex = 0;
this.buttonAddTruck.Text = "Добавить грузовик";
this.buttonAddTruck.UseVisualStyleBackColor = true;
this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click);
//
// pictureBoxCollection
//
this.pictureBoxCollection.Location = new System.Drawing.Point(2, 1);
this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.pictureBoxCollection.Location = new System.Drawing.Point(1, 12);
this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(803, 376);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.Size = new System.Drawing.Size(668, 381);
this.pictureBoxCollection.TabIndex = 1;
this.pictureBoxCollection.TabStop = false;
//
// labelInstruments
//
this.labelInstruments.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.labelInstruments.AutoSize = true;
this.labelInstruments.Location = new System.Drawing.Point(852, 22);
this.labelInstruments.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.labelInstruments.Location = new System.Drawing.Point(753, 12);
this.labelInstruments.Name = "labelInstruments";
this.labelInstruments.Size = new System.Drawing.Size(83, 15);
this.labelInstruments.TabIndex = 1;
this.labelInstruments.Size = new System.Drawing.Size(136, 28);
this.labelInstruments.TabIndex = 2;
this.labelInstruments.Text = "Инструменты";
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(840, 64);
this.buttonAdd.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(161, 44);
this.buttonAdd.TabIndex = 2;
this.buttonAdd.Text = "Добавить грузовик";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.ButtonAddTruck_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(874, 140);
this.maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(110, 23);
this.maskedTextBoxNumber.TabIndex = 3;
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(840, 167);
this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(165, 45);
this.buttonDelete.TabIndex = 4;
this.buttonDelete.Text = "Удалить грузовик";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveTruck_Click);
//
// buttonUpdate
//
this.buttonUpdate.Location = new System.Drawing.Point(840, 260);
this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.buttonUpdate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUpdate.Location = new System.Drawing.Point(741, 172);
this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonUpdate.Name = "buttonUpdate";
this.buttonUpdate.Size = new System.Drawing.Size(165, 45);
this.buttonUpdate.TabIndex = 5;
this.buttonUpdate.Text = "Обновить";
this.buttonUpdate.Size = new System.Drawing.Size(150, 33);
this.buttonUpdate.TabIndex = 3;
this.buttonUpdate.Text = "Обновить набор";
this.buttonUpdate.UseVisualStyleBackColor = true;
this.buttonUpdate.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);
//
// buttonDeleteTruck
//
this.buttonDeleteTruck.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDeleteTruck.Location = new System.Drawing.Point(734, 537);
this.buttonDeleteTruck.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonDeleteTruck.Name = "buttonDeleteTruck";
this.buttonDeleteTruck.Size = new System.Drawing.Size(157, 37);
this.buttonDeleteTruck.TabIndex = 4;
this.buttonDeleteTruck.Text = "Удалить грузовик";
this.buttonDeleteTruck.UseVisualStyleBackColor = true;
this.buttonDeleteTruck.Click += new System.EventHandler(this.buttonDeleteTruck_Click);
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.maskedTextBoxNumber.Location = new System.Drawing.Point(734, 480);
this.maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.maskedTextBoxNumber.Mask = "00";
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(157, 27);
this.maskedTextBoxNumber.TabIndex = 5;
this.maskedTextBoxNumber.ValidatingType = typeof(int);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(741, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 20);
this.label1.TabIndex = 7;
this.label1.Text = "Наборы";
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 20;
this.listBoxStorages.Location = new System.Drawing.Point(741, 209);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(150, 144);
this.listBoxStorages.TabIndex = 8;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged);
//
// buttonAddStorage
//
this.buttonAddStorage.Location = new System.Drawing.Point(741, 132);
this.buttonAddStorage.Name = "buttonAddStorage";
this.buttonAddStorage.Size = new System.Drawing.Size(150, 33);
this.buttonAddStorage.TabIndex = 9;
this.buttonAddStorage.Text = "Добавить набор";
this.buttonAddStorage.UseVisualStyleBackColor = true;
this.buttonAddStorage.Click += new System.EventHandler(this.buttonAddStorage_Click);
//
// buttonDeleteStorage
//
this.buttonDeleteStorage.Location = new System.Drawing.Point(741, 360);
this.buttonDeleteStorage.Name = "buttonDeleteStorage";
this.buttonDeleteStorage.Size = new System.Drawing.Size(150, 33);
this.buttonDeleteStorage.TabIndex = 10;
this.buttonDeleteStorage.Text = "Удалить набор";
this.buttonDeleteStorage.UseVisualStyleBackColor = true;
this.buttonDeleteStorage.TabIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged);
this.buttonDeleteStorage.Click += new System.EventHandler(this.buttonDeleteStorage_Click);
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(741, 99);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(150, 27);
this.textBoxStorageName.TabIndex = 11;
//
// FormTruckCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1152, 382);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.buttonDelete);
this.ClientSize = new System.Drawing.Size(911, 689);
this.Controls.Add(this.textBoxStorageName);
this.Controls.Add(this.buttonDeleteStorage);
this.Controls.Add(this.buttonAddStorage);
this.Controls.Add(this.listBoxStorages);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonAddTruck);
this.Controls.Add(this.maskedTextBoxNumber);
this.Controls.Add(this.buttonAdd);
this.Controls.Add(this.buttonDeleteTruck);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.labelInstruments);
this.Controls.Add(this.pictureBoxCollection);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormTruckCollection";
this.Text = "Грузовик";
this.Text = "Набор грузовиков";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -118,11 +185,17 @@
#endregion
private Button buttonAddTruck;
private PictureBox pictureBoxCollection;
private Label labelInstruments;
private Button buttonAdd;
private TextBox maskedTextBoxNumber;
private Button buttonDelete;
private Button buttonUpdate;
private Button buttonDeleteTruck;
private ColorDialog colorDialog;
private MaskedTextBox maskedTextBoxNumber;
private Label label1;
private ListBox listBoxStorages;
private Button buttonAddStorage;
private Button buttonDeleteStorage;
private TextBox textBoxStorageName;
}
}

View File

@ -7,33 +7,92 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.Generic;
using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.MovementStratg;
using ProjectGasolineTanker;
namespace ProjectGasolineTanker
{
public partial class FormTruckCollection : Form
{
private readonly TruckGenericCollection<DrawingTruck, DrawingObjectTruck> _truck;
private readonly TruckGenericStorage _storage;
public FormTruckCollection()
{
InitializeComponent();
_truck = new TruckGenericCollection<DrawingTruck, DrawingObjectTruck>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new TruckGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
private void ButtonAddTruck_Click(object sender, EventArgs e)
private void buttonAddStorage_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTruck();
}
private void buttonDeleteStorage_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void buttonAddTruck_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
MessageBox.Show("Не выбран набор");
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormGasolineTanker form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_truck + form.SelectedTruck != -1)
if (obj + form.SelectedTruck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _truck.ShowTruck();
pictureBoxCollection.Image = obj.ShowTruck();
}
else
{
@ -42,44 +101,55 @@ namespace ProjectGasolineTanker
}
}
private void ButtonRemoveTruck_Click(object sender, EventArgs e)
private void buttonDeleteTruck_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = 0;
try
{
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
}
catch
{
MessageBox.Show("Ошибка ввода данных");
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (_truck - (_truck.ReturnLength() - pos))
if (obj - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _truck.ShowTruck();
pictureBoxCollection.Image = obj.ShowTruck();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
private void buttonUpdate_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _truck.ShowTruck();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowTruck();
}
}
}

View File

@ -57,4 +57,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="colorDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>36</value>
</metadata>
</root>

View File

@ -10,97 +10,75 @@ namespace ProjectGasolineTanker.Generic
where T : class
{
private readonly T?[] _places;
private readonly List<T?> _places;
public int Count => _places.Length - 1;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T truck)
{
int pos = Count - 1;
if (_places[Count] != null)
{
for (int i = pos; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
_places.Insert(0, truck);
return 0;
}
}
for (int i = pos + 1; i <= Count; ++i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = truck;
return pos;
}
public bool Insert(T truck, int position)
{
// TODO проверка позиции
if (position < 0 || position > Count)
if (position < 0 || position >= Count || Count >= _maxCount)
{
return false;
}
if (_places[Count] != null)
{
int pos = Count;
for (int i = Count; i > 0; --i)
{
if (_places[i] == null)
{
pos = i;
break;
}
}
for (int i = Count; i >= pos; --i)
{
_places[i - 1] = _places[i];
}
}
_places[Count] = truck;
_places.Insert(position, truck);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position > Count)
if (position < 0 || position >= Count)
{
return false;
}
_places[position] = null;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position > Count)
get
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
set
{
if (position < 0 || position > Count || Count >= _maxCount)
{
return;
}
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetTruck(int? maxTruck = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTruck.HasValue && i == maxTruck.Value)
{
yield break;
}
}
}
}
}

View File

@ -21,6 +21,7 @@ namespace ProjectGasolineTanker.Generic
private readonly SetGeneric<T> _collection;
public TruckGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
@ -40,24 +41,18 @@ namespace ProjectGasolineTanker.Generic
}
public static bool operator -(TruckGenericCollection<T, U> collect, int pos)
{
if (collect._collection.Get(pos) == null)
if (collect._collection.GetTruck(pos) == null)
{
return false;
}
return collect?._collection.Remove(pos) ?? false;
}
public int ReturnLength()
{
return _collection.Count;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowTruck()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
@ -67,7 +62,6 @@ namespace ProjectGasolineTanker.Generic
return bmp;
}
private void DrawBackground(Graphics gr)
{
Pen pen = new(Color.Black, 3);
@ -75,44 +69,31 @@ namespace ProjectGasolineTanker.Generic
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
// линия разметки
gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
}
private void DrawObjects(Graphics g)
{
int x = 0;
int y = 0;
int index = -1;
int y = _pictureHeight / _placeSizeHeight - 1;
for (int i = 0; i <= _collection.Count; ++i)
foreach (var truck in _collection.GetTruck())
{
if (truck != null)
{
if (x > _pictureWidth / _placeSizeWidth - 1)
{
DrawingTruck _truck = _collection.Get(i);
x = 0;
y = 0;
if (_truck != null)
{
index = i;
while (index - _pictureWidth / _placeSizeWidth >= 0)
{
y++;
index -= _pictureWidth / _placeSizeWidth;
--y;
}
if (index > 0)
{
x += index;
}
x = _pictureWidth / _placeSizeWidth - 1 - x;
_truck.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
_truck.DrawTransport(g);
truck.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
truck.DrawTransport(g);
++x;
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectGasolineTanker.Drawings;
using ProjectGasolineTanker.MovementStratg;
namespace ProjectGasolineTanker.Generic
{
internal class TruckGenericStorage
{
readonly Dictionary<string, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>> _TruckStorages;
public List<string> Keys => _TruckStorages.Keys.ToList();
//Ширина окна отрисовки
private readonly int _pictureWidth;
//Высота окна отрисовки
private readonly int _pictureHeight;
public TruckGenericStorage(int pictureWidth, int pictureHeight)
{
_TruckStorages = new Dictionary<string, TruckGenericCollection<DrawingTruck, DrawingObjectTruck>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
foreach (string nameStorage in Keys)
{
if (nameStorage == name)
{
MessageBox.Show("Набор с заданным именем уже занят", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
_TruckStorages.Add(name, new TruckGenericCollection<DrawingTruck, DrawingObjectTruck>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (_TruckStorages.ContainsKey(name))
{
_TruckStorages.Remove(name);
}
}
public TruckGenericCollection<DrawingTruck, DrawingObjectTruck>? this[string ind]
{
get
{
if (_TruckStorages.ContainsKey(ind))
{
return _TruckStorages[ind];
}
return null;
}
}
}
}

View File

@ -9,7 +9,6 @@ using ProjectGasolineTanker.Entities;
namespace ProjectGasolineTanker.MovementStratg
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;

View File

@ -12,15 +12,11 @@ namespace ProjectGasolineTanker.MovementStratg
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}
}

View File

@ -6,7 +6,9 @@ using System.Threading.Tasks;
namespace ProjectGasolineTanker.MovementStratg
{
/// <summary>
/// Стратегия перемещения объекта в центр экрана
/// </summary>
public class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
namespace ProjectGasolineTanker.MovementStratg
{
public class ObjectParameters
{
private readonly int _x;