PIbd-23_Lisov_N.A._AirFight.../AirFighter/AbstractMap.cs

222 lines
7.1 KiB
C#
Raw Normal View History

2022-09-26 23:17:44 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirFighter
{
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();
2022-09-27 16:04:47 +04:00
2022-09-26 23:17:44 +04:00
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Bitmap MoveObject(Direction direction)
{
int CollisionFlag = 0;
2022-09-27 16:04:47 +04:00
Size objSize1 = new Size(100,100);
//COLLISION CHECK
if (direction == Direction.Up)
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
Point LeftTop = new Point((int)_drawingObject.GetCurrentPosition().Left, (int)(_drawingObject.GetCurrentPosition().Top - _drawingObject.Step));
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
2022-09-27 16:04:47 +04:00
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (objectRect.IntersectsWith(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Down)
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
Point LeftTop = new Point((int)_drawingObject.GetCurrentPosition().Left, (int)(_drawingObject.GetCurrentPosition().Top + _drawingObject.Step));
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
2022-09-27 16:04:47 +04:00
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
2022-09-27 16:04:47 +04:00
if (objectRect.IntersectsWith(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Left)
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
Point LeftTop = new Point((int)(_drawingObject.GetCurrentPosition().Left - _drawingObject.Step), (int)_drawingObject.GetCurrentPosition().Top);
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
2022-09-27 16:04:47 +04:00
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
2022-09-27 16:04:47 +04:00
if (objectRect.IntersectsWith(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Right)
{
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
Point LeftTop = new Point((int)(_drawingObject.GetCurrentPosition().Left + _drawingObject.Step), (int)_drawingObject.GetCurrentPosition().Top);
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
2022-09-27 16:04:47 +04:00
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (objectRect.IntersectsWith(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
if (CollisionFlag != 1)
2022-09-26 23:17:44 +04:00
{
_drawingObject.MoveObject(direction);
2022-09-27 16:04:47 +04:00
_drawingObject.DoSomething();
2022-09-26 23:17:44 +04:00
}
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);
2022-09-27 16:04:47 +04:00
Rectangle rectObject = new Rectangle(x, y, 100, 100);
for (int i = 0; i < _map.GetLength(0); i++)
{
for (int j = 0; j < _map.GetLength(1); j++)
{
if (_map[i, j] == _barrier)
{
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (rectObject.IntersectsWith(rectBarrier))
{
return false;
}
}
}
}
2022-09-26 23:17:44 +04:00
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;
}
2022-09-27 16:04:47 +04:00
2022-09-26 23:17:44 +04:00
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
2022-09-27 16:04:47 +04:00
2022-09-26 23:17:44 +04:00
}
}