Лаба2

This commit is contained in:
Alenka
2023-10-12 18:30:16 +04:00
parent 4d583a149b
commit 929e314196
10 changed files with 612 additions and 124 deletions

View File

@@ -9,5 +9,122 @@ namespace Cruiser.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
/// <summary>
/// Статус перемещения
/// </summary>
private Status _state = Status.NotInit;
/// <summary>
/// Ширина поля
/// </summary>
protected int FieldWidth { get; private set; }
/// <summary>
/// Высота поля
/// </summary>
protected int FieldHeight { get; private set; }
/// <summary>
/// Статус перемещения
/// </summary>
public Status GetStatus() { return _state; }
/// <summary>
/// Установка данных
/// </summary>
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int
height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
/// <summary>
/// Шаг перемещения
/// </summary>
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
/// <summary>
/// Перемещение влево
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveLeft() => MoveTo(Direction.Left);
/// <summary>
/// Перемещение вправо
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveRight() => MoveTo(Direction.Right);
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected bool MoveUp() => MoveTo(Direction.Up);
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected bool MoveDown() => MoveTo(Direction.Down);
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract bool IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private bool MoveTo(Direction directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@@ -4,9 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser
namespace Cruiser.Entities
{
public class Cruiser
public class EntityCruiser
{
public int Speed { get; private set; }
@@ -17,33 +17,24 @@ namespace Cruiser
public Color BodyColor { get; private set; }
public Color AdditionalColor { get; private set; }
// public Color AdditionalColor { get; private set; }
public bool Headlights { get; private set; }
public bool HelicopterPad { get; private set; }
// public bool Headlights { get; private set; }
// public bool HelicopterPad { get; private set; }
public bool Coating { get; private set; }
// public bool Coating { get; private set; }
public double Step => (double)Speed * 100 / Weight;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="headlights">Признак наличия передних фар</param>
/// <param name="helicopterPad">Признак наличия площадки для вертолета</param>
/// <param name="coating">Признак наличия покрытия</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool headlights, bool helicopterPad, bool coating)
public EntityCruiser(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Headlights = headlights;
HelicopterPad = helicopterPad;
Coating = coating;
}
}

View File

@@ -3,76 +3,106 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Entities;
namespace Cruiser
namespace Cruiser.DrawningObjects
{
public class DrawningCruiser
{
public Cruiser? Cruiser { get; private set; }
public EntityCruiser ? EntityCruiser { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
protected int _startPosX;
private int _startPosY;
protected int _startPosY;
private readonly int _cruiserWidth = 150;
private readonly int _cruiserHeight = 50;
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _cruiserWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _cruiserHeight;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="headlights">Признак наличия фар</param>
/// <param name="helicopterPad">Признак наличия антикрыла</param>
/// <param name="coating">Признак наличия гоночной полосы</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool headlights, bool helicopterPad, bool coating, int width, int height)
public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height)
{
if (width < _cruiserWidth || height < _cruiserHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight > _cruiserWidth && _pictureWidth > _cruiserHeight)
{
Cruiser = new Cruiser();
Cruiser.Init(speed, weight, bodyColor, additionalColor,
headlights, helicopterPad, coating);
return true;
}
return false;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
protected DrawningCruiser(int speed, double weight, Color bodyColor, int
width, int height, int carWidth, int carHeight)
{
if (width <= _cruiserWidth || height <= _cruiserHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_cruiserWidth = carWidth;
_cruiserHeight = carHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (Cruiser == null) return;
if (EntityCruiser == null) return;
while (x + _cruiserWidth > _pictureWidth)
{
x -= (int)Cruiser.Step;
x -= (int)EntityCruiser.Step;
}
while (x < 0)
{
x += (int)Cruiser.Step;
x += (int)EntityCruiser.Step;
}
while (y + _cruiserHeight > _pictureHeight)
{
y -= (int)Cruiser.Step;
y -= (int)EntityCruiser.Step;
}
while (y < 0)
{
y += (int)Cruiser.Step;
y += (int)EntityCruiser.Step;
}
_startPosX = x;
@@ -83,7 +113,7 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (Cruiser == null)
if (EntityCruiser == null)
{
return;
}
@@ -91,47 +121,67 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
{
case Direction.Left:
if (_startPosX - Cruiser.Step > 0)
if (_startPosX - EntityCruiser.Step > 0)
{
_startPosX -= (int)Cruiser.Step;
_startPosX -= (int)EntityCruiser.Step;
}
break;
case Direction.Up:
if (_startPosY - Cruiser.Step > 0)
if (_startPosY - EntityCruiser.Step > 0)
{
_startPosY -= (int)Cruiser.Step;
_startPosY -= (int)EntityCruiser.Step;
}
break;
case Direction.Right:
if (_startPosX + Cruiser.Step + _cruiserWidth < _pictureWidth)
if (_startPosX + EntityCruiser.Step + _cruiserWidth < _pictureWidth)
{
_startPosX += (int)Cruiser.Step;
_startPosX += (int)EntityCruiser.Step;
}
break;
case Direction.Down:
if (_startPosY + Cruiser.Step + _cruiserHeight < _pictureHeight)
if (_startPosY + EntityCruiser.Step + _cruiserHeight < _pictureHeight)
{
_startPosY += (int)Cruiser.Step;
_startPosY += (int)EntityCruiser.Step;
}
break;
}
}
public bool CanMove(Direction direction)
{
if (EntityCruiser == null)
{
return false;
}
return direction switch
{
//влево
Direction.Left => _startPosX - EntityCruiser.Step > 0,
//вверх
Direction.Up => _startPosY - EntityCruiser.Step > 0,
// вправо
Direction.Right => _startPosX + EntityCruiser.Step + _cruiserWidth < _pictureWidth,
//вниз
Direction.Down => _startPosY + EntityCruiser.Step + _cruiserHeight < _pictureHeight,
_ => false,
};
}
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (Cruiser == null)
if (EntityCruiser == null)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new
SolidBrush(Cruiser.AdditionalColor);
Pen pen = new Pen(Color.Black);
Brush brush = new SolidBrush(EntityCruiser.BodyColor);
//SolidBrush(Cruiser.AdditionalColor);
//границы автомобиля
@@ -145,7 +195,7 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52);
//если есть доп.фонари
if (Cruiser.Headlights)
/* if (Cruiser.Headlights)
{
Brush brYellow = new SolidBrush(Color.Yellow);
@@ -153,9 +203,9 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
20);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 35, 20,
20);
}
}*/
//основание лодки!!!
Brush br = new SolidBrush(Cruiser.BodyColor);
Brush br = new SolidBrush(EntityCruiser.BodyColor);
g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 90, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 20, _startPosY + 5, 70, 50);
@@ -179,7 +229,7 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
g.DrawRectangle(Pens.Black, _startPosX + 50,
_startPosY + 19, 30, 25);
if (Cruiser.HelicopterPad)
/* if (Cruiser.HelicopterPad)
{
//если есть площадка под вертолет
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 20, 20);
@@ -188,7 +238,7 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
{
//если есть спец покрытие для площадки под вертолет
g.FillEllipse(Brushes.Red, _startPosX + 90, _startPosY + 20, 20, 20);
}
}*/
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
using Cruiser.MovementStrategy;
namespace Cruiser.MovementStrategy
{
internal class DrawningObjectCar : IMoveableObject
{
private readonly DrawningPro? _drawningCar = null;
public DrawningObjectCar(DrawningPro drawningCar)
{
_drawningCar = drawningCar;
}
public ObjectParameters? GetObjectPosition
{
get
{
if (_drawningCar == null || _drawningCar.EntityCruiser ==
null)
{
return null;
}
return new ObjectParameters(_drawningCar.GetPosX,
_drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight);
}
}
public int GetStep => (int)(_drawningCar?.EntityCruiser?.Step ?? 0);
public bool CheckCanMove(Direction direction) =>
_drawningCar?.CanMove(direction) ?? false;
public void MoveObject(Direction direction) =>
_drawningCar?.MoveTransport(direction);
}
}

View File

@@ -0,0 +1,57 @@
using Cruiser.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Entities;
using Cruiser;
namespace Cruiser.DrawningObjects
{
public class DrawningPro : DrawningCruiser
{
public DrawningPro(int speed, double weight, Color bodyColor, Color additionalColor, bool headlights, bool helicopterPad, bool coating, int width, int height) : base(speed, weight, bodyColor, width, height, 100, 50)
{
if (EntityCruiser != null)
{
EntityCruiser = new Pro(speed, weight, bodyColor, additionalColor, headlights, helicopterPad, coating);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser is not Pro cruiser)
{
return;
}
Pen pen = new Pen(Color.Black);
Brush addBrush = new SolidBrush(cruiser.AdditionalColor);
Brush brush = new SolidBrush(cruiser.BodyColor);
base.DrawTransport(g);
if (cruiser.Headlights)
{
Brush brYellow = new SolidBrush(Color.Yellow);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 5, 20,
20);
g.FillEllipse(brYellow, _startPosX + 80, _startPosY + 35, 20,
20);
}
if (cruiser.HelicopterPad)
{
//если есть площадка под вертолет
g.DrawEllipse(pen, _startPosX + 90, _startPosY + 20, 20, 20);
}
/* if (cruiser.Coating)
{
//если есть спец покрытие для площадки под вертолет
g.FillEllipse(Brushes.Red, _startPosX + 90, _startPosY + 20, 20, 20);
}*/
}
}
}

View File

@@ -28,6 +28,9 @@
this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonCreate = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.ButtonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
@@ -60,7 +63,6 @@
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 1;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
@@ -72,7 +74,6 @@
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
@@ -84,7 +85,6 @@
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 3;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
@@ -96,22 +96,57 @@
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 4;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonCreate
//
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonCreate.Location = new System.Drawing.Point(36, 302);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(112, 34);
this.buttonCreate.Size = new System.Drawing.Size(146, 34);
this.buttonCreate.TabIndex = 5;
this.buttonCreate.Text = "Создать";
this.buttonCreate.Text = "Создать Про";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreateSportCar_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button2.Location = new System.Drawing.Point(222, 302);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(185, 34);
this.button2.TabIndex = 6;
this.button2.Text = "Создать простой";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"Центр",
"Угол"});
this.comboBox1.Location = new System.Drawing.Point(415, 22);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(182, 33);
this.comboBox1.TabIndex = 7;
//
// ButtonStep
//
this.ButtonStep.Location = new System.Drawing.Point(485, 70);
this.ButtonStep.Name = "ButtonStep";
this.ButtonStep.Size = new System.Drawing.Size(112, 34);
this.ButtonStep.TabIndex = 8;
this.ButtonStep.Text = "Шаг";
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// CruiserForm
//
this.ClientSize = new System.Drawing.Size(667, 358);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonRight);
@@ -125,61 +160,6 @@
}
/// <param name="e"></param>
private void ButtonCreateSportCar_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
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)), Convert.ToBoolean(random.Next(0, 2)), pictureBox1.Width, pictureBox1.Height);
_drawningCruiser.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningCruiser.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawningCruiser.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawningCruiser.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawningCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
}
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Bitmap bmp = new(pictureBox1.Width,
pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBox1.Image = bmp;
}
#endregion
@@ -190,5 +170,8 @@ private Button button1;
private Button buttonRight;
private Button buttonUp;
private Button buttonCreate;
private Button button2;
private ComboBox comboBox1;
private Button ButtonStep;
}
}

View File

@@ -1,15 +1,124 @@
using System.Windows.Forms;
using Cruiser.DrawningObjects;
using Cruiser.Entities;
using Cruiser.MovementStrategy;
namespace Cruiser
{
public partial class CruiserForm : Form
{
private DrawningCruiser? _drawningCruiser;
private DrawningPro? _drawningCruiser;
private AbstractStrategy? _abstractStrategy;
public CruiserForm()
{
InitializeComponent();
}
private void Draw()
{
if (_drawningCruiser == null)
{
return;
}
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBox1.Image = bmp;
}
private void buttonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonUp":
_drawningCruiser.MoveTransport(Direction.Up);
break;
case "buttonDown":
_drawningCruiser.MoveTransport(Direction.Down);
break;
case "buttonLeft":
_drawningCruiser.MoveTransport(Direction.Left);
break;
case "buttonRight":
_drawningCruiser.MoveTransport(Direction.Right);
break;
}
Draw();
}
private void button2_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningCruiser(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBox1.Width, pictureBox1.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBox1.Enabled)
{
_abstractStrategy = comboBox1.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawningObjectCar(_drawningCruiser), pictureBox1.Width,
pictureBox1.Height);
comboBox1.Enabled = false;
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBox1.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningPro(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
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)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBox1.Width, pictureBox1.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
}
}
@@ -17,3 +126,4 @@ namespace Cruiser

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.MovementStrategy;
namespace Cruiser.MovementStrategy
{
internal class MoveToBorder : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal;
if (Math.Abs(diffX) > GetStep())
{
MoveRight();
}
var diffY = FieldHeight - objParams.ObjectMiddleVertical;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.MovementStrategy;
namespace Cruiser.MovementStrategy
{
internal class MoveToCenter : AbstractStrategy
{
protected override bool IsTargetDestinaion()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return false;
}
return (objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2);
}
protected override void MoveToTarget()
{
var objParams = GetObjectParameters;
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

33
Cruiser/Cruiser/Pro.cs Normal file
View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Cruiser.Entities
{
public class Pro : EntityCruiser
{
public Color AdditionalColor { get; private set; }
public bool Headlights { get; private set; }
public bool HelicopterPad { get; private set; }
public bool Coating { get; private set; }
public Pro(int speed, double weight, Color bodyColor, Color
additionalColor, bool headlights, bool helicopterPad, bool coating) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Headlights = headlights;
HelicopterPad = helicopterPad;
Coating = coating;
}
}
}