Сделанная лаба

This commit is contained in:
Марат Заргаров 2022-11-15 09:13:53 +04:00
parent d26f9b040e
commit f97d2694ef
8 changed files with 154 additions and 34 deletions

View File

@ -8,21 +8,22 @@ namespace DoubleDeckerBus
{
internal abstract class AbstractMap
{
private IDrawningObject _drawningObject = null;
private IDrawningObject _drawingObject = null;
protected int[,] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected readonly Random _random = new();
protected readonly Random _random = new Random();
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawningObject
drawningObject)
public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
_drawingObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
@ -32,26 +33,48 @@ namespace DoubleDeckerBus
}
public Bitmap MoveObject(Direction direction)
{
_drawningObject.MoveObject(direction);
_drawingObject.MoveObject(direction);
bool collision = CheckCollision();
if (collision)
{
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 bool SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
return true;
_drawingObject.SetObject(x, y, _width, _height);
return !CheckCollision();
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
if (_drawingObject == null || _map == null)
{
return bmp;
}
@ -70,12 +93,37 @@ namespace DoubleDeckerBus
}
}
}
_drawningObject.DrawningObject(gr);
_drawingObject.DrawningObject(gr);
return bmp;
}
private bool CheckCollision()
{
var pos = _drawingObject.GetCurrentPosition();
int startX = (int)((pos.Left) / _size_x);
int endX = (int)((pos.Right) / _size_x);
int startY = (int)((pos.Top) / _size_y);
int endY = (int)((pos.Bottom) / _size_y);
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++)
{
if (_map[x, y] == _barrier)
{
return true;
}
}
}
return false;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}

View File

@ -34,7 +34,7 @@ namespace DoubleDeckerBus
/// <summary>
/// Ширина отрисовки автобуса
/// </summary>
private readonly int _busWidth = 112;
private readonly int _busWidth = 115;
/// <summary>
/// Высота отрисовки автобуса
/// </summary>
@ -183,7 +183,7 @@ namespace DoubleDeckerBus
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _busWidth, _startPosY +_busHeight);
return (_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight);
}
}
}

View File

@ -18,10 +18,10 @@ namespace DoubleDeckerBus
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="secondFloor">Признак наличия второго этажа</param>
/// <param name="line">Признак наличия полосы</param>
public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool line) :
base(speed, weight, bodyColor, 112, 50)
public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool stair) :
base(speed, weight, bodyColor, 115, 100)
{
Bus = new EntityDoubleDeckerBus(speed, weight, bodyColor, dopColor, secondFloor, bodyKit, line);
Bus = new EntityDoubleDeckerBus(speed, weight, bodyColor, dopColor, secondFloor, bodyKit, stair);
}
public override void DrawTransport(Graphics g)
{
@ -50,11 +50,22 @@ namespace DoubleDeckerBus
_startPosY += 40;
base.DrawTransport(g);
_startPosY -= 40;
if (doubleDeckerBus.Line)
Brush brBlack = new SolidBrush(Color.Black);
if (doubleDeckerBus.Stair)
{
g.FillRectangle(brBlack, _startPosX + 73, _startPosY + 57, 4, 5);
g.FillRectangle(brBlack, _startPosX + 76, _startPosY + 53, 3, 5);
g.FillRectangle(brBlack, _startPosX + 78, _startPosY + 49, 4, 5);
g.FillEllipse(brBlack, _startPosX + 73, _startPosY + 57, 8, 5);
g.FillEllipse(brBlack, _startPosX + 76, _startPosY + 53, 6, 5);
g.FillEllipse(brBlack, _startPosX + 78, _startPosY + 49, 4, 5);
}
Brush brGray = new SolidBrush(Color.DarkGray);
if (doubleDeckerBus.BodyKit)
{
g.FillRectangle(brGray, _startPosX, _startPosY + 75, 14, 8);
g.FillRectangle(brGray, _startPosX + 32, _startPosY + 75, 53, 8);
g.FillRectangle(brGray, _startPosX + 102, _startPosY + 75, 14, 8);
}
}

View File

@ -28,7 +28,15 @@ namespace DoubleDeckerBus
}
public void DrawningObject(Graphics g)
{
_bus.DrawTransport(g);
if (_bus == null) return;
if (_bus is DrawningDoubleDeckerBus doubleDeckerBus)
{
doubleDeckerBus.DrawTransport(g);
}
else
{
_bus.DrawTransport(g);
}
}
}

View File

@ -23,7 +23,7 @@ namespace DoubleDeckerBus
/// <summary>
/// Признак наличия гоночной полосы
/// </summary>
public bool Line { get; private set; }
public bool Stair { get; private set; }
/// <summary>
/// Инициализация свойств
/// </summary>
@ -33,14 +33,14 @@ namespace DoubleDeckerBus
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="secondFloor">Признак наличия второго этажа</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="line">Признак наличия полосы</param>
public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool line) :
/// <param name="stair">Признак наличия лестницы</param>
public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color dopColor, bool secondFloor, bool bodyKit, bool stair) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
SecondFloor = secondFloor;
BodyKit = bodyKit;
Line = line;
Stair = stair;
}
}

View File

@ -157,11 +157,13 @@
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта"});
"Простая карта",
"Моя карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
this.comboBoxSelectorMap.TabIndex = 8;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// FormMap
//

View File

@ -16,7 +16,7 @@ namespace DoubleDeckerBus
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
_abstractMap = new MyMap();
}
/// <summary>
/// Заполнение информации по объекту
@ -75,14 +75,10 @@ namespace DoubleDeckerBus
private void ButtonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new();
var car = new DrawningDoubleDeckerBus(rnd.Next(100, 300), rnd.Next(1000,
2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,
256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,
256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,
2)), Convert.ToBoolean(rnd.Next(0, 2)));
var car = new DrawningDoubleDeckerBus(rnd.Next(100, 300), rnd.Next(1000,2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0,256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0,2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(car);
}
/// <summary>
@ -97,6 +93,9 @@ namespace DoubleDeckerBus
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Моя карта":
_abstractMap = new MyMap();
break;
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoubleDeckerBus
{
internal class MyMap : AbstractMap
{
/// <summary>
/// Цвет участка закрытого
/// </summary>
private readonly Brush barrierColor = new SolidBrush(Color.Red);
/// <summary>
/// Цвет участка открытого
/// </summary>
private readonly Brush roadColor = new SolidBrush(Color.LightBlue);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}