PIbd-23_Mochalov_D.V._Locom.../Locomotive/Locomotive/AbstractMap.cs

172 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Locomotive
{
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(); // abstract void
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
// added logic must be checked!
bool isFree = true;
int startPosX = (int)(_drawningObject.GetCurrentPosition().Left / _size_x);
int startPosY = (int)(_drawningObject.GetCurrentPosition().Top / _size_y);
int objectWidth = (int)(_drawningObject.GetCurrentPosition().Right / _size_x);
int objectHeight = (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y);
switch (direction)
{
case Direction.Right:
for (int i = objectWidth; i <= objectWidth + (int)(_drawningObject.Step / _size_x); i++)
{
for (int j = startPosY; j <= objectHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
case Direction.Left:
for (int i = startPosX; i >= (int)(_drawningObject.Step / _size_x); i--)
{
for (int j = startPosY; j <= objectHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
case Direction.Up:
for (int i = startPosX; i <= objectWidth; i++)
{
for (int j = startPosY; j >= (int)(_drawningObject.Step / _size_y); j--)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
case Direction.Down:
for (int i = startPosX; i <= objectWidth; i++)
{
for (int j = objectHeight; j <= objectHeight + (int)(_drawningObject.Step / _size_y); j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
}
if (isFree)
{
_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);
// added logic must be checked!
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
if (i * _size_x >= x && j * _size_y >= y &&
i * _size_x <= x + _drawningObject.GetCurrentPosition().Right &&
j * _size_y <= j + _drawningObject.GetCurrentPosition().Bottom)
{
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);
}
}