diff --git a/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs b/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs index 8896eda..3d62f00 100644 --- a/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/DrawingWarmlyShip.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using WarmlyShip.Entities; +using WarmlyShip.MovementStrategy; namespace WarmlyShip.DrawingObjects { @@ -123,6 +124,10 @@ namespace WarmlyShip.DrawingObjects break; } } + /// + /// Получение объекта IMoveableObject из объекта DrawingCar + /// + public IMoveableObject GetMoveableObject => new DrawingObjectShip(this); } } diff --git a/WarmlyShip/WarmlyShip/FormWarmlyShip.cs b/WarmlyShip/WarmlyShip/FormWarmlyShip.cs index 60ffe75..fe8b703 100644 --- a/WarmlyShip/WarmlyShip/FormWarmlyShip.cs +++ b/WarmlyShip/WarmlyShip/FormWarmlyShip.cs @@ -7,10 +7,13 @@ namespace WarmlyShip { private DrawingWarmlyShip? _drawingWarmlyShip; - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + public DrawingWarmlyShip? SelectedShip { get; private set; } public FormWarmlyShip() { InitializeComponent(); + _strategy = null; + SelectedShip = null; } private void Draw() { @@ -27,10 +30,16 @@ namespace WarmlyShip private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); + 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; + } + _drawingWarmlyShip = new DrawingWarmlyShip(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); _drawingWarmlyShip.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -38,12 +47,24 @@ namespace WarmlyShip private void buttonCreateWarmlyShip_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogColor = new(); + if (dialogColor.ShowDialog() == DialogResult.OK) + { + color = dialogColor.Color; + } + + Color addColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogDopColor = new(); + if (dialogDopColor.ShowDialog() == DialogResult.OK) + { + addColor = dialogDopColor.Color; + } _drawingWarmlyShip = new DrawingWarmlyShipWithPipes(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), + color, addColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); @@ -83,32 +104,31 @@ namespace WarmlyShip } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new - DrawingObjectShip(_drawingWarmlyShip), pictureBoxWarmlyShip.Width, + _strategy.SetData(_drawingWarmlyShip.GetMoveableObject, pictureBoxWarmlyShip.Width, pictureBoxWarmlyShip.Height); comboBoxStrategy.Enabled = false; } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } } diff --git a/WarmlyShip/WarmlyShip/SetGeneric.cs b/WarmlyShip/WarmlyShip/SetGeneric.cs new file mode 100644 index 0000000..267381f --- /dev/null +++ b/WarmlyShip/WarmlyShip/SetGeneric.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyShip.Generics +{ + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T warmlyship) + { + // TODO вставка в начало набора + return Insert(warmlyship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T warmlyship, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + int nullIndex = -1; + int i; + + if (position < 0 || position >= Count) + return -1; + + for (i = position; i < Count; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + if (nullIndex < 0) + return -1; + + for (i = nullIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = warmlyship; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива значение null + if (position < 0 || position >= Count) + { + return false; + } + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + } +} + diff --git a/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs b/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs new file mode 100644 index 0000000..7a8139a --- /dev/null +++ b/WarmlyShip/WarmlyShip/ShipsGenericCollection.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using WarmlyShip.DrawingObjects; +using WarmlyShip.MovementStrategy; + +namespace WarmlyShip.Generics +{ + internal class ShipsGenericCollection + where T : DrawingWarmlyShip + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 185; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 180; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public ShipsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(ShipsGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(ShipsGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowCars() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + 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); + } + } + /// + /// /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + T? obj; + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int diff = 1; + int currWidth = 0; + for (int i = 0; i < _collection.Count; i++) + { + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + currWidth++; + if (currWidth > width) + { + diff++; + currWidth = 1; + } + obj = _collection.Get(i); + if (obj != null) + { + obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, + (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); + obj.DrawTransport(g); + } + } + } + } +} +