Collision

This commit is contained in:
shadowik 2022-09-23 14:34:19 +04:00
parent ecbbda9477
commit c38737fa61
2 changed files with 65 additions and 4 deletions

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ScrollBar;
namespace DoubleDeckerBus
{
@ -18,6 +20,7 @@ namespace DoubleDeckerBus
protected readonly int _freeRoad = 0;
protected readonly int _barrier = 1;
public Bitmap CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
@ -33,10 +36,28 @@ namespace DoubleDeckerBus
public Bitmap MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом направлении
if (true)
{
_drawingObject.MoveObject(direction);
_drawingObject.MoveObject(direction);
bool collision = CheckCollision();
if (collision) {
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();
}
private bool SetObjectOnMap()
@ -48,7 +69,15 @@ namespace DoubleDeckerBus
int x = _random.Next(0, 10);
int y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
while (CheckCollision()) {
x = _random.Next(0, 10);
y = _random.Next(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
}
return true;
}
private Bitmap DrawMapWithObject()
@ -75,6 +104,38 @@ namespace DoubleDeckerBus
}
_drawingObject.DrawingObject(gr);
return bmp;
}
private bool CheckCollision() {
var pos = _drawingObject.GetCurrentPosition();
int startX = (int)((pos.Left) / _size_x);
int endX = (int)((pos.Right) / _size_x);
int startY = (int)((pos.Top) / _size_y);
int endY = (int)((pos.Bottom) / _size_y);
if (startX < 0 || startY < 0 || endX > _map.GetLength(1) || endY > _map.GetLength(0)) { return false; }
/* startX = (int)((pos.Left - 5) / _size_x);
endX = (int)((pos.Right + 5) / _size_x);
startY = (int)((pos.Top - 5) / _size_y);
endY = (int)((pos.Bottom + 5) / _size_y);*/
for (int y = startY - 1; y <= endY + 1; y++)
{
for (int x = startX - 1; x <= endX + 1; x++)
{
if (_map[x, y] == _barrier)
{
return true;
break;
}
}
}
return false;
}
protected abstract void GenerateMap();

View File

@ -34,7 +34,7 @@ namespace DoubleDeckerBus
void IDrawingObject.DrawingObject(Graphics g)
{
// TODO
_bus.DrawTransport(g);
}
}
}