This commit is contained in:
Alenka 2023-11-04 17:53:47 +04:00
parent 315f4dda55
commit 37aa57504d
15 changed files with 567 additions and 158 deletions

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.MovementStrategy
{
public abstract class AbstractStrategy
{
private IMoveableObject? _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth { get; private set; }
protected int FieldHeight { get; private set; }
public Status GetStatus() { return _state; }
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;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected bool MoveLeft() => MoveTo(Direction.Left);
protected bool MoveRight() => MoveTo(Direction.Right);
protected bool MoveUp() => MoveTo(Direction.Up);
protected bool MoveDown() => MoveTo(Direction.Down);
protected ObjectParameters? GetObjectParameters =>
_moveableObject?.GetObjectPosition;
protected int? GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject?.GetStep;
}
protected abstract void MoveToTarget();
protected abstract bool IsTargetDestinaion();
private bool MoveTo(Direction direction)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(direction) ?? false)
{
_moveableObject.MoveObject(direction);
return true;
}
return false;
}
}
}

View File

@ -1,34 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser
{
public class Cruiser
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
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 double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool headlights, bool helicopterPad, bool coating)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Headlights = headlights;
HelicopterPad = helicopterPad;
Coating = coating;
}
}
}

View File

@ -2,23 +2,8 @@
{ {
partial class CruiserForm partial class CruiserForm
{ {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
#region Windows Form Designer generated code #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.pictureBoxCruiser = new System.Windows.Forms.PictureBox(); this.pictureBoxCruiser = new System.Windows.Forms.PictureBox();
@ -26,10 +11,13 @@
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.buttonAdvancedCreate = new System.Windows.Forms.Button();
this.buttonCreate = new System.Windows.Forms.Button(); this.buttonCreate = new System.Windows.Forms.Button();
this.comboBoxCruiser = new System.Windows.Forms.ComboBox();
this.ButtonStep = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// pictureBoxCruiser
this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 0); this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCruiser.Name = "pictureBoxCruiser"; this.pictureBoxCruiser.Name = "pictureBoxCruiser";
@ -37,7 +25,8 @@
this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCruiser.TabIndex = 0; this.pictureBoxCruiser.TabIndex = 0;
this.pictureBoxCruiser.TabStop = false; this.pictureBoxCruiser.TabStop = false;
this.pictureBoxCruiser.Click += new System.EventHandler(this.buttonMove_Click);
// buttonDown
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Cruiser.Properties.Resources.вниз; this.buttonDown.BackgroundImage = global::Cruiser.Properties.Resources.вниз;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -46,8 +35,8 @@
this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 1; this.buttonDown.TabIndex = 1;
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click);
// buttonLeft
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Cruiser.Properties.Resources.влево; this.buttonLeft.BackgroundImage = global::Cruiser.Properties.Resources.влево;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -56,8 +45,8 @@
this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 2; this.buttonLeft.TabIndex = 2;
this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click);
// buttonRight
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Cruiser.Properties.Resources.вправо; this.buttonRight.BackgroundImage = global::Cruiser.Properties.Resources.вправо;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -66,8 +55,8 @@
this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 3; this.buttonRight.TabIndex = 3;
this.buttonRight.UseVisualStyleBackColor = true; this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
// buttonUp
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Cruiser.Properties.Resources.вверх; this.buttonUp.BackgroundImage = global::Cruiser.Properties.Resources.вверх;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
@ -76,19 +65,49 @@
this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 4; this.buttonUp.TabIndex = 4;
this.buttonUp.UseVisualStyleBackColor = true; this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click);
// buttonAdvancedCreate
this.buttonAdvancedCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.buttonAdvancedCreate.Location = new System.Drawing.Point(12, 267);
this.buttonAdvancedCreate.Name = "buttonAdvancedCreate";
this.buttonAdvancedCreate.Size = new System.Drawing.Size(210, 69);
this.buttonAdvancedCreate.TabIndex = 5;
this.buttonAdvancedCreate.Text = "Создать продвинутую версию";
this.buttonAdvancedCreate.UseVisualStyleBackColor = true;
this.buttonAdvancedCreate.Click += new System.EventHandler(this.buttonAdvancedCreate_Click);
// buttonCreate
this.buttonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 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.Location = new System.Drawing.Point(240, 267);
this.buttonCreate.Name = "buttonCreate"; this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(112, 34); this.buttonCreate.Size = new System.Drawing.Size(242, 69);
this.buttonCreate.TabIndex = 5; this.buttonCreate.TabIndex = 6;
this.buttonCreate.Text = "Создать"; this.buttonCreate.Text = "Создать простую версию";
this.buttonCreate.UseVisualStyleBackColor = true; this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
// comboBoxCruiser
this.comboBoxCruiser.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCruiser.FormattingEnabled = true;
this.comboBoxCruiser.Items.AddRange(new object[] {
"Центр",
"Угол"});
this.comboBoxCruiser.Location = new System.Drawing.Point(415, 22);
this.comboBoxCruiser.Name = "comboBoxCruiser";
this.comboBoxCruiser.Size = new System.Drawing.Size(182, 33);
this.comboBoxCruiser.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.ClientSize = new System.Drawing.Size(667, 358);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.comboBoxCruiser);
this.Controls.Add(this.buttonCreate); this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.buttonAdvancedCreate);
this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonLeft);
@ -100,7 +119,6 @@
this.PerformLayout(); this.PerformLayout();
} }
/// <param name="e"></param>
#endregion #endregion
private PictureBox pictureBoxCruiser; private PictureBox pictureBoxCruiser;
@ -108,6 +126,9 @@
private Button buttonLeft; private Button buttonLeft;
private Button buttonRight; private Button buttonRight;
private Button buttonUp; private Button buttonUp;
private Button buttonAdvancedCreate;
private Button buttonCreate; private Button buttonCreate;
private ComboBox comboBoxCruiser;
private Button ButtonStep;
} }
} }

View File

@ -1,31 +1,30 @@
using System.Windows.Forms; using System.Windows.Forms;
using Cruiser.DrawningObjects;
using Cruiser.MovementStrategy;
namespace Cruiser namespace Cruiser
{ {
public partial class CruiserForm : Form public partial class CruiserForm : Form
{ {
private DrawningCruiser? _drawningCruiser; private DrawningCruiser? _drawningCruiser;
private AbstractStrategy? _abstractStrategy;
public CruiserForm() public CruiserForm()
{ {
InitializeComponent(); InitializeComponent();
} }
private void ButtonCreate_Click(object sender, EventArgs e) private void Draw()
{ {
Random random = new(); if (_drawningCruiser == null)
_drawningCruiser = new DrawningCruiser(); {
_drawningCruiser.Init(random.Next(100, 300), return;
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)), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Draw();
} }
private void ButtonMove_Click(object sender, EventArgs e) Bitmap bmp = new Bitmap(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawningCruiser == null) if (_drawningCruiser == null)
{ {
@ -49,18 +48,70 @@ namespace Cruiser
} }
Draw(); Draw();
} }
private void Draw() private void buttonCreate_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)),
pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{ {
if (_drawningCruiser == null) if (_drawningCruiser == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxCruiser.Width, if (comboBoxCruiser.Enabled)
{
_abstractStrategy = comboBoxCruiser.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawningObjectCruiser(_drawningCruiser), pictureBoxCruiser.Width,
pictureBoxCruiser.Height); pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp); comboBoxCruiser.Enabled = false;
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
} }
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxCruiser.Enabled = true;
_abstractStrategy = null;
}
}
private void buttonAdvancedCreate_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningAdvancedCruiser(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)),
pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
} }
} }

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Cruiser namespace Cruiser
{ {
public enum Direction public enum Direction

View File

@ -0,0 +1,48 @@
using Cruiser.DrawningObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Entities;
namespace Cruiser.DrawningObjects
{
public class DrawningAdvancedCruiser : DrawningCruiser
{
public DrawningAdvancedCruiser(int speed, double weight, Color bodyColor, Color additionalColor, bool headlights, bool helicopterPad, bool coating, int width, int height) : base(speed, weight, bodyColor, width, height, 150, 50)
{
if (EntityCruiser != null)
{
EntityCruiser = new EntityAdvancedCruiser(speed, weight, bodyColor, additionalColor, headlights, helicopterPad, coating);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser is not EntityAdvancedCruiser 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.FillEllipse(Brushes.Green, _startPosX + 90, _startPosY + 20, 20, 20);
}
if (cruiser.Coating)
{
g.FillEllipse(Brushes.Red, _startPosX + 80, _startPosY + 20, 20, 20);
g.FillEllipse(Brushes.Red, _startPosX + 60, _startPosY + 20, 20, 20);
}
}
}
}

View File

@ -3,116 +3,118 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cruiser.Entities;
namespace Cruiser namespace Cruiser.DrawningObjects
{ {
public class DrawningCruiser public class DrawningCruiser
{ {
public Cruiser? Cruiser { get; private set; }
public EntityCruiser ? EntityCruiser { get; protected set; }
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
private int _startPosX; protected int _startPosX;
private int _startPosY; protected int _startPosY;
private readonly int _cruiserWidth = 150; private readonly int _cruiserWidth = 150;
private readonly int _cruiserHeight = 50; private readonly int _cruiserHeight = 50;
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _cruiserWidth;
public int GetHeight => _cruiserHeight;
public bool Init(int speed, double weight, Color bodyColor, Color public DrawningCruiser(int speed, double weight, Color bodyColor, int width, int height)
additionalColor, bool headlights, bool helicopterPad, bool coating, int width, int height)
{ {
if (_pictureHeight < _cruiserHeight || _pictureWidth < _cruiserWidth) if (width < _cruiserWidth || height < _cruiserHeight)
{ {
return false; return;
} }
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
Cruiser = new Cruiser(); EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
Cruiser.Init(speed, weight, bodyColor, additionalColor, }
headlights, helicopterPad, coating); protected DrawningCruiser(int speed, double weight, Color bodyColor, int
return true; width, int height, int cruiserWidth, int cruiserHeight)
{
if (width <= _cruiserWidth || height <= _cruiserHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_cruiserWidth = cruiserWidth;
_cruiserHeight = cruiserHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight) if (x < 0 || x + _cruiserWidth > _pictureWidth)
{ {
_startPosX = 0; x = Math.Max(0, _pictureWidth - _cruiserWidth);
_startPosY = 0; }
if (y < 0 || y + _cruiserHeight > _pictureHeight)
{
y = Math.Max(0, _pictureHeight - _cruiserHeight);
} }
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
if (Cruiser == null) if (!CanMove(direction) || EntityCruiser == null)
{ {
return; return;
} }
switch (direction) switch (direction)
{ {
case Direction.Left: case Direction.Left:
if (_startPosX - Cruiser.Step > 0) _startPosX -= (int)EntityCruiser.Step;
{
_startPosX -= (int)Cruiser.Step;
}
break; break;
case Direction.Up: case Direction.Up:
if (_startPosY - Cruiser.Step > 0) _startPosY -= (int)EntityCruiser.Step;
{
_startPosY -= (int)Cruiser.Step;
}
break; break;
case Direction.Right: case Direction.Right:
if (_startPosX + Cruiser.Step + _cruiserWidth < _pictureWidth) _startPosX += (int)EntityCruiser.Step;
{
_startPosX += (int)Cruiser.Step;
}
break; break;
case Direction.Down: case Direction.Down:
if (_startPosY + Cruiser.Step + _cruiserHeight < _pictureHeight) _startPosY += (int)EntityCruiser.Step;
{
_startPosY += (int)Cruiser.Step;
}
break; break;
} }
} }
public void DrawTransport(Graphics g) public bool CanMove(Direction direction)
{ {
if (Cruiser == null) 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,
};
}
public virtual void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{ {
return; return;
} }
Pen pen = new(Color.Black); Pen pen = new Pen(Color.Black);
Brush additionalBrush = new Brush brush = new SolidBrush(EntityCruiser.BodyColor);
SolidBrush(Cruiser.AdditionalColor);
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 5, 20, 20); g.DrawEllipse(pen, _startPosX + 15, _startPosY + 5, 20, 20);
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 35, 20, 20); g.DrawEllipse(pen, _startPosX + 15, _startPosY + 35, 20, 20);
g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30); g.DrawRectangle(pen, _startPosX + 9, _startPosY + 15, 10, 30);
g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10, g.DrawRectangle(pen, _startPosX + 90, _startPosY + 15, 10,
30); 30);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52); g.DrawRectangle(pen, _startPosX + 20, _startPosY + 4, 70, 52);
Brush br = new SolidBrush(EntityCruiser.BodyColor);
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);
}
Brush br = new SolidBrush(Cruiser.BodyColor);
g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30); g.FillRectangle(br, _startPosX + 10, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 90, _startPosY + 15, 10, 30); g.FillRectangle(br, _startPosX + 90, _startPosY + 15, 10, 30);
g.FillRectangle(br, _startPosX + 20, _startPosY + 5, 70, 50); g.FillRectangle(br, _startPosX + 20, _startPosY + 5, 70, 50);
@ -129,21 +131,11 @@ additionalColor, bool headlights, bool helicopterPad, bool coating, int width, i
g.DrawPolygon(pen, points1); g.DrawPolygon(pen, points1);
g.FillRectangle(Brushes.Black, _startPosX + 5, _startPosY + 15, 10, 10); g.FillRectangle(Brushes.Black, _startPosX + 5, _startPosY + 15, 10, 10);
g.FillRectangle(Brushes.Black, _startPosX + 5, _startPosY + 35, 10, 10); g.FillRectangle(Brushes.Black, _startPosX + 5, _startPosY + 35, 10, 10);
g.DrawRectangle(Pens.Black, _startPosX + 35, g.DrawRectangle(Pens.Black, _startPosX + 35,
_startPosY + 23, 15, 15); _startPosY + 23, 15, 15);
g.DrawRectangle(Pens.Black, _startPosX + 50, g.DrawRectangle(Pens.Black, _startPosX + 50,
_startPosY + 19, 30, 25); _startPosY + 19, 30, 25);
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

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

View File

@ -0,0 +1,25 @@
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 EntityAdvancedCruiser : 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 EntityAdvancedCruiser(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;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.Entities
{
public class EntityCruiser
{
public int Speed { get; private set; }
public double Weight { get; private set; }
public Color BodyColor { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public EntityCruiser(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.MovementStrategy
{
public interface IMoveableObject
{
ObjectParameters? GetObjectPosition { get; }
int GetStep { get; }
bool CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}
}

View File

@ -0,0 +1,60 @@
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 = objParams.RightBorder - FieldWidth;
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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();
}
}
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.MovementStrategy
{
public class ObjectParameters
{
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
public int LeftBorder => _x;
public int TopBorder => _y;
public int RightBorder => _x + _width;
public int DownBorder => _y + _height;
public int ObjectMiddleHorizontal => _x + _width / 2;
public int ObjectMiddleVertical => _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}
}

15
Cruiser/Cruiser/Status.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}