Фиксация

This commit is contained in:
Макс Бондаренко 2022-10-05 11:04:05 +04:00
parent 5aa3e08ed0
commit 18aeff9872
7 changed files with 155 additions and 26 deletions

View File

@ -32,28 +32,13 @@ namespace WarmlyShip
return DrawMapWithObject(); return DrawMapWithObject();
} }
private bool CanMove(Direction direction) private bool CanMove(Direction direction, (float Left, float Right, float Top, float Bottom) position)
{ {
for (int i = 0; i < _map.GetLength(0); ++i) for (int i = (int)(position.Top / _size_y); i <= (int)(position.Bottom / _size_y); ++i)
{ {
for (int j = 0; j < _map.GetLength(1); ++j) for (int j = (int)(position.Left / _size_x); j <= (int)(position.Right / _size_x); ++j)
{ {
if (direction == Direction.Left && _drawningObject.GetCurrentPosition().Left - _drawningObject.Step < i * _size_x && _drawningObject.GetCurrentPosition().Left > i * _size_x + _size_x && _map[i,j] == _barrier) if (i >= 0 && j >= 0 && _map[i, j] == _barrier) return false;
{
return false;
}
else if (direction == Direction.Up && _drawningObject.GetCurrentPosition().Top - _drawningObject.Step < j * _size_y && _drawningObject.GetCurrentPosition().Top > j * _size_y + _size_y && _map[i, j] == _barrier)
{
return false;
}
else if (direction == Direction.Right && _drawningObject.GetCurrentPosition().Right < i * _size_x && _drawningObject.GetCurrentPosition().Right + _drawningObject.Step > i * _size_x + _size_x && _map[i, j] == _barrier)
{
return false;
}
else if (direction == Direction.Down && _drawningObject.GetCurrentPosition().Bottom < j * _size_y && _drawningObject.GetCurrentPosition().Bottom + _drawningObject.Step > j * _size_y + _size_y && _map[i, j] == _barrier)
{
return false;
}
} }
} }
return true; return true;
@ -61,13 +46,31 @@ namespace WarmlyShip
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
// TODO проверка, что объект может переместится в требуемомнаправлении (float Left, float Right, float Top, float Bottom) position = _drawningObject.GetCurrentPosition();
if (CanMove(direction)) if (direction == Direction.Left)
{
position.Left -= _drawningObject.Step;
}
else if (direction == Direction.Right)
{
position.Right += _drawningObject.Step;
}
else if (direction == Direction.Up)
{
position.Top -= _drawningObject.Step;
}
else if (direction == Direction.Down)
{
position.Bottom += _drawningObject.Step;
}
if (CanMove(direction, position))
{ {
_drawningObject.MoveObject(direction); _drawningObject.MoveObject(direction);
} }
return DrawMapWithObject(); return DrawMapWithObject();
} }
private bool SetObjectOnMap() private bool SetObjectOnMap()
{ {
if (_drawningObject == null || _map == null) if (_drawningObject == null || _map == null)
@ -77,7 +80,13 @@ namespace WarmlyShip
int x = _random.Next(0, 10); int x = _random.Next(0, 10);
int y = _random.Next(0, 10); int y = _random.Next(0, 10);
_drawningObject.SetObject(x, y, _width, _height); _drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки for (int i = (int)(_drawningObject.GetCurrentPosition().Top / _size_y); i <= (int)(_drawningObject.GetCurrentPosition().Bottom / _size_y); ++i)
{
for (int j = (int)(_drawningObject.GetCurrentPosition().Left / _size_x); j <= (int)(_drawningObject.GetCurrentPosition().Right / _size_x); ++j)
{
if (_map[i, j] == _barrier) return false;
}
}
return true; return true;
} }
private Bitmap DrawMapWithObject() private Bitmap DrawMapWithObject()

View File

@ -117,7 +117,7 @@ namespace WarmlyShip
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{ {
return (_startPosX, _startPosY, _startPosX + _warmlyShipWidth, _startPosY + _warmlyShipHeight); return (_startPosX, _startPosX + _warmlyShipWidth, _startPosY, _startPosY + _warmlyShipHeight);
} }
} }
} }

View File

@ -159,7 +159,9 @@
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true; this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] { this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта"}); "Простая карта",
"Вторая карта",
"Последняя карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23); this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);

View File

@ -75,6 +75,12 @@ namespace WarmlyShip
case "Простая карта": case "Простая карта":
_abstractMap = new SimpleMap(); _abstractMap = new SimpleMap();
break; break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
case "Последняя карта":
_abstractMap = new LastMap();
break;
} }
} }

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class LastMap : AbstractMap
{
private readonly Brush barrierColor = new SolidBrush(Color.Red);
private readonly Brush roadColor = new SolidBrush(Color.Green);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y);
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y);
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
while (counter < 45)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
if (x > 0 && y > 0 && x < _map.GetLength(0) - 1 && y < _map.GetLength(1) - 1)
{
_map[x - 1, y] = _barrier;
_map[x + 1, y] = _barrier;
_map[x, y - 1] = _barrier;
_map[x, y + 1] = _barrier;
}
counter++;
}
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WarmlyShip
{
internal class SecondMap : AbstractMap
{
private readonly Brush barrierColor = new SolidBrush(Color.Gold);
private readonly Brush roadColor = new SolidBrush(Color.Black);
protected override void DrawBarrierPart(Graphics g, int i, int j)
{
g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y);
}
protected override void DrawRoadPart(Graphics g, int i, int j)
{
g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y);
}
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
int counter = 0;
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
for (int i = 0; i < _map.GetLength(0); ++i)
{
_map[i, _map.GetLength(1) / 2] = _barrier;
_map[i, _map.GetLength(1) - 1] = _barrier;
}
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[_map.GetLength(0) - 1, j] = _barrier;
}
while (counter < 45)
{
int x = _random.Next(0, 100);
int y = _random.Next(0, 100);
if (_map[x, y] == _freeRoad)
{
_map[x, y] = _barrier;
counter++;
}
}
}
}
}

View File

@ -14,11 +14,11 @@ namespace WarmlyShip
protected override void DrawBarrierPart(Graphics g, int i, int j) protected override void DrawBarrierPart(Graphics g, int i, int j)
{ {
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, _size_x, _size_y); g.FillRectangle(barrierColor, j * _size_x, i * _size_y, _size_x, _size_y);
} }
protected override void DrawRoadPart(Graphics g, int i, int j) protected override void DrawRoadPart(Graphics g, int i, int j)
{ {
g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y); g.FillRectangle(roadColor, j * _size_x, i * _size_y, _size_x, _size_y);
} }
protected override void GenerateMap() protected override void GenerateMap()
{ {