165 lines
5.9 KiB
C#
165 lines
5.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Catamaran
|
|||
|
{
|
|||
|
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 Random();
|
|||
|
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 boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
|
|||
|
int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
|
|||
|
|
|||
|
switch (direction)
|
|||
|
{
|
|||
|
// вправо
|
|||
|
case Direction.Right:
|
|||
|
for (int i = boatWidth; i <= boatWidth + (int)(_drawingObject.Step / _size_x); i++)
|
|||
|
{
|
|||
|
for (int j = startPosY; j <= boatHeight; 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 <= boatHeight; j++)
|
|||
|
{
|
|||
|
if (_map[i, j] == _barrier)
|
|||
|
{
|
|||
|
isFree = false;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
break;
|
|||
|
//вверх
|
|||
|
case Direction.Up:
|
|||
|
for (int i = startPosX; i <= boatWidth; 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 <= boatWidth; i++)
|
|||
|
{
|
|||
|
for (int j = boatHeight; j <= boatHeight + (int)(_drawingObject.Step / _size_y); j++)
|
|||
|
{
|
|||
|
if (_map[i, j] == _barrier)
|
|||
|
{
|
|||
|
isFree = false;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (isFree)
|
|||
|
{
|
|||
|
_drawingObject.MoveObject(direction);
|
|||
|
}
|
|||
|
return DrawMapWithObject();
|
|||
|
}
|
|||
|
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);
|
|||
|
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
|||
|
_drawingObject.SetObject(x, y, _width, _height);
|
|||
|
int startPosX = (int)(_drawingObject.GetCurrentPosition().Left / _size_x);
|
|||
|
int startPosY = (int)(_drawingObject.GetCurrentPosition().Right / _size_y);
|
|||
|
int boatWidth = (int)(_drawingObject.GetCurrentPosition().Top / _size_x);
|
|||
|
int boatHeight = (int)(_drawingObject.GetCurrentPosition().Bottom / _size_y);
|
|||
|
for (int i = startPosX; i <= boatWidth; i++)
|
|||
|
{
|
|||
|
for (int j = startPosY; j <= boatHeight; j++)
|
|||
|
{
|
|||
|
if (_map[i, j] == _barrier)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
private Bitmap DrawMapWithObject()
|
|||
|
{
|
|||
|
Bitmap bmp = new Bitmap(_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);
|
|||
|
|
|||
|
}
|
|||
|
}
|