This commit is contained in:
shadowik 2022-10-04 02:03:01 +04:00
parent 906bd4a4e5
commit c84fb1538f
5 changed files with 132 additions and 104 deletions

View File

@ -34,9 +34,7 @@ namespace DoubleDeckerBus
return DrawMapWithObject(); return DrawMapWithObject();
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемом направлении
_drawingObject.MoveObject(direction); _drawingObject.MoveObject(direction);
bool collision = CheckCollision(); bool collision = CheckCollision();
@ -69,16 +67,9 @@ namespace DoubleDeckerBus
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height); _drawingObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
while (CheckCollision()) {
x = _random.Next(0, 10); return !CheckCollision();
y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
}
return true;
} }
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()
{ {

View File

@ -29,17 +29,17 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxSettings = new System.Windows.Forms.GroupBox(); this.groupBoxSettings = new System.Windows.Forms.GroupBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonAddBus = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonDeleteBus = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonDeleteBus = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddBus = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxSettings.SuspendLayout(); this.groupBoxSettings.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -64,72 +64,6 @@
this.groupBoxSettings.TabStop = false; this.groupBoxSettings.TabStop = false;
this.groupBoxSettings.Text = "Инструменты"; this.groupBoxSettings.Text = "Инструменты";
// //
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(6, 87);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(188, 36);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автобус";
this.buttonAddBus.UseVisualStyleBackColor = true;
this.buttonAddBus.Click += new System.EventHandler(this.ButtonAddBus_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 152);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonDeleteBus
//
this.buttonDeleteBus.Location = new System.Drawing.Point(6, 181);
this.buttonDeleteBus.Name = "buttonDeleteBus";
this.buttonDeleteBus.Size = new System.Drawing.Size(188, 36);
this.buttonDeleteBus.TabIndex = 3;
this.buttonDeleteBus.Text = "Удалить автобус";
this.buttonDeleteBus.UseVisualStyleBackColor = true;
this.buttonDeleteBus.Click += new System.EventHandler(this.ButtonRemoveBus_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 261);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(182, 35);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(6, 320);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(182, 35);
this.button1.TabIndex = 5;
this.button1.Text = "Посмотреть карту";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonRight // buttonRight
// //
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
@ -186,6 +120,76 @@
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// button1
//
this.button1.Location = new System.Drawing.Point(6, 320);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(182, 35);
this.button1.TabIndex = 5;
this.button1.Text = "Посмотреть карту";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 261);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(182, 35);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonDeleteBus
//
this.buttonDeleteBus.Location = new System.Drawing.Point(6, 181);
this.buttonDeleteBus.Name = "buttonDeleteBus";
this.buttonDeleteBus.Size = new System.Drawing.Size(188, 36);
this.buttonDeleteBus.TabIndex = 3;
this.buttonDeleteBus.Text = "Удалить автобус";
this.buttonDeleteBus.UseVisualStyleBackColor = true;
this.buttonDeleteBus.Click += new System.EventHandler(this.ButtonRemoveBus_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 152);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(188, 23);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(6, 87);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(188, 36);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автобус";
this.buttonAddBus.UseVisualStyleBackColor = true;
this.buttonAddBus.Click += new System.EventHandler(this.ButtonAddBus_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Водная карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(188, 23);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.Text = "Простая карта";
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetBuses // FormMapWithSetBuses
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);

View File

@ -18,6 +18,9 @@ namespace DoubleDeckerBus
public FormMapWithSetBuses() public FormMapWithSetBuses()
{ {
InitializeComponent(); InitializeComponent();
AbstractMap map = new SimpleMap();
_mapBusesCollectionGeneric = new MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
} }
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
@ -28,6 +31,9 @@ namespace DoubleDeckerBus
case "Простая карта": case "Простая карта":
map = new SimpleMap(); map = new SimpleMap();
break; break;
case "Водная карта":
map = new WaterMap();
break;
} }
if (map != null) if (map != null)
{ {
@ -49,7 +55,14 @@ namespace DoubleDeckerBus
FormBus form = new(); FormBus form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (form.SelectedBus == null)
{
MessageBox.Show("Вы не создали объект");
return;
}
DrawingObjectBus bus = new(form.SelectedBus); DrawingObjectBus bus = new(form.SelectedBus);
if (_mapBusesCollectionGeneric + bus) if (_mapBusesCollectionGeneric + bus)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");

View File

@ -94,17 +94,14 @@ namespace DoubleDeckerBus
} }
} }
} }
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g) private void DrawBackground(Graphics g)
{ {
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) 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, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
} }
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
@ -113,10 +110,27 @@ namespace DoubleDeckerBus
private void DrawBuses(Graphics g) private void DrawBuses(Graphics g)
{ {
for (int i = 0; i < _setBuses.Count; i++) int width = _pictureWidth / _placeSizeWidth;
{ int height = _pictureHeight / _placeSizeHeight;
// TODO установка позиции
int currentWidth = 0;
int currentHeight = 0;
for (int i = 0; i < _setBuses.Count; i++) {
_setBuses.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
_setBuses.Get(i)?.DrawingObject(g); _setBuses.Get(i)?.DrawingObject(g);
if (currentHeight < height)
currentHeight++;
else
{
currentHeight = 0;
currentWidth++;
}
if (currentWidth > width) return;
} }
} }
} }

View File

@ -12,39 +12,45 @@ namespace DoubleDeckerBus
private readonly T[] _places; private readonly T[] _places;
public int Count => _places.Length; public int Count => _places.Length;
private int BusyPlaces = 0;
public SetBusesGeneric(int count) public SetBusesGeneric(int count)
{ {
_places = new T[count]; _places = new T[count];
} }
public bool Insert(T car) public bool Insert(T bus)
{ {
// TODO вставка в начало набора return Insert(bus, 0);
return true;
} }
public bool Insert(T bus, int position) public bool Insert(T bus, int position)
{ {
// TODO проверка позиции if (position < 0 || position >= _places.Length || BusyPlaces == _places.Length) return false;
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент BusyPlaces++;
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента while (_places[position] != null) {
// TODO вставка по позиции for (int i = _places.Length - 1; i > 0; --i) {
if (_places[i] == null && _places[i - 1] != null) {
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = bus; _places[position] = bus;
return true; return true;
} }
public bool Remove(int position) public bool Remove(int position)
{ {
// TODO проверка позиции if (position < 0 || position >= _places.Length) return false;
// TODO удаление объекта из массива, присовив элементу массива значение null _places[position] = null;
return true; return true;
} }
public T Get(int position) public T Get(int position)
{ {
// TODO проверка позиции if (position < 0 || position >= _places.Length) return null;
return _places[position]; return _places[position];
} }
} }