This commit is contained in:
Александр Чегодаев 2023-12-14 00:21:22 +04:00
parent 4a1039d391
commit 7b9c27b2b6
17 changed files with 2585 additions and 197 deletions

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Drawnings;
namespace ProjectCruiser.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(DirectionType.Left);
protected bool MoveRight() => MoveTo(DirectionType.Right);
protected bool MoveUp() => MoveTo(DirectionType.Up);
protected bool MoveDown() => MoveTo(DirectionType.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(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject?.CheckCanMove(directionType) ?? false)
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}
}

View File

@ -4,16 +4,17 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser
namespace ProjectCruiser.Drawnings
{
internal enum DirectionType
public enum DirectionType
{
Up = 1,
Down = 2,
Left = 3,
Right = 4
}
}

View File

@ -1,114 +1,131 @@
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser
using ProjectCruiser.Entities;
using ProjectCruiser.Drawnings;
using System.Drawing.Drawing2D;
namespace ProjectCruiser.DrawningObjects
{
internal class DrawningCruiser
public class DrawningCruiser
{
public EntityCruiser? EntityCruiser { get; private set; }
public EntityCruiser? EntityCruiser { get; protected set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _cruiserWidth = 150;
private readonly int _cruiserHeight = 50;
public bool Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool vert, bool rocket, int width, int height)
protected int _startPosX;
protected int _startPosY;
protected readonly int _cruiserWidth = 145;
protected readonly int _cruiserHeight = 45;
public DrawningCruiser(int speed, double weight, Color bodyColor, int
width, int height)
{
if (width < _pictureWidth || height < _pictureHeight)
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
protected DrawningCruiser(int speed, double weight, Color bodyColor, int
width, int height, int cruiserWidth, int cruiserHeight)
{
if (width <= _pictureWidth || height <= _pictureHeight)
{
return false;
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityCruiser = new EntityCruiser();
EntityCruiser.Init(speed, weight, bodyColor, additionalColor, vert, rocket);
return true;
_cruiserWidth = cruiserWidth;
_cruiserHeight = cruiserHeight;
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
if (x <= _pictureWidth - _cruiserWidth && y <= _pictureHeight - _cruiserHeight)
if (x < 0 || y < 0 || x + _cruiserWidth > _pictureWidth || y + _cruiserHeight > _pictureHeight)
{
_startPosX = x;
_startPosY = y;
x = 10;
y = 10;
}
_startPosX = x;
_startPosY = y;
}
protected int PictureWidth
{
get { return _pictureWidth; }
}
protected int PictureHeight
{
get { return _pictureHeight; }
}
public void MoveTransport(DirectionType direction)
public int GetPosX => _startPosX;
public int GetPosY => _startPosY;
public int GetWidth => _cruiserWidth;
public int GetHeight => _cruiserHeight;
public virtual bool CanMove(DirectionType direction)
{
if (EntityCruiser == null)
{
return false;
}
return direction switch
{
DirectionType.Left => _startPosX - EntityCruiser.Step > 0,
DirectionType.Up => _startPosY - EntityCruiser.Step > 7,
DirectionType.Right => _startPosX + EntityCruiser.Step + _cruiserWidth <= _pictureWidth,
DirectionType.Down => _startPosY + EntityCruiser.Step + _cruiserHeight <= _pictureHeight,
_ => false,
};
}
public virtual void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityCruiser == null)
{
return;
}
switch (direction)
{
case DirectionType.Left:
if (_startPosX - EntityCruiser.Step > 0)
{
_startPosX -= (int)EntityCruiser.Step;
}
else if (_startPosX - EntityCruiser.Step < 0)
{
_startPosX = _startPosX - _startPosX + 3;
}
_startPosX -= (int)EntityCruiser.Step;
break;
case DirectionType.Up:
if (_startPosY - EntityCruiser.Step > 0)
{
_startPosY -= (int)EntityCruiser.Step;
}
else if (_startPosY - EntityCruiser.Step < 0)
{
_startPosY = _startPosY - _startPosY + 0;
}
_startPosY -= (int)EntityCruiser.Step;
break;
case DirectionType.Right:
if (_startPosX + EntityCruiser.Step + _cruiserWidth < _pictureWidth)
{
_startPosX += (int)EntityCruiser.Step;
}
else if (_startPosX + EntityCruiser.Step + _cruiserWidth > _pictureWidth)
{
_startPosX += _pictureWidth - _startPosX - _cruiserWidth;
}
_startPosX += (int)EntityCruiser.Step;
break;
case DirectionType.Down:
if (_startPosY + EntityCruiser.Step + _cruiserHeight < _pictureHeight)
{
_startPosY += (int)EntityCruiser.Step;
}
else if (_startPosY + EntityCruiser.Step + _cruiserHeight > _pictureHeight)
{
_startPosY += _pictureHeight - _startPosY - _cruiserHeight;
}
_startPosY += (int)EntityCruiser.Step;
break;
}
}
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityCruiser == null)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityCruiser.AdditionalColor);
Brush BodyColor = new SolidBrush(EntityCruiser.BodyColor);
GraphicsPath path1 = new GraphicsPath();
@ -118,7 +135,7 @@ additionalColor, bool vert, bool rocket, int width, int height)
path1.AddLine(_startPosX + 100, _startPosY + 50, _startPosX + 150, _startPosY + 25);
path1.AddLine(_startPosX + 100, _startPosY + 0, _startPosX + 150, _startPosY + 25);
g.FillPath(additionalBrush, path1);
g.FillPath(BodyColor, path1);
g.DrawPath(pen, path1);
Brush brBlack = new SolidBrush(Color.Black);
@ -127,30 +144,11 @@ additionalColor, bool vert, bool rocket, int width, int height)
g.FillRectangle(brBlack, _startPosX - 3, _startPosY + 7, 3, 15);
g.DrawRectangle(pen, _startPosX - 3, _startPosY + 25, 3, 15);
g.FillRectangle(brBlack, _startPosX - 3, _startPosY + 25, 3, 15);
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 12, 20, 25);
g.FillRectangle(brBlack, _startPosX + 60, _startPosY + 12, 20, 25);
g.DrawRectangle(pen, _startPosX + 30, _startPosY + 19, 30, 13);
g.FillRectangle(brBlack, _startPosX + 30, _startPosY + 19, 30, 13);
if (EntityCruiser.Vert)
{
Brush brRed = new SolidBrush(Color.Red);
g.FillEllipse(brRed, _startPosX + 95, _startPosY + 15, 20, 20);
g.DrawEllipse(pen, _startPosX + 95, _startPosY + 15, 20, 20);
}
if (EntityCruiser.Rocket)
{
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 3, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 3, 15, 12);
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 18, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 18, 15, 12);
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 33, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 33, 15, 12);
}
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.Drawnings;
using ProjectCruiser.Entities;
namespace ProjectCruiser.DrawningObjects
{
public class DrawningCruiserDou : DrawningCruiser
{
public DrawningCruiserDou(int speed, double weight, Color bodyColor, Color
additionalColor, bool vert, bool rocket, int width, int height) :
base(speed, weight, bodyColor, width, height, 145, 45)
{
if (EntityCruiser != null)
{
EntityCruiser = new EntityCruiserDou(speed, weight, bodyColor,
additionalColor, vert, rocket);
}
}
public override void DrawTransport(Graphics g)
{
if (EntityCruiser is not EntityCruiserDou cruiserWith)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(cruiserWith.AdditionalColor);
Brush BodyColor = new SolidBrush(EntityCruiser.BodyColor);
Brush brBlack = new SolidBrush(Color.Black);
base.DrawTransport(g);
if (cruiserWith.Vert)
{
Brush brRed = new SolidBrush(Color.Red);
g.FillEllipse(brRed, _startPosX + 95, _startPosY + 15, 20, 20);
g.DrawEllipse(pen, _startPosX + 95, _startPosY + 15, 20, 20);
}
if (cruiserWith.Rocket)
{
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 3, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 3, 15, 12);
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 18, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 18, 15, 12);
g.DrawEllipse(pen, _startPosX + 8, _startPosY + 33, 15, 12);
g.FillEllipse(brBlack, _startPosX + 8, _startPosY + 33, 15, 12);
}
}
}
}

View File

@ -4,34 +4,24 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser
namespace ProjectCruiser.Entities
{
internal class EntityCruiser
public class EntityCruiser
{
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 Vert { get; private set; }
public bool Rocket { get; private set; }
public double Step => (double)Speed * 100 / Weight;
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool vert, bool rocket)
public EntityCruiser(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Vert = vert;
Rocket = rocket;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
namespace ProjectCruiser.Entities
{
public class EntityCruiserDou : EntityCruiser
{
public Color AdditionalColor { get; private set; }
public bool Vert { get; private set; }
public bool Rocket { get; private set; }
public EntityCruiserDou(int speed, double weight, Color bodyColor, Color additionalColor, bool vert, bool rocket)
: base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Vert = vert;
Rocket = rocket;
}
}
}

View File

@ -1,4 +1,4 @@
namespace Cruiser
namespace ProjectCruiser
{
partial class FormCruiser
{
@ -28,15 +28,106 @@
/// </summary>
private void InitializeComponent()
{
this.pictureBoxCruiser = new System.Windows.Forms.PictureBox();
this.buttonCreate = new System.Windows.Forms.Button();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormCruiser));
this.comboBoxStrategy = new System.Windows.Forms.ComboBox();
this.ButtonCreateCruiserBat = new System.Windows.Forms.Button();
this.ButtonCreateCruiser = new System.Windows.Forms.Button();
this.ButtonStep = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.pictureBoxCruiser = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit();
this.SuspendLayout();
//
// comboBoxStrategy
//
this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxStrategy.FormattingEnabled = true;
this.comboBoxStrategy.Items.AddRange(new object[] {
"MoveToCenter",
"MoveToBorder",
"-"});
this.comboBoxStrategy.Location = new System.Drawing.Point(667, 12);
this.comboBoxStrategy.Name = "comboBoxStrategy";
this.comboBoxStrategy.Size = new System.Drawing.Size(121, 23);
this.comboBoxStrategy.TabIndex = 0;
//
// ButtonCreateCruiserBat
//
this.ButtonCreateCruiserBat.Location = new System.Drawing.Point(19, 386);
this.ButtonCreateCruiserBat.Name = "ButtonCreateCruiserBat";
this.ButtonCreateCruiserBat.Size = new System.Drawing.Size(160, 57);
this.ButtonCreateCruiserBat.TabIndex = 1;
this.ButtonCreateCruiserBat.Text = "Создать крейсер с ракетными шахтами и площадкой под вертолет";
this.ButtonCreateCruiserBat.UseVisualStyleBackColor = true;
this.ButtonCreateCruiserBat.Click += new System.EventHandler(this.ButtonCreateUstaBat_Click);
//
// ButtonCreateCruiser
//
this.ButtonCreateCruiser.Location = new System.Drawing.Point(185, 386);
this.ButtonCreateCruiser.Name = "ButtonCreateCruiser";
this.ButtonCreateCruiser.Size = new System.Drawing.Size(157, 55);
this.ButtonCreateCruiser.TabIndex = 2;
this.ButtonCreateCruiser.Text = "Создать крейсер";
this.ButtonCreateCruiser.UseVisualStyleBackColor = true;
this.ButtonCreateCruiser.Click += new System.EventHandler(this.ButtonCreateUsta_Click);
//
// ButtonStep
//
this.ButtonStep.Location = new System.Drawing.Point(713, 50);
this.ButtonStep.Name = "ButtonStep";
this.ButtonStep.Size = new System.Drawing.Size(75, 23);
this.ButtonStep.TabIndex = 3;
this.ButtonStep.Text = "Шаг";
this.ButtonStep.UseVisualStyleBackColor = true;
this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click);
//
// buttonUp
//
this.buttonUp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonUp.BackgroundImage")));
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(678, 352);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(44, 38);
this.buttonUp.TabIndex = 4;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonDown.BackgroundImage")));
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(678, 395);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(44, 38);
this.buttonDown.TabIndex = 5;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonLeft
//
this.buttonLeft.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonLeft.BackgroundImage")));
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(629, 395);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(44, 38);
this.buttonLeft.TabIndex = 6;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("buttonRight.BackgroundImage")));
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(727, 395);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(44, 38);
this.buttonRight.TabIndex = 7;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// pictureBoxCruiser
//
this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill;
@ -44,78 +135,22 @@
this.pictureBoxCruiser.Name = "pictureBoxCruiser";
this.pictureBoxCruiser.Size = new System.Drawing.Size(800, 450);
this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCruiser.TabIndex = 0;
this.pictureBoxCruiser.TabIndex = 8;
this.pictureBoxCruiser.TabStop = false;
//
// 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(12, 408);
this.buttonCreate.Name = "buttonCreate";
this.buttonCreate.Size = new System.Drawing.Size(83, 30);
this.buttonCreate.TabIndex = 1;
this.buttonCreate.Text = "Создать";
this.buttonCreate.UseVisualStyleBackColor = true;
this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click);
//
// buttonUp
//
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.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonUp.Location = new System.Drawing.Point(720, 372);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 2;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.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.BackgroundImage = global::Cruiser.Properties.Resources.влево;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonLeft.Location = new System.Drawing.Point(684, 408);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 3;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.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.BackgroundImage = global::Cruiser.Properties.Resources.вниз;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonDown.Location = new System.Drawing.Point(720, 408);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 4;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.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.BackgroundImage = global::Cruiser.Properties.Resources.вправо;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.buttonRight.Location = new System.Drawing.Point(756, 408);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 5;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click);
//
// FormCruiser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonRight);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonLeft);
this.Controls.Add(this.buttonDown);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonCreate);
this.Controls.Add(this.ButtonStep);
this.Controls.Add(this.ButtonCreateCruiser);
this.Controls.Add(this.ButtonCreateCruiserBat);
this.Controls.Add(this.comboBoxStrategy);
this.Controls.Add(this.pictureBoxCruiser);
this.Name = "FormCruiser";
this.Text = "FormCruiser";
@ -126,12 +161,14 @@
}
#endregion
private PictureBox pictureBoxCruiser;
private Button buttonCreate;
private ComboBox comboBoxStrategy;
private Button ButtonCreateCruiserBat;
private Button ButtonCreateCruiser;
private Button ButtonStep;
private Button buttonUp;
private Button buttonLeft;
private Button buttonDown;
private Button buttonLeft;
private Button buttonRight;
private PictureBox pictureBoxCruiser;
}
}

View File

@ -1,11 +1,15 @@
using System.Windows.Forms;
using ProjectCruiser.DrawningObjects;
using ProjectCruiser.Drawnings;
using ProjectCruiser.MovementStrategy;
namespace Cruiser
namespace ProjectCruiser
{
public partial class FormCruiser : Form
{
private DrawningCruiser? _drawningCruiser;
private AbstractStrategy? _abstractStrategy;
public FormCruiser()
{
InitializeComponent();
@ -19,29 +23,41 @@ namespace Cruiser
}
Bitmap bmp = new(pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
Graphics gr = Graphics.FromImage(bmp);
_drawningCruiser.DrawTransport(gr);
pictureBoxCruiser.Image = bmp;
}
private void buttonCreate_Click(object sender, EventArgs e)
private void ButtonCreateUstaBat_Click(object sender, EventArgs e)
{
Random random = new();
_drawningCruiser = new DrawningCruiser();
_drawningCruiser.Init(random.Next(100, 300),
_drawningCruiser = new DrawningCruiserDou(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)), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawningCruiser.SetPosition(random.Next(10, 100),
random.Next(10, 100));
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)
private void ButtonCreateUsta_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 ButtonMove_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
@ -65,5 +81,43 @@ namespace Cruiser
}
Draw();
}
private void ButtonStep_Click(object sender, EventArgs e)
{
if (_drawningCruiser == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new
DrawningObjectCruiser(_drawningCruiser), pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy != null)
{
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
}
}
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectCruiser.DrawningObjects;
using ProjectCruiser.Drawnings;
namespace ProjectCruiser.MovementStrategy
{
public 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(DirectionType direction) =>
_drawningCruiser?.CanMove(direction) ?? false;
public void MoveObject(DirectionType direction) =>
_drawningCruiser?.MoveTransport(direction);
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy
{
public 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())
{
MoveRight();
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.Abs(diffY) > GetStep())
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCruiser.MovementStrategy
{
public 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;
var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.Abs(diffX) > GetStep() || Math.Abs(diffY) > GetStep())
{
if (Math.Abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
if (Math.Abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}
}
}

View File

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

View File

@ -1,4 +1,4 @@
namespace Cruiser
namespace ProjectCruiser
{
internal static class Program
{

View File

@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------
namespace Cruiser.Properties {
namespace ProjectCruiser.Properties {
using System;
@ -39,7 +39,7 @@ namespace Cruiser.Properties {
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Cruiser.Properties.Resources", typeof(Resources).Assembly);
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ProjectCruiser.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;

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 ProjectCruiser.MovementStrategy
{
public enum Status
{
NotInit,
InProgress,
Finish
}
}