Свои карты
This commit is contained in:
parent
5341f1556d
commit
fa4cf4f7f0
@ -33,15 +33,115 @@ namespace MotorBoat
|
||||
public Bitmap MoveObject(Direction direction)
|
||||
{
|
||||
// TODO проверка, что объект может переместится в требуемом направлении
|
||||
if (true)
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
|
||||
bool can = true;
|
||||
|
||||
int topYinS = Convert.ToInt32((topY) / _size_y);
|
||||
int rightXinS = Convert.ToInt32((rightX) / _size_x);
|
||||
int leftXinS = Convert.ToInt32((leftX) / _size_x);
|
||||
int bottomYinS = Convert.ToInt32(bottomY / _size_y);
|
||||
|
||||
int stepinS = 0;
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
_drawningObject.MoveObject(direction);
|
||||
case Direction.Up:
|
||||
stepinS = Convert.ToInt32((topY - _drawningObject.Step) / _size_y);
|
||||
if (stepinS < 0) stepinS = 0;
|
||||
if (stepinS != topYinS)
|
||||
{
|
||||
for (int j = topYinS; j > stepinS; j--)
|
||||
{
|
||||
for (int i = leftXinS; i < rightXinS; i++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
can = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!can)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
_drawningObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
|
||||
case Direction.Down:
|
||||
stepinS = Convert.ToInt32((bottomY + _drawningObject.Step) / _size_y);
|
||||
if (stepinS > _height) stepinS = _height;
|
||||
if (stepinS != topYinS)
|
||||
{
|
||||
for (int j = topYinS; j < stepinS; j++)
|
||||
{
|
||||
for (int i = leftXinS; i < rightXinS; i++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
can = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!can)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
_drawningObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
|
||||
case Direction.Left:
|
||||
stepinS = Convert.ToInt32((leftX - _drawningObject.Step) / _size_x);
|
||||
if (stepinS < 0) stepinS = 0;
|
||||
if (stepinS != leftXinS)
|
||||
{
|
||||
for (int j = topYinS; j < bottomYinS; j++)
|
||||
{
|
||||
for (int i = leftXinS; i > stepinS; i--)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
can = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!can)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
_drawningObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
|
||||
case Direction.Right:
|
||||
stepinS = Convert.ToInt32((rightX + _drawningObject.Step) / _size_x);
|
||||
if (stepinS > _width) stepinS = _width;
|
||||
if (stepinS != leftXinS)
|
||||
{
|
||||
for (int j = topYinS; j < bottomYinS; j++)
|
||||
{
|
||||
for (int i = rightXinS; i < stepinS; i++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
can = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!can)
|
||||
{
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
_drawningObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
private bool SetObjectOnMap()
|
||||
{
|
||||
if (_drawningObject == null || _map == null)
|
||||
if (_drawningObject == null || _map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -49,6 +149,26 @@ namespace MotorBoat
|
||||
int y = _random.Next(0, 10);
|
||||
_drawningObject.SetObject(x, y, _width, _height);
|
||||
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
||||
(float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition();
|
||||
//координаты лодки в "клетках" массива карты
|
||||
int topYinS = Convert.ToInt32(topY / _size_y);
|
||||
int rightXinS = Convert.ToInt32(rightX / _size_x);
|
||||
int leftXinS = Convert.ToInt32(leftX / _size_x);
|
||||
int bottomYinS = Convert.ToInt32(bottomY / _size_y);
|
||||
|
||||
if (leftXinS < 0 || bottomYinS > _height || topYinS<0 || rightX > _width) { return false; }
|
||||
|
||||
for (int j = topYinS; j < bottomYinS; j++)
|
||||
{
|
||||
for (int i = leftXinS; i < rightXinS; i++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
private Bitmap DrawMapWithObject()
|
||||
|
@ -12,6 +12,8 @@ namespace MotorBoat
|
||||
Up = 1,
|
||||
Down = 2,
|
||||
Left = 3,
|
||||
Right = 4
|
||||
Right = 4,
|
||||
DownLeft=5,
|
||||
DownRight=6,
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,7 @@ namespace MotorBoat
|
||||
Point[] points;
|
||||
Pen pen = new(Color.Black);
|
||||
Brush dopBrush = new SolidBrush(motorBoat.DopColor);
|
||||
|
||||
|
||||
|
||||
g.FillEllipse(new SolidBrush(Color.Black), _startPosX - 17, _startPosY - 24, 14, 5);
|
||||
g.FillEllipse(new SolidBrush(Color.Black), _startPosX - 17, _startPosY - 21, 14, 5);
|
||||
|
||||
@ -90,9 +89,7 @@ namespace MotorBoat
|
||||
};
|
||||
g.FillPolygon(dopBrush, points);
|
||||
g.DrawPolygon(pen, points);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ namespace MotorBoat
|
||||
void IDrawningObject.DrawningObject(Graphics g)
|
||||
{
|
||||
// TODO
|
||||
_boat.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ namespace MotorBoat
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void ButtonCreate_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
@ -72,7 +71,7 @@ namespace MotorBoat
|
||||
private void buttonCreateModif_Click(object sender, EventArgs e)
|
||||
{
|
||||
Random rnd = new();
|
||||
_boat = new DrawningMotorBoat(rnd.Next(100, 300), rnd.Next(1000, 2000),
|
||||
_boat = new DrawningMotorBoat(rnd.Next(100, 200), rnd.Next(100, 200),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
|
||||
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
|
||||
|
5
MotorBoat/MotorBoat/FormMap.Designer.cs
generated
5
MotorBoat/MotorBoat/FormMap.Designer.cs
generated
@ -159,11 +159,14 @@
|
||||
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.comboBoxSelectorMap.FormattingEnabled = true;
|
||||
this.comboBoxSelectorMap.Items.AddRange(new object[] {
|
||||
"Простая карта"});
|
||||
"Простая карта",
|
||||
"Розовая карта",
|
||||
"Морская карта"});
|
||||
this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 8);
|
||||
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
|
||||
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
|
||||
this.comboBoxSelectorMap.TabIndex = 7;
|
||||
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
|
||||
//
|
||||
// FormMap
|
||||
//
|
||||
|
@ -17,7 +17,7 @@ namespace MotorBoat
|
||||
public FormMap()
|
||||
{
|
||||
InitializeComponent();
|
||||
_abstractMap = new SimpleMap();
|
||||
_abstractMap = new SimpleMap();
|
||||
}
|
||||
/// <summary>
|
||||
/// Заполнение информации по объекту
|
||||
@ -95,6 +95,12 @@ namespace MotorBoat
|
||||
case "Простая карта":
|
||||
_abstractMap = new SimpleMap();
|
||||
break;
|
||||
case "Розовая карта":
|
||||
_abstractMap = new PinkMap();
|
||||
break;
|
||||
case "Морская карта":
|
||||
_abstractMap = new SeaMap();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
MotorBoat/MotorBoat/PinkMap.cs
Normal file
52
MotorBoat/MotorBoat/PinkMap.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorBoat
|
||||
{
|
||||
internal class PinkMap : AbstractMap
|
||||
{
|
||||
/// <summary>
|
||||
/// Цвет участка закрытого
|
||||
/// </summary>
|
||||
private readonly Brush barrierColor = new SolidBrush(Color.Purple);
|
||||
/// <summary>
|
||||
/// Цвет участка открытого
|
||||
/// </summary>
|
||||
private readonly Brush roadColor = new SolidBrush(Color.LightPink);
|
||||
protected override void DrawBarrierPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillEllipse(barrierColor, i *( _size_x-1), j * (_size_y-1), 15, 10);
|
||||
}
|
||||
protected override void DrawRoadPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
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 < 40)
|
||||
{
|
||||
int x = _random.Next(1, 100);
|
||||
int y = _random.Next(1, 100);
|
||||
if (_map[x, y] == _freeRoad)
|
||||
{
|
||||
_map[x, y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
53
MotorBoat/MotorBoat/SeaMap.cs
Normal file
53
MotorBoat/MotorBoat/SeaMap.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorBoat
|
||||
{
|
||||
internal class SeaMap : AbstractMap
|
||||
{
|
||||
/// <summary>
|
||||
/// Цвет участка закрытого
|
||||
/// </summary>
|
||||
private readonly Pen barrierColor = new Pen(Color.DarkBlue, 2);
|
||||
/// <summary>
|
||||
/// Цвет участка открытого
|
||||
/// </summary>
|
||||
private readonly Brush roadColor = new SolidBrush(Color.LightBlue);
|
||||
protected override void DrawBarrierPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.DrawArc(barrierColor, i * (_size_x - 1), j * (_size_y - 1), 7, 6, 0, 180);
|
||||
g.DrawArc(barrierColor, i * (_size_x - 1) + 6, j * (_size_y - 1), 7, 6, 0, 180);
|
||||
}
|
||||
protected override void DrawRoadPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
}
|
||||
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 < 40)
|
||||
{
|
||||
int x = _random.Next(1, 100);
|
||||
int y = _random.Next(1, 100);
|
||||
if (_map[x, y] == _freeRoad)
|
||||
{
|
||||
_map[x, y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,7 +16,6 @@ namespace MotorBoat
|
||||
/// Цвет участка открытого
|
||||
/// </summary>
|
||||
private readonly Brush roadColor = new SolidBrush(Color.Gray);
|
||||
|
||||
protected override void DrawBarrierPart(Graphics g, int i, int j)
|
||||
{
|
||||
g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1));
|
||||
|
Loading…
Reference in New Issue
Block a user