Создание методов проверки возможности передвижения.

This commit is contained in:
Programmist73 2022-09-27 10:33:30 +04:00
parent bc0803532e
commit c1158d0aca
9 changed files with 143 additions and 244 deletions

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.VisualBasic.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -33,17 +34,62 @@ namespace Airbus
return DrawMapWithObject();
}
//проверка на "накладывание"
protected bool CheckPosition(float Left, float Right, float Top, float Bottom)
{
int x_start = (int)(Left / _size_x);
int y_start = (int)(Right / _size_y);
int x_end = (int)(Top / _size_x);
int y_end = (int)(Bottom / _size_y);
for(int i = x_start; i <= x_end; i++)
{
for(int j = y_start; j <= y_end; j++)
{
if (_map[i, j] == _barrier)
{
return true;
}
}
}
return false;
}
public Bitmap MoveObject(Direction direction)
{
//Сделать проверку на то, что объект может поместиться в выбранном направлении
if (true)
_drawningObject.MoveObject(direction);
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
if (CheckPosition(Left, Right, Top, Bottom))
{
_drawningObject.MoveObject(direction);
_drawningObject.MoveObject(MoveObjectBack(direction));
}
return DrawMapWithObject();
}
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
{
case Direction.Up:
return Direction.Down;
break;
case Direction.Down:
return Direction.Up;
break;
case Direction.Left:
return Direction.Right;
break;
case Direction.Right:
return Direction.Left;
break;
}
return Direction.None;
}
private bool SetObjectOnMap()
{
if(_drawningObject == null || _map == null)
@ -54,8 +100,40 @@ namespace Airbus
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
//Сделать проверку на то, то объект не "накладывается" на закрытые участки
return true;
(float Left, float Right, float Top, float Bottom) = _drawningObject.GetCurrentPosition();
if(!CheckPosition(Left, Right, Top, Bottom))
{
return true;
}
float x_start = Left;
float y_start = Right;
float x_length = Top - Left;
float y_length = Bottom - Right;
while(CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length))
{
bool check;
do
{
check = CheckPosition(x_start, y_start, x_start + x_length, y_start + y_length);
if (!check)
{
_drawningObject.SetObject((int)x_start, (int)y_start, _width, _height);
return true;
}
else
{
x_start += _size_x;
}
} while (check);
}
x_start = x;
y_start += _size_y;
return false;
}
private Bitmap DrawMapWithObject()

View File

@ -102,23 +102,57 @@ namespace Airbus
}
Pen pen = new(Color.Black);
Brush darkBrush = new SolidBrush(Color.DarkKhaki);
Brush darcBlue = new SolidBrush(Color.DarkSlateBlue);
//корпус
g.FillRectangle(darkBrush, _startPosX, _startPosY + 10, 40, 10);
g.DrawRectangle(pen, _startPosX, _startPosY + 10, 40, 10);
//крыло
Brush darkBrush = new SolidBrush(Color.Black);
g.FillRectangle(darkBrush, _startPosX + 10, _startPosY + 13, 20, 2);
g.FillRectangle(darkBrush, _startPosX + 12, _startPosY + 15, 16, 2);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 13, 20, 2);
g.DrawRectangle(pen, _startPosX + 12, _startPosY + 15, 16, 2);
//хвостовое крыло
Point[] point_tailWing = new Point[]
{
new Point((int)_startPosX, (int)_startPosY + 10),
new Point((int)_startPosX, (int)_startPosY),
new Point((int)_startPosX + 10, (int)_startPosY + 10),
new Point((int)_startPosX, (int)_startPosY + 10)
};
g.FillPolygon(darkBrush, point_tailWing);
g.DrawLine(pen, _startPosX, _startPosY + 10, _startPosX, _startPosY);
g.DrawLine(pen, _startPosX, _startPosY, _startPosX + 10, _startPosY + 10);
//заднее поперечное крыло
g.FillEllipse(darkBrush, _startPosX - 3, _startPosY + 7, 10, 5);
g.DrawEllipse(pen, _startPosX - 3, _startPosY + 7, 10, 5);
//нос самолёта
//верхняя половина
Point[] point_upperBow = new Point[]
{
new Point((int)_startPosX + 40, (int)_startPosY + 15),
new Point((int)_startPosX + 50, (int)_startPosY + 16),
new Point((int)_startPosX + 40, (int)_startPosY + 10),
new Point((int)_startPosX + 40, (int)_startPosY + 15)
};
//нижняя половина
Point[] point_lowerBow = new Point[]
{
new Point((int)_startPosX + 40, (int)_startPosY + 15),
new Point((int)_startPosX + 40, (int)_startPosY + 20),
new Point((int)_startPosX + 50, (int)_startPosY + 16),
new Point((int)_startPosX + 40, (int)_startPosY + 15),
};
g.FillPolygon(darcBlue, point_upperBow);
g.FillPolygon(darkBrush, point_lowerBow);
g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 16);
g.DrawLine(pen, _startPosX + 40, _startPosY + 10, _startPosX + 47, _startPosY + 15);
g.DrawLine(pen, _startPosX + 40, _startPosY + 20, _startPosX + 50, _startPosY + 15);
@ -127,10 +161,16 @@ namespace Airbus
g.FillRectangle(darkBrush, _startPosX + 10, _startPosY + 21, 2, 2);
g.FillRectangle(darkBrush, _startPosX + 12, _startPosY + 23, 4, 4);
g.FillRectangle(darkBrush, _startPosX + 8, _startPosY + 23, 2, 4);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 21, 2, 2);
g.DrawRectangle(pen, _startPosX + 12, _startPosY + 23, 4, 4);
g.DrawRectangle(pen, _startPosX + 8, _startPosY + 23, 2, 4);
//переднее шасси
g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 21, 2, 2);
g.FillRectangle(darkBrush, _startPosX + 35, _startPosY + 23, 4, 4);
g.FillRectangle(darkBrush, _startPosX + 36, _startPosY + 21, 2, 2);
g.FillRectangle(darkBrush, _startPosX + 36, _startPosY + 23, 4, 4);
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 21, 2, 2);
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 23, 4, 4);
}
//смена границ формы отрисовки

View File

@ -45,5 +45,7 @@ namespace Airbus
g.DrawEllipse(pen, _startPosX + 24, _startPosY + 22, 10, 5);
}
}
}
}

View File

@ -13,14 +13,11 @@ namespace Airbus
public partial class FormMap : Form
{
private AbstractMap _abstractMap;
private SecondVersionAbstractMap _secondAbstractMap;
private ThirdVersionAbstractMap _thirdVersionAbstractMap;
public FormMap()
{
InitializeComponent();
_abstractMap = new SimpleMap();
_secondAbstractMap = new SecondSimpleMap();
}
//заполнение информации по объекту
@ -30,21 +27,9 @@ namespace Airbus
toolStripStatusLabelWeight.Text = $"Вес: {airbus.Airbus?.Weight}";
toolStripStatusLabelCorpusColor.Text = $"Цвет: {airbus.Airbus?.CorpusColor.Name}";
if (comboBoxSelectorMap.Text == "Простая карта")
{
pictureBoxAirbus.Image = _abstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height,
new DrawningObjectAirbus(airbus));
}
if (comboBoxSelectorMap.Text == "Буря в пустыне")
{
pictureBoxAirbus.Image = _secondAbstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height,
new DrawningObjectAirbus(airbus));
}
if (comboBoxSelectorMap.Text == "Звёздные войны")
{
pictureBoxAirbus.Image = _thirdVersionAbstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height,
new DrawningObjectAirbus(airbus));
}
pictureBoxAirbus.Image = _abstractMap.CreateMap(pictureBoxAirbus.Width, pictureBoxAirbus.Height,
new DrawningObjectAirbus(airbus));
}
//обработка нажатия кнопки "Создать"
@ -77,18 +62,7 @@ namespace Airbus
break;
}
if (comboBoxSelectorMap.Text == "Простая карта")
{
pictureBoxAirbus.Image = _abstractMap?.MoveObject(dir);
}
if (comboBoxSelectorMap.Text == "Буря в пустыне")
{
pictureBoxAirbus.Image = _secondAbstractMap?.MoveObject(dir);
}
if (comboBoxSelectorMap.Text == "Звёздные войны")
{
pictureBoxAirbus.Image = _thirdVersionAbstractMap?.MoveObject(dir);
}
pictureBoxAirbus.Image = _abstractMap?.MoveObject(dir);
}
//обработка нажития кнопки "Модификация"
@ -110,10 +84,10 @@ namespace Airbus
_abstractMap = new SimpleMap();
break;
case "Буря в пустыне":
_secondAbstractMap = new SecondSimpleMap();
_abstractMap = new SecondSimpleMap();
break;
case "Звёздные войны":
_thirdVersionAbstractMap = new ThirdSimpleMap();
_abstractMap = new ThirdSimpleMap();
break;
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Airbus
{
internal class SecondSimpleMap : SecondVersionAbstractMap
internal class SecondSimpleMap : AbstractMap
{
//цвет закрытого участка
Brush barriedColor = new SolidBrush(Color.DarkRed);
@ -16,17 +16,12 @@ namespace Airbus
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
//float _weightLength = i * (_size_x + 1) - i * _size_x;
//float _heightLength = j * (_size_y + 1) - j * _size_y;
g.FillEllipse(barriedColor, i * _size_x, j * _size_y, 10, 10);
g.FillEllipse(barriedColor, i * _size_x, j * _size_y, _size_x, _size_x);
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
float _weightLength = i * (_size_x + 1);
float _heightLength = j * (_size_y + 1);
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _weightLength, _heightLength);
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
protected override void GenerateMap()

View File

@ -1,95 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal abstract class SecondVersionAbstractMap
{
private IDrawningObject _drawningObject = 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 int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
//Сделать проверку на то, что объект может поместиться в выбранном направлении
if (true)
{
_drawningObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
//Сделать проверку на то, то объект не "накладывается" на закрытые участки
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
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

@ -16,12 +16,12 @@ namespace Airbus
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barriedColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
g.FillRectangle(barriedColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
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));
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
protected override void GenerateMap()

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Airbus
{
internal class ThirdSimpleMap : ThirdVersionAbstractMap
internal class ThirdSimpleMap : AbstractMap
{
Random rnd = new Random();
@ -24,12 +24,12 @@ namespace Airbus
{
randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
barriedColor = new SolidBrush(randomColor);
g.FillRectangle(barriedColor, i * _size_x, j * _size_y, i * (_size_x + 1) + 10, j * (_size_y + 1));
g.FillRectangle(barriedColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
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));
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y);
}
protected override void GenerateMap()

View File

@ -1,95 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Airbus
{
internal abstract class ThirdVersionAbstractMap
{
private IDrawningObject _drawningObject = 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 int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
//Сделать проверку на то, что объект может поместиться в выбранном направлении
if (true)
{
_drawningObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private bool SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
//Сделать проверку на то, то объект не "накладывается" на закрытые участки
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = Graphics.FromImage(bmp);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (_map[i, j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}
}