This commit is contained in:
Дамир Нугаев 2022-11-19 01:35:09 +04:00
parent 69a860866f
commit ef40adc5fc
10 changed files with 287 additions and 51 deletions

View File

@ -54,19 +54,32 @@ namespace Bus
public Bitmap MoveObject(Direction direction)
{
_drawingObject.MoveObject(direction);
(float Left, float Right, float Top, float Bottom) = _drawingObject.GetCurrentPosition();
bool isTrue = true;
bool collision = CheckCollision();
if (CheckBarriers(Left, Right, Top, Bottom))
if (collision)
{
_drawingObject.MoveObject(MoveObjectBack(direction));
switch (direction)
{
case Direction.Left:
_drawingObject.MoveObject(Direction.Right);
break;
case Direction.Right:
_drawingObject.MoveObject(Direction.Left);
break;
case Direction.Up:
_drawingObject.MoveObject(Direction.Down);
break;
case Direction.Down:
_drawingObject.MoveObject(Direction.Up);
break;
}
}
return DrawMapWithObject();
}
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
@ -90,11 +103,10 @@ namespace Bus
{
return false;
}
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
if (!CheckBarriers(0, 0, 0, 0)) return false;
return true;
return !CheckCollision();
}
private Bitmap DrawMapWithObject()
@ -135,8 +147,6 @@ namespace Bus
if (startX < 0 || startY < 0 || endX > _map.GetLength(1) || endY > _map.GetLength(0)) { return false; }
for (int y = startY; y < endY; y++)
{
for (int x = startX; x < endX; x++)

View File

@ -35,5 +35,10 @@ namespace Bus
{
_bus.DrawTransport(g);
}
public string GetInfo() => _bus?.GetDataForSave();
public static IDrawingObject Create(string data) => new DrawingObjectBus(data.CreateDrawingBus());
}
}

View File

@ -13,12 +13,19 @@ namespace Bus
public Color BodyColor { get; set; }
public float Step => Speed * 100 / Weight;
public EntityBus (int speed, float weight, Color bodyColor)
public EntityBus(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <=0 ? rnd.Next(50,150) : speed;
Speed = speed <= 0 ? rnd.Next(50, 150) : speed;
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
public static EntityBus Creator(string data)
{
string[] strs = data.Split(':');
return new EntityBus(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
}
}

43
Bus/Bus/ExtentionBus.cs Normal file
View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal static class ExtentionBus
{
private static readonly char _separatorForObject = ':';
public static DrawingBus CreateDrawingBus(this string info)
{
string[] strs = info.Split(_separatorForObject);
if (strs.Length == 3)
{
return new DrawingBus(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]));
}
if (strs.Length == 7)
{
return new DrawingSportBus(Convert.ToInt32(strs[0]),
Convert.ToInt32(strs[1]), Color.FromName(strs[2]),
Color.FromName(strs[3]), Convert.ToBoolean(strs[4]),
Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]));
}
return null;
}
public static string GetDataForSave(this DrawingBus drawingBus)
{
var bus = drawingBus.Bus;
var str = $"{bus.Speed}{_separatorForObject}{bus.Weight}{_separatorForObject}{bus.BodyColor.Name}";
if (bus is not EntitySportBus sportBus)
{
return str;
}
return $"{str}{_separatorForObject}{sportBus.DopColor.Name}{_separatorForObject}{sportBus.BodyKit}{_separatorForObject}{sportBus.Wing}{_separatorForObject}{sportBus.Sportline}";
}
}
}

View File

@ -41,13 +41,20 @@
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.buttonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.groupBox2.SuspendLayout();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
@ -62,9 +69,9 @@
this.groupBox1.Controls.Add(this.buttonRemoveBus);
this.groupBox1.Controls.Add(this.buttonAddBus);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(598, 0);
this.groupBox1.Location = new System.Drawing.Point(598, 28);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 618);
this.groupBox1.Size = new System.Drawing.Size(250, 590);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -74,7 +81,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Bus.Properties.Resources.Frame_4;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(95, 566);
this.buttonRight.Location = new System.Drawing.Point(95, 538);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(40, 40);
this.buttonRight.TabIndex = 10;
@ -86,7 +93,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Bus.Properties.Resources.Frame_5;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(55, 566);
this.buttonDown.Location = new System.Drawing.Point(55, 538);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(40, 40);
this.buttonDown.TabIndex = 9;
@ -98,7 +105,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Bus.Properties.Resources.Frame_6;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(15, 566);
this.buttonLeft.Location = new System.Drawing.Point(15, 538);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(40, 40);
this.buttonLeft.TabIndex = 8;
@ -110,7 +117,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Bus.Properties.Resources.Frame_3;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(55, 526);
this.buttonUp.Location = new System.Drawing.Point(55, 498);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 7;
@ -179,9 +186,9 @@
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Location = new System.Drawing.Point(0, 28);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(598, 618);
this.pictureBox.Size = new System.Drawing.Size(598, 590);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
@ -199,6 +206,13 @@
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Карты";
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(8, 26);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(125, 27);
this.textBoxNewMapName.TabIndex = 5;
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(8, 203);
@ -229,12 +243,46 @@
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// textBoxNewMapName
// menuStrip
//
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 26);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(125, 27);
this.textBoxNewMapName.TabIndex = 5;
this.menuStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.файлToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(0, 0);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(848, 28);
this.menuStrip.TabIndex = 13;
//
// файлToolStripMenuItem
//
this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.SaveToolStripMenuItem,
this.LoadToolStripMenuItem});
this.файлToolStripMenuItem.Name = айлToolStripMenuItem";
this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24);
this.файлToolStripMenuItem.Text = "Файл";
//
// SaveToolStripMenuItem
//
this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem";
this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.SaveToolStripMenuItem.Text = "Сохранение";
this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click);
//
// LoadToolStripMenuItem
//
this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem";
this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.LoadToolStripMenuItem.Text = "Загрузка";
this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click);
//
// openFileDialog
//
this.openFileDialog.Filter = "txt file | *.txt";
//
// saveFileDialog
//
this.saveFileDialog.Filter = "txt file | *.txt";
//
// FormMapWithSetDoubleDeckerBus
//
@ -244,6 +292,8 @@
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMapWithSetDoubleDeckerBus";
this.Text = "Автобус";
this.groupBox1.ResumeLayout(false);
@ -251,7 +301,10 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
@ -274,5 +327,11 @@
private ListBox listBoxMaps;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
private MenuStrip menuStrip;
private ToolStripMenuItem файлToolStripMenuItem;
private ToolStripMenuItem SaveToolStripMenuItem;
private ToolStripMenuItem LoadToolStripMenuItem;
private OpenFileDialog openFileDialog;
private SaveFileDialog saveFileDialog;
}
}

View File

@ -37,13 +37,11 @@ namespace Bus
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
@ -66,12 +64,10 @@ namespace Bus
map = new MyMap();
break;
}
if(map != null)
{
_mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBusCollectionGeneric = null;
@ -81,7 +77,6 @@ namespace Bus
private void ButtonAddBus_Click(object sender, EventArgs e)
{
var formBusConfig = new FormBusConfig();
// TODO Call method AddEvent from fromBusConfig
formBusConfig.AddEvent(AddBus);
formBusConfig.Show();
}
@ -104,8 +99,7 @@ namespace Bus
}
}
private void ButtonRemoveBus_Click(object sender, EventArgs e)
private void ButtonRemoveBus_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
@ -130,7 +124,7 @@ namespace Bus
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapBusCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -139,7 +133,7 @@ namespace Bus
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapBusCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -148,7 +142,7 @@ namespace Bus
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapBusCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -170,7 +164,7 @@ namespace Bus
dir = Direction.Right;
break;
}
pictureBox.Image = _mapBusCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void ButtonAddMap_Click(object sender, EventArgs e)
@ -208,6 +202,35 @@ namespace Bus
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_mapsCollection.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
ReloadMaps();
}
else
{
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}

View File

@ -57,4 +57,13 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>144, 17</value>
</metadata>
<metadata name="saveFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>311, 17</value>
</metadata>
</root>

View File

@ -13,5 +13,6 @@ namespace Bus
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
(float Left, float Right, float Top, float Bottom) GetCurrentPosition();
string GetInfo();
}
}

View File

@ -70,11 +70,11 @@ namespace Bus
private void Shaking()
{
int j = _setBus.Count - 1;
for (int i = 0; i<_setBus.Count;i++)
for (int i = 0; i < _setBus.Count; i++)
{
if(_setBus[i] == null)
if (_setBus[i] == null)
{
for(; j > i; j--)
for (; j > i; j--)
{
var bus = _setBus[j];
if (bus != null)
@ -140,5 +140,23 @@ namespace Bus
if (currentHeight > height) return;
}
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var bus in _setBus.GetBuses())
{
data += $"{bus.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
_setBus.Insert(DrawingObjectBus.Create(rec) as T);
}
}
}
}

View File

@ -8,7 +8,7 @@ namespace Bus
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>> _mapStorages;
readonly Dictionary<string, MapWithSetDoubleDeckerBusGeneric<IDrawingObject, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
@ -16,9 +16,13 @@ namespace Bus
private readonly int _pictureHeight;
private readonly char separatorDict = '|';
private readonly char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>>();
_mapStorages = new Dictionary<string, MapWithSetDoubleDeckerBusGeneric<IDrawingObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -28,28 +32,85 @@ namespace Bus
if (_mapStorages.ContainsKey(name)) return;
else
{
_mapStorages.Add(name, new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.Add(name, new MapWithSetDoubleDeckerBusGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
// TODO проверяет наличие элемента с определенным ключом
{
if (_mapStorages.ContainsKey(name))
{
_mapStorages.Remove(name);
}
}
public MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> this[string ind] // инициализация индексатора
public MapWithSetDoubleDeckerBusGeneric<IDrawingObject, AbstractMap> this[string ind] // инициализация индексатора
{
get
{
// TODO получает из словаря элемент по ключу
_mapStorages.TryGetValue(ind, out var MapWithSetDoubleDeckerBusGeneric);
return MapWithSetDoubleDeckerBusGeneric;
_mapStorages.TryGetValue(ind, out var result);
return result;
}
}
private static void WriteToFile(string text, FileStream stream)
{
byte[] info = new UTF8Encoding(true).GetBytes(text);
stream.Write(info, 0, info.Length);
}
public bool SaveData(string filename)
{
if (File.Exists(filename))
{
File.Delete(filename);
}
using (StreamWriter fs = new(filename))
{
fs.Write($"MapsCollection{Environment.NewLine}");
foreach (var storage in _mapStorages)
{
fs.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}");
}
}
return true;
}
public bool LoadData(string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (StreamReader sr = new(filename))
{
string str = "";
if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
_mapStorages.Clear();
while ((str = sr.ReadLine()) != null)
{
var elem = str.Split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "MyMap":
map = new MyMap();
break;
}
_mapStorages.Add(elem[0],new MapWithSetDoubleDeckerBusGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries));
}
}
return true;
}
}
}