Polevoy S.V Lab_work2 #2
84
SelfPropelledArtilleryUnit/AbstractMap.cs
Normal file
84
SelfPropelledArtilleryUnit/AbstractMap.cs
Normal file
@ -0,0 +1,84 @@
|
||||
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)
|
||||
{
|
||||
// TODO доделать коллизию
|
||||
_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 проверка на налажение
|
||||
return true;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ namespace Artilleries
|
||||
pictureBoxArtilleries.Image = bmp;
|
||||
}
|
||||
|
||||
private void Prepair()
|
||||
private void SetData()
|
||||
{
|
||||
Random rnd = new();
|
||||
_artillery.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxArtilleries.Width, pictureBoxArtilleries.Height);
|
||||
@ -30,7 +30,7 @@ namespace Artilleries
|
||||
{
|
||||
Random rnd = new();
|
||||
_artillery = new DrawingArtillery(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
|
||||
Prepair();
|
||||
SetData();
|
||||
Draw();
|
||||
}
|
||||
|
||||
@ -44,7 +44,7 @@ namespace Artilleries
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
rnd.Next(0, 2) == 1, rnd.Next(0, 2) == 1
|
||||
);
|
||||
Prepair();
|
||||
SetData();
|
||||
Draw();
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Artilleries
|
||||
{
|
||||
internal interface IDrawningObject
|
||||
internal interface IDrawingObject
|
||||
{
|
||||
public float Step { get; }
|
||||
void SetObject(int x, int y, int width, int height);
|
Loading…
Reference in New Issue
Block a user