2022-09-26 20:56:35 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Artilleries
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
GenerateMap();
|
|
|
|
|
}
|
|
|
|
|
while (!SetObjectOnMap());
|
|
|
|
|
return DrawMapWithObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Bitmap MoveObject(Direction direction)
|
|
|
|
|
{
|
|
|
|
|
_drawingObject.MoveObject(direction);
|
2022-09-26 23:12:22 +04:00
|
|
|
|
if (ObjectIntersects())
|
|
|
|
|
{
|
|
|
|
|
switch (direction)
|
|
|
|
|
{
|
|
|
|
|
case Direction.Left:
|
|
|
|
|
_drawingObject.MoveObject(Direction.Right);
|
|
|
|
|
break;
|
|
|
|
|
case Direction.Right:
|
|
|
|
|
_drawingObject.MoveObject(Direction.Left);
|
|
|
|
|
break;
|
|
|
|
|
case Direction.Up:
|
|
|
|
|
_drawingObject.MoveObject(Direction.Down);
|
|
|
|
|
break;
|
|
|
|
|
case Direction.Down:
|
|
|
|
|
_drawingObject.MoveObject(Direction.Up);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-26 20:56:35 +04:00
|
|
|
|
return DrawMapWithObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool SetObjectOnMap()
|
|
|
|
|
{
|
|
|
|
|
if (_drawingObject == null || _map == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 23:57:25 +04:00
|
|
|
|
for (int i = 2; i < _map.GetLength(0); i++)
|
2022-09-26 23:12:22 +04:00
|
|
|
|
{
|
2022-09-26 23:57:25 +04:00
|
|
|
|
for (int j = 2; j < _map.GetLength(1); j++)
|
2022-09-26 23:12:22 +04:00
|
|
|
|
{
|
|
|
|
|
_drawingObject.SetObject((int) (i * _size_x), (int) (j * _size_y), _width, _height);
|
|
|
|
|
if (!ObjectIntersects()) return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 20:56:35 +04:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 23:12:22 +04:00
|
|
|
|
private bool ObjectIntersects()
|
|
|
|
|
{
|
|
|
|
|
var location = _drawingObject.GetCurrentPosition();
|
|
|
|
|
for (int i = 0; i < _map.GetLength(0); i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < _map.GetLength(1); j++)
|
|
|
|
|
{
|
|
|
|
|
if (_map[i, j] == _barrier)
|
|
|
|
|
{
|
|
|
|
|
if (i * _size_x >= location.Left && (i + 1) * _size_x <= location.Right && j * _size_y >= location.Top && (j + 1) * _size_y <= location.Bottom)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 20:56:35 +04:00
|
|
|
|
private Bitmap DrawMapWithObject()
|
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new Bitmap(_width, _height);
|
|
|
|
|
if (_drawingObject == null || _map == null)
|
|
|
|
|
{
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
Graphics g = 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(g, i, j);
|
|
|
|
|
} else if (_map[i, j] == _barrier)
|
|
|
|
|
{
|
|
|
|
|
DrawBarrierPart(g, i, j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_drawingObject.DrawingObject(g);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|