249 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContainerShip
{
internal abstract class AbstractMap : IEquatable<AbstractMap>
{
private IDrawingObject _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 _water = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject
drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
bool moveAccept = true;
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
int xObjWidth = (int)Math.Ceiling((Right - Left) / _size_x);
int yObjHeight = (int)Math.Ceiling((Bottom - Top) / _size_y);
int vertStep = (int)Math.Ceiling(_drawningObject.Step / _size_y);
int horizStep = (int)Math.Ceiling(_drawningObject.Step / _size_x);
int xObjLeftBorder = (int)Math.Floor(Left / _size_x);
int xObjRightBorder = (int)Math.Ceiling(Right / _size_x);
int yObjTopBorder = (int)Math.Floor(Top / _size_y);
int yObjBottomBorder = (int)Math.Ceiling(Bottom / _size_y);
switch (direction)
{
case Direction.Up:
for (int i = 0; i < vertStep; i++)
{
if (!moveAccept)
{
break;
}
for (int j = 0; j < xObjWidth; j++)
{
if (yObjTopBorder - i < 0 || xObjLeftBorder + j >= _map.GetLength(1))
{
break;
}
if (_map[xObjLeftBorder + j, yObjTopBorder - i] == _barrier)
{
moveAccept = false;
break;
}
}
}
break;
case Direction.Down:
for (int i = 0; i < vertStep; i++)
{
if (!moveAccept)
{
break;
}
for (int j = 0; j < xObjWidth; j++)
{
if (yObjBottomBorder + i >= _map.GetLength(0) || xObjLeftBorder + j >= _map.GetLength(1))
{
break;
}
if (_map[xObjLeftBorder + j, yObjBottomBorder + i] == _barrier)
{
moveAccept = false;
break;
}
}
}
break;
case Direction.Left:
for (int i = 0; i < yObjHeight; i++)
{
if (!moveAccept)
{
break;
}
for (int j = 0; j < horizStep; j++)
{
if (yObjTopBorder + i >= _map.GetLength(0) || xObjLeftBorder - j < 0)
{
break;
}
if (_map[xObjLeftBorder - j, yObjTopBorder + i] == _barrier)
{
moveAccept = false;
break;
}
}
}
break;
case Direction.Right:
for (int i = 0; i < yObjHeight; i++)
{
if (!moveAccept)
{
break;
}
for (int j = 0; j < horizStep; j++)
{
if (yObjTopBorder + i >= _map.GetLength(0) || xObjRightBorder + j >= _map.GetLength(1))
{
break;
}
if (_map[xObjRightBorder + j, yObjTopBorder + i] == _barrier)
{
moveAccept = false;
break;
}
}
}
break;
}
if (moveAccept)
{
_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);
(float Left, float Top, float Right, float Bottom) = _drawningObject.GetCurrentPosition();
int xObjWidth = (int)Math.Ceiling((Right - Left) / _size_x);
int yObjHeight = (int)Math.Ceiling((Bottom - Top) / _size_y);
int xObjLeftBorder = (int)Math.Floor(Left / _size_x);
int yObjTopBorder = (int)Math.Floor(Top / _size_y);
while (y < _height - (Bottom - Top))
{
while (x < _width - (Right - Left))
{
if (CheckSpawnArea(xObjWidth, yObjHeight, xObjLeftBorder, yObjTopBorder))
{
_drawningObject.SetObject(x, y, _width, _height);
return true;
}
x += (int)_size_x;
xObjLeftBorder = (int)(x / _size_x);
}
x = 0;
y += (int)_size_y;
yObjTopBorder = (int)(y / _size_y);
}
return false;
}
private bool CheckSpawnArea(int xObjWidth, int yObjHeight, int xObjLeftBorder, int yObjTopBorder)
{
for (int i = 0; i <= yObjHeight; i++)
{
for (int j = 0; j <= xObjWidth; j++)
{
if (yObjTopBorder + i >= _map.GetLength(0) || xObjLeftBorder + j >= _map.GetLength(1) || _map[xObjLeftBorder + j, yObjTopBorder + i] == _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] == _water)
{
DrawWaterPart(gr, i, j);
}
else if (_map[i, j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawWaterPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
public bool Equals(AbstractMap? other)
{
if (other == null ||
_map != other._map ||
_width != other._width ||
_size_x != other._size_x ||
_size_y != other._size_y ||
_height != other._height ||
GetType() != other.GetType() ||
_map.GetLength(0) != other._map.GetLength(0) ||
_map.GetLength(1) != other._map.GetLength(1))
{
return false;
}
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] != other._map[i, j])
{
return false;
}
}
}
return true;
}
}
}