Polevoy S.V Lab_work2 #2
@ -33,8 +33,25 @@ namespace Artilleries
|
||||
|
||||
public Bitmap MoveObject(Direction direction)
|
||||
{
|
||||
// TODO доделать коллизию
|
||||
_drawingObject.MoveObject(direction);
|
||||
if (ObjectIntersects())
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case Direction.Left:
|
||||
_drawingObject.MoveObject(Direction.Right);
|
||||
break;
|
||||
case Direction.Right:
|
||||
_drawingObject.MoveObject(Direction.Left);
|
||||
break;
|
||||
case Direction.Up:
|
||||
_drawingObject.MoveObject(Direction.Down);
|
||||
break;
|
||||
case Direction.Down:
|
||||
_drawingObject.MoveObject(Direction.Up);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
|
||||
@ -45,13 +62,38 @@ namespace Artilleries
|
||||
return false;
|
||||
}
|
||||
|
||||
int x = _random.Next(0, 10);
|
||||
int y = _random.Next(0, 10);
|
||||
_drawingObject.SetObject(x, y, _width, _height);
|
||||
// TODO проверка на налажение
|
||||
for (int i = 0; i < _map.GetLength(0); i++)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); j++)
|
||||
{
|
||||
_drawingObject.SetObject((int) (i * _size_x), (int) (j * _size_y), _width, _height);
|
||||
if (!ObjectIntersects()) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool ObjectIntersects()
|
||||
{
|
||||
var location = _drawingObject.GetCurrentPosition();
|
||||
for (int i = 0; i < _map.GetLength(0); i++)
|
||||
{
|
||||
for (int j = 0; j < _map.GetLength(1); j++)
|
||||
{
|
||||
if (_map[i, j] == _barrier)
|
||||
{
|
||||
if (i * _size_x >= location.Left && (i + 1) * _size_x <= location.Right && j * _size_y >= location.Top && (j + 1) * _size_y <= location.Bottom)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Bitmap DrawMapWithObject()
|
||||
{
|
||||
Bitmap bmp = new Bitmap(_width, _height);
|
||||
|
53
SelfPropelledArtilleryUnit/ForestMap.cs
Normal file
53
SelfPropelledArtilleryUnit/ForestMap.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Artilleries
|
||||
{
|
||||
internal class ForestMap : AbstractMap
|
||||
{
|
||||
private readonly Brush barrierColor = new SolidBrush(Color.Green);
|
||||
private readonly Brush roadColor = new SolidBrush(Color.Brown);
|
||||
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));
|
||||
}
|
||||
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[50, 50];
|
||||
_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 < 20)
|
||||
{
|
||||
int x = _random.Next(2, 49);
|
||||
int y = _random.Next(3, 50);
|
||||
var points = new int[] { _map[x, y], _map[x, y - 1], _map[x, y - 2], _map[x - 1, y - 2], _map[x + 1, y - 2], _map[x, y - 3] };
|
||||
var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad };
|
||||
if (points.SequenceEqual(forComparison))
|
||||
{
|
||||
_map[x, y] = _barrier;
|
||||
_map[x, y - 1] = _barrier;
|
||||
_map[x, y - 2] = _barrier;
|
||||
_map[x - 1, y - 2] = _barrier;
|
||||
_map[x + 1, y - 2] = _barrier;
|
||||
_map[x, y - 3] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
SelfPropelledArtilleryUnit/FormMap.Designer.cs
generated
3
SelfPropelledArtilleryUnit/FormMap.Designer.cs
generated
@ -158,7 +158,8 @@
|
||||
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(12, 12);
|
||||
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
|
||||
this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 23);
|
||||
|
@ -79,6 +79,9 @@ namespace Artilleries
|
||||
case "Простая карта":
|
||||
_abstractMap = new SimpleMap();
|
||||
break;
|
||||
case "Лесная карта":
|
||||
_abstractMap = new ForestMap();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user