From 5ae5e17d51c9d98dedf3c8b25d1327b70b1691b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=B6=D0=B8=D0=B2=D0=B8=D0=BD?= Date: Mon, 10 Oct 2022 00:02:53 +0400 Subject: [PATCH] test --- Stormtrooper/Stormtrooper/AbstractMap.cs | 4 + Stormtrooper/Stormtrooper/Direction.cs | 2 +- .../Stormtrooper/DrowningStormtrooper.cs | 6 +- .../Stormtrooper/EntityStormtrooper.cs | 2 +- .../FormMapWithSetStormtroopers.Designer.cs | 39 ++++ .../FormMapWithSetStormtroopers.cs | 20 ++ .../FormMapWithSetStormtroopers.resx | 120 ++++++++++++ .../Stormtrooper/FormStormtrooper.Designer.cs | 13 ++ Stormtrooper/Stormtrooper/FormStormtrooper.cs | 8 + .../MapWithSetStormtroopersGeneric.cs | 174 ++++++++++++++++++ .../Stormtrooper/SetStormtroopersGeneric.cs | 77 ++++++++ 11 files changed, 460 insertions(+), 5 deletions(-) create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs create mode 100644 Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx create mode 100644 Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs create mode 100644 Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs diff --git a/Stormtrooper/Stormtrooper/AbstractMap.cs b/Stormtrooper/Stormtrooper/AbstractMap.cs index 29c10ef..25844ba 100644 --- a/Stormtrooper/Stormtrooper/AbstractMap.cs +++ b/Stormtrooper/Stormtrooper/AbstractMap.cs @@ -24,6 +24,10 @@ namespace Stormtrooper int left = Convert.ToInt32((_drawningObject.GetCurrentPosition().Left + x) / _size_x) > 0 ? Convert.ToInt32((_drawningObject.GetCurrentPosition().Left + x) / _size_x) : 0; int up = Convert.ToInt32((_drawningObject.GetCurrentPosition().Top + y) / _size_y) > 0 ? Convert.ToInt32((_drawningObject.GetCurrentPosition().Top + y) / _size_y) : 0; int down = Convert.ToInt32((_drawningObject.GetCurrentPosition().Bottom + y) / _size_y) > 0 ? Convert.ToInt32((_drawningObject.GetCurrentPosition().Bottom + y) / _size_y) : 0; + if (_drawningObject.GetCurrentPosition().Bottom + y > _height || _drawningObject.GetCurrentPosition().Right + x > _width || + _drawningObject.GetCurrentPosition().Top + y < 0 || _drawningObject.GetCurrentPosition().Left + x < 0) { + return false; + } for (int i = left; i <= right; i++) { for (int j = up; j <= down; j++) diff --git a/Stormtrooper/Stormtrooper/Direction.cs b/Stormtrooper/Stormtrooper/Direction.cs index ad7316d..32f5ffe 100644 --- a/Stormtrooper/Stormtrooper/Direction.cs +++ b/Stormtrooper/Stormtrooper/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/Stormtrooper/Stormtrooper/DrowningStormtrooper.cs b/Stormtrooper/Stormtrooper/DrowningStormtrooper.cs index db9f014..dc79d38 100644 --- a/Stormtrooper/Stormtrooper/DrowningStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/DrowningStormtrooper.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal class DrowningStormtrooper + public class DrowningStormtrooper { /// /// Класс-сущность @@ -31,7 +31,7 @@ namespace Stormtrooper /// /// Ширина отрисовки автомобиля /// - private readonly int _stormtrooperWidth = 90; + private readonly int _stormtrooperWidth = 80; /// /// Высота отрисовки автомобиля /// @@ -271,7 +271,7 @@ namespace Stormtrooper } public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() { - return (_startPosX, _startPosY, _startPosX + _stormtrooperWidth, _startPosY + _stormtrooperHeight); + return (_startPosX, _startPosX + _stormtrooperWidth, _startPosY, _startPosY + _stormtrooperHeight); } } } diff --git a/Stormtrooper/Stormtrooper/EntityStormtrooper.cs b/Stormtrooper/Stormtrooper/EntityStormtrooper.cs index 55e8408..3400c1e 100644 --- a/Stormtrooper/Stormtrooper/EntityStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/EntityStormtrooper.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Stormtrooper { - internal class EntityStormtrooper + public class EntityStormtrooper { public int Speed { get; private set; } /// diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs new file mode 100644 index 0000000..6d7b8c3 --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.Designer.cs @@ -0,0 +1,39 @@ +namespace Stormtrooper +{ + partial class FormMapWithSetStormtroopers + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormMapWithSetStormtroopers"; + } + + #endregion + } +} \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs new file mode 100644 index 0000000..0f1d925 --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.cs @@ -0,0 +1,20 @@ +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; + +namespace Stormtrooper +{ + public partial class FormMapWithSetStormtroopers : Form + { + public FormMapWithSetStormtroopers() + { + InitializeComponent(); + } + } +} diff --git a/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Stormtrooper/Stormtrooper/FormMapWithSetStormtroopers.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs index f52744b..2ce3d20 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.Designer.cs @@ -40,6 +40,7 @@ this.buttonLeft = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelectStormtrooper = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxStormtrooper)).BeginInit(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -155,11 +156,22 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); // + // buttonSelectStormtrooper + // + this.buttonSelectStormtrooper.Location = new System.Drawing.Point(489, 364); + this.buttonSelectStormtrooper.Name = "buttonSelectStormtrooper"; + this.buttonSelectStormtrooper.Size = new System.Drawing.Size(129, 29); + this.buttonSelectStormtrooper.TabIndex = 8; + this.buttonSelectStormtrooper.Text = "Выбрать"; + this.buttonSelectStormtrooper.UseVisualStyleBackColor = true; + this.buttonSelectStormtrooper.Click += new System.EventHandler(this.ButtonSelectStormtrooper_Click); + // // FormStormtrooper // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelectStormtrooper); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); @@ -192,5 +204,6 @@ private Button buttonLeft; private Button buttonRight; private Button buttonCreateModif; + private Button buttonSelectStormtrooper; } } \ No newline at end of file diff --git a/Stormtrooper/Stormtrooper/FormStormtrooper.cs b/Stormtrooper/Stormtrooper/FormStormtrooper.cs index 9ffb425..87a741d 100644 --- a/Stormtrooper/Stormtrooper/FormStormtrooper.cs +++ b/Stormtrooper/Stormtrooper/FormStormtrooper.cs @@ -15,6 +15,8 @@ namespace Stormtrooper private DrowningStormtrooper _stormtrooper; + public DrowningStormtrooper SelectedStormtrooper { get; private set; } + public FormStormtrooper() { InitializeComponent(); @@ -91,5 +93,11 @@ namespace Stormtrooper SetData(stormtrooper); } + + private void ButtonSelectStormtrooper_Click(object sender, EventArgs e) + { + SelectedStormtrooper = _stormtrooper; + DialogResult = DialogResult.OK; + } } } diff --git a/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs new file mode 100644 index 0000000..4142127 --- /dev/null +++ b/Stormtrooper/Stormtrooper/MapWithSetStormtroopersGeneric.cs @@ -0,0 +1,174 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class MapWithSetStormtroopersGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 120; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 160; + /// + /// Набор объектов + /// + private readonly SetStormtroopersGeneric _setCars; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetStormtroopersGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setCars = new SetStormtroopersGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator + (MapWithSetStormtroopersGeneric map, T car) + { + return map._setCars.Insert(car); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator - (MapWithSetStormtroopersGeneric map, int + position) + { + return map._setCars.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawCars(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setCars.Count; i++) + { + var car = _setCars.Get(i); + if (car != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, car); + } + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setCars.Count - 1; + for (int i = 0; i < _setCars.Count; i++) + { + if (_setCars.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setCars.Get(j); + if (car != null) + { + _setCars.Insert(car, i); + _setCars.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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 DrawCars(Graphics g) + { + for (int i = 0; i < _setCars.Count; i++) + { + // TODO установка позиции + _setCars.Get(i)?.DrawningObject(g); + } + } + + } +} diff --git a/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs new file mode 100644 index 0000000..c75c56d --- /dev/null +++ b/Stormtrooper/Stormtrooper/SetStormtroopersGeneric.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Stormtrooper +{ + internal class SetStormtroopersGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetCarsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T car) + { + // TODO вставка в начало набора + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T car, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = car; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присовив элементу массива значение null + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + + } +}