diff --git a/WarmlyLocomotive/DrawningWarmlyLocomotive.cs b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs index 5cdc203..354ec17 100644 --- a/WarmlyLocomotive/DrawningWarmlyLocomotive.cs +++ b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs @@ -59,11 +59,13 @@ public class DrawningWarmlyLocomotive /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { - // TODO проверка, что объект "влезает" в размеры поля - // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена - _pictureWidth = width; - _pictureHeight = height; - return true; + if (width >= _drawningLocomotiveWidth && height >= _drawningLocomotiveHeight) { + _pictureWidth = width; + _pictureHeight = height; + return true; + } + return false; + } /// /// Установка позиции @@ -76,10 +78,28 @@ public class DrawningWarmlyLocomotive { return; } - // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы - // то надо изменить координаты, чтобы он оставался в этих границах - _startPosX = x; - _startPosY = y; + + if((x + _drawningLocomotiveWidth > _pictureWidth ) && (y + _drawningLocomotiveHeight > _pictureHeight)) + { + _startPosX = x - _drawningLocomotiveWidth; + _startPosY = y - _drawningLocomotiveHeight; + } + else if((x + _drawningLocomotiveWidth <= _pictureWidth) && (y + _drawningLocomotiveHeight > _pictureHeight)) + { + _startPosX = x; + _startPosY = y - _drawningLocomotiveHeight; + } + else if ((x + _drawningLocomotiveWidth > _pictureWidth) && (y + _drawningLocomotiveHeight <= _pictureHeight)) + { + _startPosX = x - _drawningLocomotiveWidth; + _startPosY = y; + } + else + { + _startPosX = x; + _startPosY = y; + } + } /// /// Изменение направления перемещения @@ -111,14 +131,14 @@ public class DrawningWarmlyLocomotive return true; // вправо case DirectionType.Right: - if (_startPosX.Value + EntityWarmlyLocomotive.Step < _pictureWidth) + if (_startPosX.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveWidth < _pictureWidth) { _startPosX += (int)EntityWarmlyLocomotive.Step; } return true; //вниз case DirectionType.Down: - if (_startPosY.Value + EntityWarmlyLocomotive.Step < _pictureHeight) + if (_startPosY.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveHeight < _pictureHeight) { _startPosY += (int)EntityWarmlyLocomotive.Step; } diff --git a/WarmlyLocomotive/FormWarmlyLocomotive.cs b/WarmlyLocomotive/FormWarmlyLocomotive.cs index 03fb16d..943d472 100644 --- a/WarmlyLocomotive/FormWarmlyLocomotive.cs +++ b/WarmlyLocomotive/FormWarmlyLocomotive.cs @@ -43,10 +43,10 @@ namespace WarmlyLocomotive Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - _drawningWarmlyLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, - pictureBoxWarmlyLocomotive.Height); - _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, - 100)); + _drawningWarmlyLocomotive.SetPictureSize( + pictureBoxWarmlyLocomotive.Width, + pictureBoxWarmlyLocomotive.Height); + _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); }