This commit is contained in:
Alenka 2023-10-12 18:50:26 +04:00
parent 929e314196
commit 5543d1951e
3 changed files with 24 additions and 46 deletions

View File

@ -46,7 +46,7 @@ namespace Cruiser.DrawningObjects
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _cruiserWidth || height < _cruiserHeight)
@ -87,24 +87,11 @@ width, int height, int carWidth, int carHeight)
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (EntityCruiser == null) return;
while (x + _cruiserWidth > _pictureWidth)
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
{
x -= (int)EntityCruiser.Step;
_startPosX = 0;
_startPosY = 0;
}
while (x < 0)
{
x += (int)EntityCruiser.Step;
}
while (y + _cruiserHeight > _pictureHeight)
{
y -= (int)EntityCruiser.Step;
}
while (y < 0)
{
y += (int)EntityCruiser.Step;
}
_startPosX = x;
_startPosY = y;
@ -113,41 +100,29 @@ width, int height, int carWidth, int carHeight)
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityCruiser == null)
if (!CanMove(direction) || EntityCruiser == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityCruiser.Step > 0)
{
_startPosX -= (int)EntityCruiser.Step;
}
_startPosX -= (int)EntityCruiser.Step;
break;
//вверх
case Direction.Up:
if (_startPosY - EntityCruiser.Step > 0)
{
_startPosY -= (int)EntityCruiser.Step;
}
_startPosY -= (int)EntityCruiser.Step;
break;
// вправо
case Direction.Right:
if (_startPosX + EntityCruiser.Step + _cruiserWidth < _pictureWidth)
{
_startPosX += (int)EntityCruiser.Step;
}
_startPosX += (int)EntityCruiser.Step;
break;
//вниз
case Direction.Down:
_startPosY += (int)EntityCruiser.Step;
break;
case Direction.Down:
if (_startPosY + EntityCruiser.Step + _cruiserHeight < _pictureHeight)
{
_startPosY += (int)EntityCruiser.Step;
}
break;
}

View File

@ -10,7 +10,7 @@ namespace Cruiser.MovementStrategy
{
internal class DrawningObjectCar : IMoveableObject
{
private readonly DrawningPro? _drawningCar = null;
private readonly DrawningCruiser? _drawningCar = null;
public DrawningObjectCar(DrawningPro drawningCar)
{
_drawningCar = drawningCar;

View File

@ -1,6 +1,6 @@
using System.Windows.Forms;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace Cruiser
@ -8,7 +8,8 @@ namespace Cruiser
public partial class CruiserForm : Form
{
private DrawningPro? _drawningCruiser;
private DrawningCruiser? _drawningCruiser;
private AbstractStrategy? _abstractStrategy;
public CruiserForm()
{
@ -61,11 +62,13 @@ namespace Cruiser
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{