Всё готово

This commit is contained in:
ShabOl 2023-11-19 17:11:10 +04:00
parent a768d109b5
commit 0922c3809f
9 changed files with 477 additions and 7 deletions

View File

@ -37,6 +37,7 @@
MovementStrategyComboBox = new ComboBox(); MovementStrategyComboBox = new ComboBox();
ButtonCreateBomber = new Button(); ButtonCreateBomber = new Button();
ButtonPerformMove = new Button(); ButtonPerformMove = new Button();
SelectBomberButton = new Button();
((System.ComponentModel.ISupportInitialize)BomberPictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)BomberPictureBox).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -142,11 +143,23 @@
ButtonPerformMove.UseVisualStyleBackColor = true; ButtonPerformMove.UseVisualStyleBackColor = true;
ButtonPerformMove.Click += ButtonPerformStep_Click; ButtonPerformMove.Click += ButtonPerformStep_Click;
// //
// SelectBomberButton
//
SelectBomberButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
SelectBomberButton.Location = new Point(354, 407);
SelectBomberButton.Name = "SelectBomberButton";
SelectBomberButton.Size = new Size(159, 42);
SelectBomberButton.TabIndex = 9;
SelectBomberButton.Text = "Выбрать бомбардировщик";
SelectBomberButton.UseVisualStyleBackColor = true;
SelectBomberButton.Click += ButtonSelectEntity_Click;
//
// BomberForm // BomberForm
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461); ClientSize = new Size(884, 461);
Controls.Add(SelectBomberButton);
Controls.Add(ButtonPerformMove); Controls.Add(ButtonPerformMove);
Controls.Add(ButtonCreateBomber); Controls.Add(ButtonCreateBomber);
Controls.Add(MovementStrategyComboBox); Controls.Add(MovementStrategyComboBox);
@ -175,5 +188,6 @@
private ComboBox MovementStrategyComboBox; private ComboBox MovementStrategyComboBox;
private Button ButtonCreateBomber; private Button ButtonCreateBomber;
private Button ButtonPerformMove; private Button ButtonPerformMove;
private Button SelectBomberButton;
} }
} }

View File

@ -8,9 +8,14 @@ namespace AirBomber
private BomberRendererBase? _bomberRenderer; private BomberRendererBase? _bomberRenderer;
private AbstractStrategy? _strategy; private AbstractStrategy? _strategy;
public BomberRendererBase? SelectedRenderer { get; private set; }
public BomberForm() public BomberForm()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
SelectedRenderer = null;
} }
private void Draw() private void Draw()
@ -29,11 +34,22 @@ namespace AirBomber
{ {
Random random = new Random(); Random random = new Random();
Color BodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
Color AdditionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog BodyColorPicker = new ColorDialog();
if (BodyColorPicker.ShowDialog() == DialogResult.OK)
BodyColor = BodyColorPicker.Color;
ColorDialog AdditionalColorPicker = new ColorDialog();
if (AdditionalColorPicker.ShowDialog() == DialogResult.OK)
AdditionalColor = AdditionalColorPicker.Color;
_bomberRenderer = new BomberRenderer( _bomberRenderer = new BomberRenderer(
random.Next(100, 300), random.Next(100, 300),
random.Next(1000, 3000), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), BodyColor,
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), AdditionalColor,
true, true,
true, true,
BomberPictureBox.Width, BomberPictureBox.Width,
@ -49,10 +65,17 @@ namespace AirBomber
{ {
Random random = new Random(); Random random = new Random();
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;
}
_bomberRenderer = new BomberRendererBase( _bomberRenderer = new BomberRendererBase(
random.Next(100, 200), random.Next(100, 200),
random.Next(1000, 1800), random.Next(1000, 1800),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), color,
BomberPictureBox.Width, BomberPictureBox.Width,
BomberPictureBox.Height BomberPictureBox.Height
); );
@ -110,17 +133,17 @@ namespace AirBomber
return; return;
_strategy.SetData( _strategy.SetData(
new ObjectEntityRenderer(_bomberRenderer), _bomberRenderer.MovableObject,
BomberPictureBox.Width, BomberPictureBox.Width,
BomberPictureBox.Height BomberPictureBox.Height
); );
MovementStrategyComboBox.Enabled = false;
} }
if (_strategy is null) if (_strategy is null)
return; return;
MovementStrategyComboBox.Enabled = false;
_strategy.MakeStep(); _strategy.MakeStep();
Draw(); Draw();
@ -130,5 +153,11 @@ namespace AirBomber
_strategy = null; _strategy = null;
} }
} }
private void ButtonSelectEntity_Click(object sender, EventArgs e)
{
SelectedRenderer = _bomberRenderer;
DialogResult = DialogResult.OK;
}
} }
} }

127
AirBomber/FormEntityCollection.Designer.cs generated Normal file
View File

@ -0,0 +1,127 @@
namespace AirBomber
{
partial class FormEntityCollection
{
/// <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()
{
CollectionPictureBox = new PictureBox();
ToolGroupBox = new GroupBox();
RefreshCollectionButton = new Button();
RemoveBomberButton = new Button();
NumberMaskedTextBox = new MaskedTextBox();
AddBomberButton = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolGroupBox.SuspendLayout();
SuspendLayout();
//
// CollectionPictureBox
//
CollectionPictureBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
CollectionPictureBox.Location = new Point(0, 0);
CollectionPictureBox.Name = "CollectionPictureBox";
CollectionPictureBox.Size = new Size(812, 603);
CollectionPictureBox.TabIndex = 0;
CollectionPictureBox.TabStop = false;
//
// ToolGroupBox
//
ToolGroupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
ToolGroupBox.Controls.Add(RefreshCollectionButton);
ToolGroupBox.Controls.Add(RemoveBomberButton);
ToolGroupBox.Controls.Add(NumberMaskedTextBox);
ToolGroupBox.Controls.Add(AddBomberButton);
ToolGroupBox.Location = new Point(818, 12);
ToolGroupBox.Name = "ToolGroupBox";
ToolGroupBox.Size = new Size(176, 579);
ToolGroupBox.TabIndex = 1;
ToolGroupBox.TabStop = false;
ToolGroupBox.Text = "Инструменты";
//
// RefreshCollectionButton
//
RefreshCollectionButton.Location = new Point(6, 190);
RefreshCollectionButton.Name = "RefreshCollectionButton";
RefreshCollectionButton.Size = new Size(164, 30);
RefreshCollectionButton.TabIndex = 3;
RefreshCollectionButton.Text = "Обновить коллекцию";
RefreshCollectionButton.UseVisualStyleBackColor = true;
RefreshCollectionButton.Click += ButtonRefreshCollection_Click;
//
// RemoveBomberButton
//
RemoveBomberButton.Location = new Point(6, 141);
RemoveBomberButton.Name = "RemoveBomberButton";
RemoveBomberButton.Size = new Size(164, 30);
RemoveBomberButton.TabIndex = 2;
RemoveBomberButton.Text = "Удалить бомбардировщик";
RemoveBomberButton.UseVisualStyleBackColor = true;
RemoveBomberButton.Click += ButtonRemoveEntity_Click;
//
// NumberMaskedTextBox
//
NumberMaskedTextBox.Location = new Point(6, 102);
NumberMaskedTextBox.Mask = "00000";
NumberMaskedTextBox.Name = "NumberMaskedTextBox";
NumberMaskedTextBox.Size = new Size(164, 23);
NumberMaskedTextBox.TabIndex = 1;
NumberMaskedTextBox.ValidatingType = typeof(int);
//
// AddBomberButton
//
AddBomberButton.Location = new Point(6, 32);
AddBomberButton.Name = "AddBomberButton";
AddBomberButton.Size = new Size(164, 43);
AddBomberButton.TabIndex = 0;
AddBomberButton.Text = "Добавить бомбардировщик";
AddBomberButton.UseVisualStyleBackColor = true;
AddBomberButton.Click += ButtonAddEntity_Click;
//
// FormEntityCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1006, 603);
Controls.Add(ToolGroupBox);
Controls.Add(CollectionPictureBox);
Name = "FormEntityCollection";
Text = "Набор бомбардировщиков";
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolGroupBox.ResumeLayout(false);
ToolGroupBox.PerformLayout();
ResumeLayout(false);
}
#endregion
private PictureBox CollectionPictureBox;
private GroupBox ToolGroupBox;
private Button RefreshCollectionButton;
private Button RemoveBomberButton;
private MaskedTextBox NumberMaskedTextBox;
private Button AddBomberButton;
}
}

View File

@ -0,0 +1,61 @@
using AirBomber.Generics;
using AirBomber.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber
{
public partial class FormEntityCollection : Form
{
private readonly EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> _entities;
public FormEntityCollection()
{
InitializeComponent();
_entities = new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(
CollectionPictureBox.Width, CollectionPictureBox.Height
);
}
public void ButtonAddEntity_Click(object sender, EventArgs e)
{
BomberForm Form = new BomberForm();
if (Form.ShowDialog() == DialogResult.OK)
{
if (_entities + Form.SelectedRenderer != -1)
{
MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _entities.ShowEntities();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
public void ButtonRemoveEntity_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
int Pos = Convert.ToInt32(NumberMaskedTextBox.Text);
if (_entities - Pos == true)
{
MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _entities.ShowEntities();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
public void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
CollectionPictureBox.Image = _entities.ShowEntities();
}
}
}

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 AirBomber.MovementStrategy;
using AirBomber.Rendering;
namespace AirBomber.Generics
{
internal class EntitiesGenericCollection<T, U>
where T : BomberRendererBase
where U : IMovableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 200;
private readonly SetGeneric<T> _collection;
public EntitiesGenericCollection(int PictureWidth, int PictureHeight)
{
int width = PictureWidth / _placeSizeWidth;
int height = PictureHeight / _placeSizeHeight;
_pictureWidth = PictureWidth;
_pictureHeight = PictureHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(EntitiesGenericCollection<T, U> collect, T? obj)
{
if (obj is null || collect is null)
return -1;
return collect._collection.Insert(obj);
}
public static bool operator -(EntitiesGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
return collect._collection.Remove(pos);
return false;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.MovableObject;
}
public Bitmap ShowEntities()
{
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)
{
for (int i = 0; i < _collection.Count; i++)
{
T? Entity = _collection.Get(i);
if (Entity is null)
continue;
/** Установка позиции */
int NumX = _pictureWidth / _placeSizeWidth;
int PosX = (NumX - 1 - i % NumX) * _placeSizeWidth;
int PosY = (i / NumX) * _placeSizeHeight;
Entity.SetPosition(PosX, PosY);
Entity.DrawEntity(g);
}
}
}
}

View File

@ -0,0 +1,75 @@
namespace AirBomber.Generics
{
internal class SetGeneric<T>
{
private readonly T?[] _objects;
public int Count => _objects.Length;
public SetGeneric(int Count)
{
_objects = new T?[Count];
}
public int Insert(T Entity)
{
for (int i = 0; i < Count; i++)
if (_objects[i] is null)
{
_objects[i] = Entity;
return i;
}
return -1;
}
public int Insert(T Entity, int Position)
{
if (Position >= Count)
return -1;
if (_objects[Position] is null)
{
_objects[Position] = Entity;
return Position;
}
/** Сдвиг элементов вправо начиная с Position до ближайшего пустого места */
int EmptyPos = -1;
for (int i = Position + 1; i < Count; i++)
if (_objects[i] is null)
{
EmptyPos = i;
break;
}
if (EmptyPos == -1)
return -1;
/** Сдвиг */
for (int i = EmptyPos; i > Position; i--)
_objects[i] = _objects[i - 1];
_objects[Position] = Entity;
return Position;
}
public bool Remove(int Position)
{
if (Position >= Count)
return false;
_objects[Position] = default(T);
return true;
}
public T? Get(int Position)
{
if (Position >= Count)
return default(T);
return _objects[Position];
}
}
}

View File

@ -6,7 +6,7 @@ namespace AirBomber
static void Main() static void Main()
{ {
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new BomberForm()); Application.Run(new FormEntityCollection());
} }
} }
} }

View File

@ -25,6 +25,8 @@ namespace AirBomber.Rendering
public int EntityWidth => _bomberWidth; public int EntityWidth => _bomberWidth;
public int EntityHeight => _bomberHeight; public int EntityHeight => _bomberHeight;
public IMovableObject MovableObject => new ObjectEntityRenderer(this);
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height) public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height)
{ {
if (Width < _bomberWidth || Height < _bomberHeight) if (Width < _bomberWidth || Height < _bomberHeight)