229 lines
7.7 KiB
C#
229 lines
7.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ArmoredVehicle
|
|
{
|
|
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 possibility = true;
|
|
(float XTop, float YTop, float XBottom, float YBottom) = _drawningObject.GetCurrentPosition();
|
|
bool busy = false;
|
|
int i;
|
|
int j;
|
|
int jt;
|
|
int i1;
|
|
switch (direction)
|
|
{
|
|
// вправо
|
|
case Direction.Right:
|
|
busy = false;
|
|
i = Math.Abs((int)Math.Ceiling(XBottom / _size_x));
|
|
j = Math.Abs((int)Math.Ceiling(YBottom / _size_y));
|
|
jt = Math.Abs((int)Math.Ceiling(YTop / _size_y));
|
|
i1 = Math.Abs((int)Math.Ceiling((XBottom + _drawningObject.Step)/ _size_x));
|
|
for (int r = i; r < i1; ++r)
|
|
{
|
|
for (int b = jt; b < j; ++b)
|
|
{
|
|
if (_map[r, b] == _barrier)
|
|
{
|
|
busy = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if (busy)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
//влево
|
|
case Direction.Left:
|
|
busy = false;
|
|
i = Math.Abs((int)Math.Ceiling((XTop - _drawningObject.Step )/ _size_x));
|
|
j = Math.Abs((int)Math.Ceiling(YBottom / _size_y));
|
|
jt = Math.Abs((int)Math.Ceiling(YTop / _size_y));
|
|
i1 = Math.Abs((int)Math.Ceiling((XTop)/ _size_x));
|
|
for (int r = i; r < i1; ++r)
|
|
{
|
|
for (int b = jt; b < j; ++b)
|
|
{
|
|
if (_map[r, b] == _barrier)
|
|
{
|
|
busy = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if (busy)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
break;
|
|
//вверх
|
|
case Direction.Up:
|
|
{
|
|
busy = false;
|
|
i = Math.Abs((int)Math.Ceiling((XTop) / _size_x));
|
|
j = Math.Abs((int)((YTop)/ _size_y));
|
|
jt = Math.Abs((int)((YTop - _drawningObject.Step )/ _size_y));
|
|
i1 = Math.Abs((int)Math.Ceiling(XBottom / _size_x));
|
|
for (int r = i; r < i1; ++r)
|
|
{
|
|
for (int b = jt; b < j; ++b)
|
|
{
|
|
if (_map[r, b] == _barrier)
|
|
{
|
|
busy = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if (busy)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
//вниз
|
|
case Direction.Down:
|
|
{
|
|
busy = false;
|
|
i = (int)Math.Ceiling(XTop / _size_x);
|
|
j = (int)((YBottom + _drawningObject.Step) / _size_y);
|
|
jt = (int)(YBottom / _size_y);
|
|
i1 = (int)Math.Ceiling(XBottom / _size_x);
|
|
for (int r = i; r <= i1; r++)
|
|
{
|
|
for (int b = jt; b <= j; b++)
|
|
{
|
|
if (_map[r, b] == _barrier)
|
|
{
|
|
busy = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if (busy)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (busy)
|
|
{
|
|
possibility = false;
|
|
}
|
|
if (possibility)
|
|
{
|
|
_drawningObject.MoveObject(direction);
|
|
}
|
|
return DrawMapWithObject();
|
|
}
|
|
private bool SetObjectOnMap()
|
|
{
|
|
if (_drawningObject == null || _map == null)
|
|
{
|
|
return false;
|
|
}
|
|
(float XTop, float YTop, float XBottom, float YBottom) = _drawningObject.GetCurrentPosition();
|
|
|
|
int x = _random.Next(0, 10);
|
|
int y = _random.Next(0, 10);
|
|
|
|
int beginI = (int)(x / _size_x);
|
|
int endI = (int)(XBottom / _size_x);
|
|
|
|
int beginJ = (int)(y / _size_y);
|
|
int endJ = (int)(YBottom / _size_y);
|
|
|
|
for (int i = beginI; i <= endI; i++)
|
|
{
|
|
for(int j = beginJ; j <= endJ; j++)
|
|
{
|
|
if(_map[i, j] == _barrier)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
_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);
|
|
}
|
|
}
|
|
}
|
|
DrawRec(gr, 50, 50);
|
|
_drawningObject.DrawningObject(gr);
|
|
return bmp;
|
|
}
|
|
public void DrawRec(Graphics g, int countI, int couJj)
|
|
{
|
|
Pen p = new Pen(Color.Black);
|
|
for (int i = 0; i < countI; i++)
|
|
{
|
|
g.DrawLine(p, i * _size_x, 0, i * _size_x, 1000);
|
|
g.DrawLine(p, 0, i * _size_y, 1000, i * _size_y);
|
|
}
|
|
}
|
|
protected abstract void GenerateMap();
|
|
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
|
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
|
}
|
|
} |