Изменения из базовой части 6 лабы
This commit is contained in:
parent
a5f607c8c0
commit
c33afe18fd
@ -43,5 +43,9 @@ namespace AirBomber
|
||||
/// <param name="drawningObject">The drawning object.</param>
|
||||
/// <returns></returns>
|
||||
public static explicit operator DrawningAirplane(DrawningObject drawningObject) => drawningObject._airplane;
|
||||
|
||||
public string GetInfo() => _airplane?.GetDataForSave();
|
||||
|
||||
public static IDrawningObject Create(string data) => new DrawningObject(data.CreateDrawningAirplane());
|
||||
}
|
||||
}
|
||||
|
66
AirBomber/AirBomber/ExtentionAirplane.cs
Normal file
66
AirBomber/AirBomber/ExtentionAirplane.cs
Normal file
@ -0,0 +1,66 @@
|
||||
namespace AirBomber
|
||||
{
|
||||
/// <summary>
|
||||
/// Расширение для класса DrawningAirplane
|
||||
/// </summary>
|
||||
internal static class ExtentionAirplane
|
||||
{
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Получение цвета из строки 16-ричных чисел, либо из его имени
|
||||
/// </summary>
|
||||
/// <param name="hexOrName"></param>
|
||||
/// <returns></returns>
|
||||
private static Color GetColor(string hexOrName)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Color.FromArgb(Convert.ToInt32(hexOrName, 16));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Color.FromName(hexOrName);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
public static DrawningAirplane CreateDrawningAirplane(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawningAirplane(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), GetColor(strs[2]));
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningAirBomber(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), GetColor(strs[2]),
|
||||
GetColor(strs[3]), Convert.ToBoolean(strs[4]),
|
||||
Convert.ToBoolean(strs[5]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningAirplane"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDataForSave(this DrawningAirplane drawningAirplane)
|
||||
{
|
||||
var Airplane = drawningAirplane.Airplane;
|
||||
var str = $"{Airplane.Speed}{_separatorForObject}{Airplane.Weight}{_separatorForObject}{Airplane.BodyColor.Name}";
|
||||
if (Airplane is not EntityAirBomber AirBomber)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return $"{str}{_separatorForObject}{AirBomber.DopColor.Name}{_separatorForObject}{AirBomber.HasBombs}{_separatorForObject}{AirBomber.HasFuelTanks}";
|
||||
}
|
||||
}
|
||||
}
|
@ -57,6 +57,12 @@
|
||||
this.buttonLeft = new System.Windows.Forms.Button();
|
||||
this.buttonUp = new System.Windows.Forms.Button();
|
||||
this.pictureBox = new System.Windows.Forms.PictureBox();
|
||||
this.menuStrip = new System.Windows.Forms.MenuStrip();
|
||||
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBoxMaps.SuspendLayout();
|
||||
this.groupBoxGenerate.SuspendLayout();
|
||||
@ -377,6 +383,46 @@
|
||||
this.pictureBox.TabIndex = 1;
|
||||
this.pictureBox.TabStop = false;
|
||||
//
|
||||
// menuStrip
|
||||
//
|
||||
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.файлToolStripMenuItem});
|
||||
this.menuStrip.Location = new System.Drawing.Point(0, 0);
|
||||
this.menuStrip.Name = "menuStrip";
|
||||
this.menuStrip.Size = new System.Drawing.Size(1015, 24);
|
||||
this.menuStrip.TabIndex = 2;
|
||||
//
|
||||
// файлToolStripMenuItem
|
||||
//
|
||||
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
|
||||
this.SaveToolStripMenuItem,
|
||||
this.LoadToolStripMenuItem});
|
||||
this.файлToolStripMenuItem.Name = "файлToolStripMenuItem";
|
||||
this.файлToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
|
||||
this.файлToolStripMenuItem.Text = "Файл";
|
||||
//
|
||||
// SaveToolStripMenuItem
|
||||
//
|
||||
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
|
||||
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.SaveToolStripMenuItem.Text = "Сохранение";
|
||||
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
|
||||
//
|
||||
// LoadToolStripMenuItem
|
||||
//
|
||||
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
|
||||
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
|
||||
this.LoadToolStripMenuItem.Text = "Загрузка";
|
||||
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
|
||||
//
|
||||
// openFileDialog
|
||||
//
|
||||
this.openFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// saveFileDialog
|
||||
//
|
||||
this.saveFileDialog.Filter = "txt file | *.txt";
|
||||
//
|
||||
// FormMapWithSetAirplanes
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
@ -384,6 +430,8 @@
|
||||
this.ClientSize = new System.Drawing.Size(1015, 810);
|
||||
this.Controls.Add(this.pictureBox);
|
||||
this.Controls.Add(this.groupBoxTools);
|
||||
this.Controls.Add(this.menuStrip);
|
||||
this.MainMenuStrip = this.menuStrip;
|
||||
this.Name = "FormMapWithSetAirplanes";
|
||||
this.Text = "Генератор самолетов";
|
||||
this.groupBoxTools.ResumeLayout(false);
|
||||
@ -396,6 +444,8 @@
|
||||
((System.ComponentModel.ISupportInitialize)(this.numericUpDownEngines)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.numericWeight)).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
|
||||
this.menuStrip.ResumeLayout(false);
|
||||
this.menuStrip.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
@ -431,5 +481,11 @@
|
||||
private ListBox listBoxMaps;
|
||||
private TextBox textBoxNewMapName;
|
||||
private Button btnAddDeletedObject;
|
||||
private MenuStrip menuStrip;
|
||||
private ToolStripMenuItem файлToolStripMenuItem;
|
||||
private ToolStripMenuItem SaveToolStripMenuItem;
|
||||
private ToolStripMenuItem LoadToolStripMenuItem;
|
||||
private OpenFileDialog openFileDialog;
|
||||
private SaveFileDialog saveFileDialog;
|
||||
}
|
||||
}
|
@ -20,7 +20,7 @@ namespace AirBomber
|
||||
/// <summary>
|
||||
/// Все объекты удаленные с каких либо карт
|
||||
/// </summary>
|
||||
private LinkedList<DrawningObject> _deletedObjects;
|
||||
private LinkedList<IDrawningObject> _deletedObjects;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
@ -304,5 +304,45 @@ namespace AirBomber
|
||||
AddAirplaneInMap(form.SelectedAirplane);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Сохранение"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_mapsCollection.SaveData(saveFileDialog.FileName))
|
||||
{
|
||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия "Загрузка"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_mapsCollection.LoadData(openFileDialog.FileName))
|
||||
{
|
||||
MessageBox.Show("Открытие прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
ReloadMaps();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось открыть", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace AirBomber
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с объектом, прорисовываемым на форме
|
||||
/// </summary>
|
||||
internal interface IDrawningObject
|
||||
public interface IDrawningObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Шаг перемещения объекта
|
||||
@ -39,5 +39,11 @@ namespace AirBomber
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
RectangleF GetCurrentPosition();
|
||||
|
||||
/// <summary>
|
||||
/// Получение информации по объекту
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string GetInfo();
|
||||
}
|
||||
}
|
||||
|
@ -179,5 +179,31 @@ namespace AirBomber
|
||||
airplane?.DrawObject(g);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение данных в виде строки
|
||||
/// </summary>
|
||||
/// <param name="sep"></param>
|
||||
/// <returns></returns>
|
||||
public string GetData(char separatorType, char separatorData)
|
||||
{
|
||||
string data = $"{_map.GetType().Name}{separatorType}";
|
||||
foreach (var car in SetAirplanes.GetAirplanes())
|
||||
{
|
||||
data += $"{car.GetInfo()}{separatorData}";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка списка из массива строк
|
||||
/// </summary>
|
||||
/// <param name="records"></param>
|
||||
public void LoadData(string[] records)
|
||||
{
|
||||
foreach (var rec in records)
|
||||
{
|
||||
SetAirplanes.Insert(DrawningObject.Create(rec) as T);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace AirBomber
|
||||
/// <summary>
|
||||
/// Словарь (хранилище) с картами
|
||||
/// </summary>
|
||||
readonly Dictionary<string, MapWithSetAirplanesGeneric<DrawningObject,
|
||||
readonly Dictionary<string, MapWithSetAirplanesGeneric<IDrawningObject,
|
||||
AbstractMap>> _mapStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий карт
|
||||
@ -30,6 +30,14 @@ namespace AirBomber
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по элементу словаря в файл
|
||||
/// </summary>
|
||||
private readonly char separatorDict = '|';
|
||||
/// <summary>
|
||||
/// Разделитель для записей коллекции данных в файл
|
||||
/// </summary>
|
||||
private readonly char separatorData = ';';
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
@ -37,7 +45,7 @@ namespace AirBomber
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new Dictionary<string,
|
||||
MapWithSetAirplanesGeneric<DrawningObject, AbstractMap>>();
|
||||
MapWithSetAirplanesGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
@ -63,7 +71,7 @@ namespace AirBomber
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public MapWithSetAirplanesGeneric<DrawningObject, AbstractMap> this[string ind]
|
||||
public MapWithSetAirplanesGeneric<IDrawningObject, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -72,7 +80,7 @@ namespace AirBomber
|
||||
}
|
||||
}
|
||||
|
||||
public DrawningObject this[string ind, int indDrawningObject]
|
||||
public IDrawningObject this[string ind, int indDrawningObject]
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -80,5 +88,73 @@ namespace AirBomber
|
||||
return mapWithSetAirplanesGeneric?.SetAirplanes[indDrawningObject];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение информации по самолетам в хранилище в файл
|
||||
/// </summary>
|
||||
/// <param name="filename">Путь и имя файла</param>
|
||||
/// <returns></returns>
|
||||
public bool SaveData(string filename)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
File.Delete(filename);
|
||||
}
|
||||
using (StreamWriter fs = new(filename))
|
||||
{
|
||||
fs.Write($"MapsCollection{Environment.NewLine}");
|
||||
foreach (var storage in _mapStorages)
|
||||
{
|
||||
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Загрузка информации по самолетам в ангарах из файла
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <returns></returns>
|
||||
public bool LoadData(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
List<string> strs = new List<string>();
|
||||
using (StreamReader fs = new(filename))
|
||||
{
|
||||
while (!fs.EndOfStream)
|
||||
{
|
||||
strs.Add(fs.ReadLine());
|
||||
}
|
||||
}
|
||||
if (!strs[0].Contains("MapsCollection"))
|
||||
{
|
||||
//если нет такой записи, то это не те данные
|
||||
return false;
|
||||
}
|
||||
//очищаем записи
|
||||
_mapStorages.Clear();
|
||||
for (int i = 1; i < strs.Count; ++i)
|
||||
{
|
||||
var elem = strs[i].Split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1])
|
||||
{
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "WallMap":
|
||||
map = new WallMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.Add(elem[0], new
|
||||
MapWithSetAirplanesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData,
|
||||
StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user