PIbd-21_MalafeevL.S._Cruise.../Cruiser/Drawing/ExtentionDrawingCruiser.cs

65 lines
2.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Drawing;
using Cruiser.Entities;
namespace Cruiser.Drawing
{
/// <summary>
/// Расширение для класса EntityCruiser
/// </summary>
public static class ExtentionDrawningCruiser
{
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info">Строка с данными для создания объекта</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
/// <returns>Объект</returns>
public static DrawingCruiser? CreateDrawingCruiser(this string info, char separatorForObject, int width, int height)
{
string[] strs = info.Split(separatorForObject);
if (strs.Length == 3)
{
return new DrawingCruiser(Convert.ToInt32(strs[0]), Convert.ToInt32(strs[1]), Color.FromName(strs[2]), width, height);
}
if (strs.Length == 6)
{
return new DrawingProCruiser(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]),
Color.FromName(strs[2]),
Color.FromName(strs[3]),
Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), width, height);
}
return null;
}
/// <summary>
/// Получение данных для сохранения в файл
/// </summary>
/// <param name="drawingCruiser">Сохраняемый объект</param>
/// <param name="separatorForObject">Разделитель даннных</param>
/// <returns>Строка с данными по объекту</returns>
public static string GetDataForSave(this DrawingCruiser drawingCruiser, char separatorForObject)
{
var cruiser = drawingCruiser.EntityCruiser;
if (cruiser == null)
{
return string.Empty;
}
var str = $"{cruiser.Speed}{separatorForObject}{cruiser.Weight}{separatorForObject}{cruiser.BodyColor.Name}";
if (cruiser is not EntityProCruiser proCruiser)
{
return str;
}
return $"{str}{separatorForObject}{proCruiser.ElementsColor.Name}{separatorForObject}{proCruiser.RocketMines}{separatorForObject}{proCruiser.Helipad}";
}
}
}