Pyatakov K.M LabWork3 #4

Closed
ker73rus wants to merge 3 commits from LabWork3 into LabWork2
16 changed files with 791 additions and 17 deletions

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cars
namespace Stormtrooper
{
internal abstract class AbstractMap
{
@ -103,9 +103,9 @@ namespace Cars
break;
}
for (int i = left; i < right; i++)
for (int i = left; i < right && i < _map.GetLength(0); i++)
{
for (int j = top; j < bottom; j++)
for (int j = top; j < bottom && j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{

View File

@ -1,5 +1,4 @@
using Cars;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

View File

@ -1,5 +1,4 @@
using Cars;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal enum Direction
public enum Direction
{
None,

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal class DrawningMilitaryAirplane
public class DrawningMilitaryAirplane
{
/// <summary>
/// Класс-сущность

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Stormtrooper
{
internal class EntityMilitaryAirplane
public class EntityMilitaryAirplane
{
public int Speed { get; private set; }
public int Weight { get; private set; }

View File

@ -1,5 +1,4 @@
using Cars;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

View File

@ -0,0 +1,214 @@
namespace Stormtrooper
{
partial class FormMapWithSetAirplane
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemove = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonCreate = new System.Windows.Forms.Button();
this.comboBoxMapSelector = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.buttonRight);
this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
this.groupBoxTools.Controls.Add(this.buttonRemove);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonCreate);
this.groupBoxTools.Controls.Add(this.comboBoxMapSelector);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(600, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(200, 450);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
//
// buttonRight
//
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.Location = new System.Drawing.Point(119, 402);
this.buttonRight.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(35, 35);
this.buttonRight.TabIndex = 18;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.Location = new System.Drawing.Point(35, 402);
this.buttonLeft.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(35, 35);
this.buttonLeft.TabIndex = 17;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.Location = new System.Drawing.Point(77, 403);
this.buttonDown.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(35, 35);
this.buttonDown.TabIndex = 16;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.Location = new System.Drawing.Point(77, 361);
this.buttonUp.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(35, 35);
this.buttonUp.TabIndex = 15;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(8, 323);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(174, 23);
this.buttonShowOnMap.TabIndex = 14;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(8, 275);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(174, 23);
this.buttonShowStorage.TabIndex = 13;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonRemove
//
this.buttonRemove.Location = new System.Drawing.Point(7, 172);
this.buttonRemove.Name = "buttonRemove";
this.buttonRemove.Size = new System.Drawing.Size(174, 23);
this.buttonRemove.TabIndex = 12;
this.buttonRemove.Text = "Удалить";
this.buttonRemove.UseVisualStyleBackColor = true;
this.buttonRemove.Click += new System.EventHandler(this.ButtonRemove_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 122);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
this.maskedTextBoxPosition.TabIndex = 11;
this.maskedTextBoxPosition.ValidatingType = typeof(int);
//
// buttonCreate
//
this.buttonCreate.Location = new System.Drawing.Point(6, 74);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(175, 23);
this.buttonCreate.TabIndex = 10;
this.buttonCreate.Text = "Добавить";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonAdd_Click);
//
// comboBoxMapSelector
//
this.comboBoxMapSelector.BackColor = System.Drawing.SystemColors.Window;
this.comboBoxMapSelector.FormattingEnabled = true;
this.comboBoxMapSelector.Items.AddRange(new object[] {
"Простая карта",
"Опасная карта",
"Облачная карта"});
this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 22);
this.comboBoxMapSelector.Name = "comboBoxMapSelector";
this.comboBoxMapSelector.Size = new System.Drawing.Size(175, 23);
this.comboBoxMapSelector.TabIndex = 9;
this.comboBoxMapSelector.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;
//
// FormMapWithSetAirplane
//
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.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetAirplane";
this.Text = "FormMapWithSetAirplane";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private PictureBox pictureBox;
private Button buttonShowOnMap;
private Button buttonShowStorage;
private Button buttonRemove;
private MaskedTextBox maskedTextBoxPosition;
private Button buttonCreate;
private ComboBox comboBoxMapSelector;
private Button buttonRight;
private Button buttonLeft;
private Button buttonDown;
private Button buttonUp;
}
}

View File

@ -0,0 +1,170 @@
using Cars;
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;
using static System.Windows.Forms.DataFormats;
namespace Stormtrooper
{
public partial class FormMapWithSetAirplane : Form
{
private MapWithSetAirplaneGeneric<DrawningObject, AbstractMap> _mapAirsCollectionGeneric;
public FormMapWithSetAirplane()
{
InitializeComponent();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxMapSelector.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Опасная карта":
map = new DangerMap();
break;
case "Облачная карта":
map = new CloudMap();
break;
}
if (map != null)
{
_mapAirsCollectionGeneric = new MapWithSetAirplaneGeneric<DrawningObject, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapAirsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAdd_Click(object sender, EventArgs e)
{
if (_mapAirsCollectionGeneric == null)
{
return;
}
MainForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if(form.SelectedAirplane != null)
{
DrawningObject airplane = new (form.SelectedAirplane);
if (_mapAirsCollectionGeneric + airplane != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapAirsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
else
{
MessageBox.Show("Объект не создан");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemove_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapAirsCollectionGeneric - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapAirsCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapAirsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapAirsCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapAirsCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapAirsCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapAirsCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapAirsCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -40,6 +40,7 @@ namespace Stormtrooper
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonCreateMod = new System.Windows.Forms.Button();
this.button_Add = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxAirplane)).BeginInit();
this.toolStripStatus.SuspendLayout();
this.SuspendLayout();
@ -157,11 +158,22 @@ namespace Stormtrooper
this.buttonCreateMod.UseVisualStyleBackColor = true;
this.buttonCreateMod.Click += new System.EventHandler(this.buttonCreateMod_Click);
//
// button_Add
//
this.button_Add.Location = new System.Drawing.Point(573, 600);
this.button_Add.Name = "button_Add";
this.button_Add.Size = new System.Drawing.Size(75, 23);
this.button_Add.TabIndex = 8;
this.button_Add.Text = "Добавить";
this.button_Add.UseVisualStyleBackColor = true;
this.button_Add.Click += new System.EventHandler(this.button_Add_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1343, 703);
this.Controls.Add(this.button_Add);
this.Controls.Add(this.buttonCreateMod);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonLeft);
@ -194,6 +206,7 @@ namespace Stormtrooper
private System.Windows.Forms.Button buttonLeft;
private System.Windows.Forms.Button buttonRight;
private Button buttonCreateMod;
private Button button_Add;
}
}

View File

@ -13,6 +13,7 @@ namespace Stormtrooper
public partial class MainForm : Form
{
DrawningMilitaryAirplane _airplane;
public DrawningMilitaryAirplane SelectedAirplane { get; private set; }
public MainForm()
{
InitializeComponent();
@ -28,9 +29,10 @@ namespace Stormtrooper
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new Random();
Random rnd = new Random();
_airplane = new DrawningMilitaryAirplane(10, 50);
_airplane.SetPosition(random.Next(100,150), random.Next(100,150), pictureBoxAirplane.Width, pictureBoxAirplane.Height);
_airplane.SetPosition(rnd.Next(100,150), rnd.Next(100,150), pictureBoxAirplane.Width, pictureBoxAirplane.Height);
SetData();
Draw();
}
@ -73,12 +75,24 @@ namespace Stormtrooper
{
Random random = new Random();
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;
}
_airplane = new DrawningStormtrooper(random.Next(10,100),random.Next(50,250),random.Next(1,100),
Color.FromArgb(random.Next(0,256), random.Next(0, 256), random.Next(0, 256)),
color,
Convert.ToBoolean(random.Next(0,2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
_airplane.SetPosition(random.Next(100, 150), random.Next(100, 150), pictureBoxAirplane.Width, pictureBoxAirplane.Height);
SetData();
Draw();
}
private void button_Add_Click(object sender, EventArgs e)
{
SelectedAirplane = _airplane;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,193 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class MapWithSetAirplaneGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 210;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 110;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetAirplaneGeneric<T> _setAirs;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetAirplaneGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAirs = new SetAirplaneGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="air"></param>
/// <returns></returns>
public static int operator +(MapWithSetAirplaneGeneric<T, U> map, T air)
{
return map._setAirs.Insert(air);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetAirplaneGeneric<T, U> map, int position)
{
return map._setAirs.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawAirs(gr);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns></returns>
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setAirs.Count; i++)
{
var air = _setAirs.Get(i);
if (air != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, air);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Перемещение объекта по крате
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setAirs.Count - 1;
for (int i = 0; i < _setAirs.Count; i++)
{
if (_setAirs.Get(i) == null)
{
for (; j > i; j--)
{
var air = _setAirs.Get(j);
if (air != null)
{
_setAirs.Insert(air, i);
_setAirs.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
g.FillRectangle(new SolidBrush(Color.DarkGray),0,0,_pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
g.FillRectangle(new SolidBrush(Color.Gray), i * _placeSizeWidth, 0, _placeSizeWidth / 2, _placeSizeHeight * (_pictureHeight / _placeSizeHeight));
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);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawAirs(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int currentWidth = width - 1;
int currentHeight = 0;
for (int i = 0; i < _setAirs.Count; i++)
{
_setAirs.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
_setAirs.Get(i)?.DrawningObject(g);
if (currentWidth > 0)
currentWidth--;
else
{
currentWidth = width - 1;
currentHeight++;
}
if (currentHeight > height) return;
}
}
}
}

View File

@ -16,7 +16,7 @@ namespace Stormtrooper
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMap());
Application.Run(new FormMapWithSetAirplane());
}
}
}

View File

@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Stormtrooper
{
internal class SetAirplaneGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetAirplaneGeneric(int count)
{
_places = new T[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="airplane">Добавляемый самолёт</param>
/// <returns></returns>
public int Insert(T airplane)
{
Insert(airplane, 0);
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="airplane">Добавляемый самолёт</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T airplane, int position)
{
if (position < 0 || position >= Count)
{
return -1;
}
if (_places[position] == null || _places[position] == default(T))
{
_places[position] = airplane;
return position;
}
else
{
bool hasEmptySpace = false;
int indexOfEmptyPlace = 0;
for (int i = position + 1; i < Count; ++i)
{
if (_places[i] == null || _places[i] == default(T))
{
hasEmptySpace = true;
indexOfEmptyPlace = i;
break;
}
}
if (hasEmptySpace)
{
for (int i = indexOfEmptyPlace; i > position; --i)
{
_places[i] = _places[i - 1];
}
_places[position] = airplane;
return position;
}
else
{
return -1;
}
}
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
T memoryObj = _places[position];
_places[position] = null;
return memoryObj;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
return _places[position];
}
}
}

View File

@ -1,4 +1,4 @@
namespace Cars
namespace Stormtrooper
{
/// <summary>
/// Простая реализация абсрактного класса AbstractMap