diff --git a/MotorBoat/MotorBoat/AbstractMap.cs b/MotorBoat/MotorBoat/AbstractMap.cs index ed803e5..52f6245 100644 --- a/MotorBoat/MotorBoat/AbstractMap.cs +++ b/MotorBoat/MotorBoat/AbstractMap.cs @@ -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() diff --git a/MotorBoat/MotorBoat/Direction.cs b/MotorBoat/MotorBoat/Direction.cs index 8b12ebb..29fb18c 100644 --- a/MotorBoat/MotorBoat/Direction.cs +++ b/MotorBoat/MotorBoat/Direction.cs @@ -12,6 +12,8 @@ namespace MotorBoat Up = 1, Down = 2, Left = 3, - Right = 4 + Right = 4, + DownLeft=5, + DownRight=6, } } diff --git a/MotorBoat/MotorBoat/DrawningMotorBoat.cs b/MotorBoat/MotorBoat/DrawningMotorBoat.cs index ade5892..4988ec3 100644 --- a/MotorBoat/MotorBoat/DrawningMotorBoat.cs +++ b/MotorBoat/MotorBoat/DrawningMotorBoat.cs @@ -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); - } - - - } + } + } } } diff --git a/MotorBoat/MotorBoat/DrawningObjectBoat.cs b/MotorBoat/MotorBoat/DrawningObjectBoat.cs index e5d43bc..36c3771 100644 --- a/MotorBoat/MotorBoat/DrawningObjectBoat.cs +++ b/MotorBoat/MotorBoat/DrawningObjectBoat.cs @@ -35,6 +35,7 @@ namespace MotorBoat void IDrawningObject.DrawningObject(Graphics g) { // TODO + _boat.DrawTransport(g); } } } diff --git a/MotorBoat/MotorBoat/FormBoat.cs b/MotorBoat/MotorBoat/FormBoat.cs index eef3b26..ed9b678 100644 --- a/MotorBoat/MotorBoat/FormBoat.cs +++ b/MotorBoat/MotorBoat/FormBoat.cs @@ -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))); diff --git a/MotorBoat/MotorBoat/FormMap.Designer.cs b/MotorBoat/MotorBoat/FormMap.Designer.cs index 64aacc2..c16d767 100644 --- a/MotorBoat/MotorBoat/FormMap.Designer.cs +++ b/MotorBoat/MotorBoat/FormMap.Designer.cs @@ -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 // diff --git a/MotorBoat/MotorBoat/FormMap.cs b/MotorBoat/MotorBoat/FormMap.cs index 8e50650..24b199d 100644 --- a/MotorBoat/MotorBoat/FormMap.cs +++ b/MotorBoat/MotorBoat/FormMap.cs @@ -17,7 +17,7 @@ namespace MotorBoat public FormMap() { InitializeComponent(); - _abstractMap = new SimpleMap(); + _abstractMap = new SimpleMap(); } /// /// Заполнение информации по объекту @@ -95,6 +95,12 @@ namespace MotorBoat case "Простая карта": _abstractMap = new SimpleMap(); break; + case "Розовая карта": + _abstractMap = new PinkMap(); + break; + case "Морская карта": + _abstractMap = new SeaMap(); + break; } } } diff --git a/MotorBoat/MotorBoat/PinkMap.cs b/MotorBoat/MotorBoat/PinkMap.cs new file mode 100644 index 0000000..0a8140b --- /dev/null +++ b/MotorBoat/MotorBoat/PinkMap.cs @@ -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 + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Purple); + /// + /// Цвет участка открытого + /// + 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++; + } + } + } + } +} diff --git a/MotorBoat/MotorBoat/SeaMap.cs b/MotorBoat/MotorBoat/SeaMap.cs new file mode 100644 index 0000000..937d2f7 --- /dev/null +++ b/MotorBoat/MotorBoat/SeaMap.cs @@ -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 + { + /// + /// Цвет участка закрытого + /// + private readonly Pen barrierColor = new Pen(Color.DarkBlue, 2); + /// + /// Цвет участка открытого + /// + 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++; + } + } + } + } +} diff --git a/MotorBoat/MotorBoat/SimpleMap.cs b/MotorBoat/MotorBoat/SimpleMap.cs index 24746b4..ce40cab 100644 --- a/MotorBoat/MotorBoat/SimpleMap.cs +++ b/MotorBoat/MotorBoat/SimpleMap.cs @@ -16,7 +16,6 @@ namespace MotorBoat /// Цвет участка открытого /// 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));