ISEbd-21_Kadyrov_A.F._Battl.../Battleship/Battleship/MapWithSetBattleshipGeneric.cs

125 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
/// <summary>
/// Карта с набром объектов под нее
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class MapWithSetBattleshipGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 150;
private readonly int _placeSizeHeight = 45;
private readonly SetBattleshipGeneric<T> _setBattleship;
private readonly U _map;
public MapWithSetBattleshipGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBattleship = new SetBattleshipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public static int operator +(MapWithSetBattleshipGeneric<T, U> map, T battleship)
{
return map._setBattleship.Insert(battleship);
}
public static T operator -(MapWithSetBattleshipGeneric<T, U> map, int position)
{
return map._setBattleship.Remove(position);
}
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureWidth);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawBattleship(gr);
return bmp;
}
public Bitmap ShowOnMap()
{
Shaking();
foreach (var battleship in _setBattleship.GetBattleship())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
}
return new(_pictureWidth, _pictureHeight);
}
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
public void Shaking()
{
int j = _setBattleship.Count - 1;
for (int i = 0; i < _setBattleship.Count; i++)
{
if (_setBattleship[i] == null)
{
for (; j > i; j--)
{
var battleship = _setBattleship[j];
if (battleship != null)
{
_setBattleship.Insert(battleship, i);
_setBattleship.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.LightSlateGray, 3);
Brush brush = new SolidBrush(Color.SkyBlue);
g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight + 2);
}
g.DrawLine(pen, i * _placeSizeWidth + 7, 2, i * _placeSizeWidth + 7, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth + 3, 2, i * _placeSizeWidth + 3, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawBattleship(Graphics gr)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var battleship in _setBattleship.GetBattleship())
{
battleship.SetObject(i % width * _placeSizeWidth + 20, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth + 20, _pictureHeight + 10);
battleship.DrawningObject(gr);
i++;
}
}
}
}