This commit is contained in:
Alenka 2023-10-13 10:29:00 +04:00
parent db85af0a99
commit 065b937d2c
9 changed files with 509 additions and 11 deletions

View File

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Generics;
using Cruiser.MovementStrategy;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace DumpTruck.Generics
{
internal class CarsGenericCollection<T, U>
where T : DrawningCruiser
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 210;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 90;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static T? operator +(CarsGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return null;
}
return collect?._collection.Insert(obj) ?? null;
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static T? operator -(CarsGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCars()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
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);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
DrawningCruiser car;
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
{
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
car = _collection.Get(i);
if (car != null)
{
car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
//car.SetPosition(_placeSizeWidth * (i/ numPlacesInColumn) + _placeSizeWidth / 20, (i % numPlacesInColumn ) *_placeSizeHeight + _placeSizeHeight / 10);
car.DrawTransport(g);
}
}
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace Cruiser.DrawningObjects
{
@ -39,14 +40,14 @@ namespace Cruiser.DrawningObjects
/// </summary>
public int GetHeight => _cruiserHeight;
public IMoveableObject GetMoveableObject => new DrawningObjectCar(this);
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _cruiserWidth || height < _cruiserHeight)

View File

@ -10,29 +10,29 @@ namespace Cruiser.MovementStrategy
{
internal class DrawningObjectCar : IMoveableObject
{
private readonly DrawningCruiser? _drawningCar = null;
public DrawningObjectCar(DrawningCruiser drawningCar)
private readonly DrawningCruiser? _drawningCruiser = null;
public DrawningObjectCar(DrawningCruiser drawningCruiser)
{
_drawningCar = drawningCar;
_drawningCruiser = drawningCruiser;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningCar == null || _drawningCar.EntityCruiser ==
if (_drawningCruiser == null || _drawningCruiser.EntityCruiser ==
null)
{
return null;
}
return new ObjectParameters(_drawningCar.GetPosX,
_drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
return new ObjectParameters(_drawningCruiser.GetPosX,
_drawningCruiser.GetPosY, _drawningCruiser.GetWidth, _drawningCruiser.GetHeight);
}
}
public int GetStep => (int)(_drawningCar?.EntityCruiser?.Step ?? 0);
public int GetStep => (int)(_drawningCruiser?.EntityCruiser?.Step ?? 0);
public bool CheckCanMove(Direction direction) =>
_drawningCar?.CanMove(direction) ?? false;
_drawningCruiser?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) =>
_drawningCar?.MoveTransport(direction);
_drawningCruiser?.MoveTransport(direction);
}
}

View File

@ -31,6 +31,7 @@
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.ButtonStep = new System.Windows.Forms.Button();
this.SelectedCruiser = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
@ -146,9 +147,20 @@
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// SelectedCruiser
//
this.SelectedCruiser.Location = new System.Drawing.Point(210, 261);
this.SelectedCruiser.Name = "SelectedCruiser";
this.SelectedCruiser.Size = new System.Drawing.Size(112, 34);
this.SelectedCruiser.TabIndex = 9;
this.SelectedCruiser.Text = "Выбрать";
this.SelectedCruiser.UseVisualStyleBackColor = true;
this.SelectedCruiser.Click += new System.EventHandler(this.SelectedCruiser_Click);
//
// CruiserForm
//
this.ClientSize = new System.Drawing.Size(667, 358);
this.Controls.Add(this.SelectedCruiser);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button2);
@ -178,5 +190,6 @@ private Button button1;
private Button button2;
private ComboBox comboBox1;
private Button ButtonStep;
private Button SelectedCruiser;
}
}

View File

@ -11,9 +11,13 @@ namespace Cruiser
private DrawningCruiser? _drawningCruiser;
private AbstractStrategy? _abstractStrategy;
private AbstractStrategy? _strategy;
public DrawningCruiser? SelectedCar { get; private set; }
public CruiserForm()
{
InitializeComponent();
_strategy = null;
SelectedCar = null;
}
private void Draw()
{
@ -122,6 +126,12 @@ namespace Cruiser
100));
Draw();
}
public void SelectedCruiser_Click(object sender, EventArgs e)
{
//SelectedCruiser = _drawningCruiser;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -0,0 +1,112 @@
namespace Cruiser
{
partial class FormCruiserCollection
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.ButtonAddCruiser = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(800, 450);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.ButtonAddCruiser);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(589, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(211, 438);
this.panel1.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(31, 168);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(150, 34);
this.button2.TabIndex = 2;
this.button2.Text = "Удалить";
this.button2.UseVisualStyleBackColor = true;
//
// ButtonAddCruiser
//
this.ButtonAddCruiser.Location = new System.Drawing.Point(31, 37);
this.ButtonAddCruiser.Name = "ButtonAddCruiser";
this.ButtonAddCruiser.Size = new System.Drawing.Size(150, 34);
this.ButtonAddCruiser.TabIndex = 3;
this.ButtonAddCruiser.Text = "Добавить";
this.ButtonAddCruiser.UseVisualStyleBackColor = true;
this.ButtonAddCruiser.Click += new System.EventHandler(this.ButtonAddCruiser_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(31, 104);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(150, 31);
this.textBox1.TabIndex = 2;
//
// FormCruiserCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.panel1);
this.Controls.Add(this.pictureBox1);
this.Name = "FormCruiserCollection";
this.Text = "FormCruiserCollection";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private PictureBox pictureBox1;
private Panel panel1;
private Button button2;
private Button ButtonAddCruiser;
private TextBox textBox1;
}
}

View File

@ -0,0 +1,53 @@
using Cruiser.MovementStrategy;
using DumpTruck.Generics;
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;
using Cruiser;
using Cruiser.DrawningObjects;
using Cruiser.Generics;
namespace Cruiser
{
public partial class FormCruiserCollection : Form
{
private readonly CarsGenericCollection<DrawningCruiser, DrawningObjectCar> _cars;
public FormCruiserCollection()
{
InitializeComponent();
_cars = new CarsGenericCollection<DrawningCruiser, DrawningObjectCar>(pictureBox1.Width, pictureBox1.Height);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void ButtonAddCruiser_Click(object sender, EventArgs e)
{
CruiserForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_cars + form.SelectedCar)
{
MessageBox.Show("Объект добавлен");
pictureBox1.Image = _cars.ShowCars();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.Generics
{
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
///
public int startPointer = 0;
public SetGeneric(int count)
{
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns>
public T? Insert(T car)
{
if (_places[Count - 1] != null)
return null;
return Insert(car, 0);
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public T? Insert(T car, int position)
{
if (!(position >= 0 && position < Count))
return null;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = car;
return car;
}
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
// проверка, что после вставляемого элемента в массиве есть пустой элемент
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
// TODO вставка по позиции
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < Count && position >= 0)
{
_places[position] = null;
return true;
}
return false;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < Count && position >= 0) { return _places[position]; }
return null;
}
}
}