Абстрактный класс для работы с картой
This commit is contained in:
parent
d8d3e31191
commit
30575abecf
197
ArmoredCar/ArmoredCar/AbstractMap.cs
Normal file
197
ArmoredCar/ArmoredCar/AbstractMap.cs
Normal file
@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ArmoredCar
|
||||
{
|
||||
internal abstract class AbstractMap
|
||||
{
|
||||
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)
|
||||
{
|
||||
bool flag = true;
|
||||
float step = _drawningObject.Step;
|
||||
(float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition();
|
||||
|
||||
int x1_obj_next = (int)((left - step) / _size_x);
|
||||
int y1_obj_next = (int)((top - step) / _size_y);
|
||||
int x2_obj_next = (int)((right + step) / _size_x);
|
||||
int y2_obj_next = (int)((bottom + step) / _size_y);
|
||||
|
||||
int x1_obj_current = (int)(left / _size_x);
|
||||
int y1_obj_current = (int)(top / _size_y);
|
||||
int x2_obj_current = (int)(right / _size_x);
|
||||
int y2_obj_current = (int)(bottom / _size_y);
|
||||
|
||||
// Проверка возможности перемещения
|
||||
switch (direction)
|
||||
{
|
||||
case Direction.Left:
|
||||
{
|
||||
if (x1_obj_next < 0)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_next; i <= x1_obj_current; i++)
|
||||
{
|
||||
for (int j = y1_obj_current; j <= y2_obj_current; j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Direction.Right:
|
||||
{
|
||||
if (x2_obj_next >= _map.GetLength(0))
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = x2_obj_current; i <= x2_obj_next; i++)
|
||||
{
|
||||
for (int j = y1_obj_current; j <= y2_obj_current; j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Direction.Up:
|
||||
{
|
||||
if (y1_obj_next < 0)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_current; i <= x2_obj_current; i++)
|
||||
{
|
||||
for (int j = y1_obj_next; j <= y1_obj_current; j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Direction.Down:
|
||||
{
|
||||
if (y2_obj_next >= _map.GetLength(0))
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_current; i <= x2_obj_current; i++)
|
||||
{
|
||||
for (int j = y2_obj_current; j <= y2_obj_next; j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag)
|
||||
{
|
||||
_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);
|
||||
(float left, float right, float top, float bottom) = _drawningObject.GetCurrentPosition();
|
||||
for (int i = (int)(x / _size_x); i <= (int) (right / _size_x); i++)
|
||||
{
|
||||
for (int j = (int)(y / _size_y); j <= (int) (bottom / _size_y); j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ namespace ArmoredCar
|
||||
{
|
||||
enum Direction
|
||||
{
|
||||
None = 0,
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
||||
|
Loading…
Reference in New Issue
Block a user