PIbd-22-Romanov-E.V.-Hoisti.../HoistingCrane/HoistingCrane/AbstractMap.cs
10Г Егор Романов 001fc02ae2 исправления
2022-12-04 17:25:38 +04:00

176 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HoistingCrane
{
internal abstract class AbstractMap
{
private IDrawingObject _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 int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
bool isFree = true;
int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x);
int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y);
int hoistingCraneWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
int hoistingCraneHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
switch (direction)
{
// вправо
case Direction.Right:
for (int i = hoistingCraneWidth; i <= hoistingCraneWidth + (int)(_drawingObject.Step / _size_x); i++)
{
for (int j = startPosY; j <= hoistingCraneHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//влево
case Direction.Left:
for (int i = startPosX; i >= (int)(_drawingObject.Step / _size_x); i--)
{
for (int j = startPosY; j <= hoistingCraneHeight; j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//вверх
case Direction.Up:
for (int i = startPosX; i <= hoistingCraneWidth; i++)
{
for (int j = startPosY; j >= (int)(_drawingObject.Step / _size_y); j--)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
//вниз
case Direction.Down:
for (int i = startPosX; i <= hoistingCraneWidth; i++)
{
for (int j = hoistingCraneHeight; j <= hoistingCraneHeight + (int)(_drawingObject.Step / _size_y); j++)
{
if (_map[i, j] == _barrier)
{
isFree = false;
break;
}
}
}
break;
}
if (isFree)
{
_drawingObject.MoveObject(direction);
}
else _drawingObject.MoveObject(SetOppositDirection(direction));
return DrawMapWithObject();
}
private Direction SetOppositDirection(Direction dir)
{
switch (dir)
{
case Direction.Up:
return Direction.Down;
case Direction.Down:
return Direction.Up;
case Direction.Left:
return Direction.Right;
case Direction.Right:
return Direction.Left;
}
return Direction.None;
}
private bool SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x);
int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y);
int hoistingcraneWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
int hoistingCraneHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
for (int i = startPosX; i <= hoistingcraneWidth; i++)
{
for (int j = startPosY; j <= hoistingCraneHeight; j++)
{
if (_map[i, j] == _barrier)
{
return false;
}
}
}
return true;
}
private Bitmap DrawMapWithObject()
{
Bitmap bmp = new(_width, _height);
if (_drawingObject == 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);
}
}
}
_drawingObject.DrawingObject(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);
}
}