From 5aa3e08ed084493c92a65a20f87341c07af5c123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 5 Oct 2022 00:41:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyShip/WarmlyShip/AbstractMap.cs | 29 +++++++++++++++++++++++++++- WarmlyShip/WarmlyShip/SimpleMap.cs | 4 ++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/WarmlyShip/WarmlyShip/AbstractMap.cs b/WarmlyShip/WarmlyShip/AbstractMap.cs index d0fdf65..76b877b 100644 --- a/WarmlyShip/WarmlyShip/AbstractMap.cs +++ b/WarmlyShip/WarmlyShip/AbstractMap.cs @@ -32,10 +32,37 @@ namespace WarmlyShip return DrawMapWithObject(); } + private bool CanMove(Direction direction) + { + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++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) + { + 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; + } + public Bitmap MoveObject(Direction direction) { // TODO проверка, что объект может переместится в требуемомнаправлении - if (true) + if (CanMove(direction)) { _drawningObject.MoveObject(direction); } diff --git a/WarmlyShip/WarmlyShip/SimpleMap.cs b/WarmlyShip/WarmlyShip/SimpleMap.cs index 88588f1..9d9cd7d 100644 --- a/WarmlyShip/WarmlyShip/SimpleMap.cs +++ b/WarmlyShip/WarmlyShip/SimpleMap.cs @@ -14,11 +14,11 @@ namespace WarmlyShip protected override void DrawBarrierPart(Graphics g, int i, int j) { - g.FillRectangle(barrierColor, i * _size_x, j * _size_y, 20, 20); + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, _size_x, _size_y); } protected override void DrawRoadPart(Graphics g, int i, int j) { - g.FillRectangle(roadColor, i * _size_x, j * _size_y, 20, 20); + g.FillRectangle(roadColor, i * _size_x, j * _size_y, _size_x, _size_y); } protected override void GenerateMap() {