Создание класса, подготавливающего данные об объекте
This commit is contained in:
parent
c6dd512670
commit
ee3326095d
57
ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs
Normal file
57
ArmoredCar/ArmoredCar/ExtensionArmoredCar.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ArmoredCar
|
||||
{
|
||||
internal static class ExtensionArmoredCar
|
||||
{
|
||||
/// <summary>
|
||||
/// Разделитель для записи информации по объекту в файл
|
||||
/// </summary>
|
||||
private static readonly char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Создание объекта из строки
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
/// <returns></returns>
|
||||
public static DrawningArmoredCar CreateDrawningArmoredCar(this string info)
|
||||
{
|
||||
string[] strs = info.Split(_separatorForObject);
|
||||
if (strs.Length == 3)
|
||||
{
|
||||
return new DrawningArmoredCar(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
|
||||
}
|
||||
if (strs.Length == 6)
|
||||
{
|
||||
return new DrawningTank(Convert.ToInt32(strs[0]),
|
||||
Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
|
||||
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
|
||||
Convert.ToBoolean(strs[5]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение данных для сохранения в файл
|
||||
/// </summary>
|
||||
/// <param name="drawningArmoredCar"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDataForSave(this DrawningArmoredCar drawningArmoredCar)
|
||||
{
|
||||
var armoredCar = drawningArmoredCar.ArmoredCar;
|
||||
var str =
|
||||
$"{armoredCar.Speed}{_separatorForObject}{armoredCar.Weight}{_separatorForObject}{armoredCar.BodyColor.Name}";
|
||||
if (armoredCar is not EntityTank tank)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return
|
||||
$"{str}{_separatorForObject}{tank.DopColor.Name}{_separatorForObject}{tank.TowerWeapon}{_separatorForObject}{tank.AMachineGun}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user