PIbd-21 Yakovlev M.G. lab work 02 #2
110
SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs
Normal file
110
SailBoat/SailBoat/DrawningObjects/DrawningBoat.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using SailBoat.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.DrawningObjects
|
||||
{
|
||||
public class DrawningBoat
|
||||
{
|
||||
public EntityBoat? EntityBoat { get; protected set; }
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
protected int _startPosX;
|
||||
|
||||
protected int _startPosY;
|
||||
|
||||
protected readonly int _boatWidth = 180;
|
||||
|
||||
protected readonly int _boatHeight = 125;
|
||||
|
||||
public int GetPosX => _startPosX;
|
||||
|
||||
public int GetPosY => _startPosY;
|
||||
|
||||
public int GetWidth => _boatWidth;
|
||||
|
||||
public int GetHight => _boatHeight;
|
||||
|
||||
public bool CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityBoat == null) { return false; }
|
||||
return direction switch
|
||||
{
|
||||
DirectionType.Left => _startPosX - EntityBoat.Step > 0,
|
||||
DirectionType.Up => _startPosY - EntityBoat.Step > 0,
|
||||
DirectionType.Right => _startPosX + EntityBoat.Step + _boatWidth < _pictureWidth,
|
||||
DirectionType.Down => _startPosY + EntityBoat.Step + _boatHeight < _pictureHeight,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
public DrawningBoat(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < 200) { width = 900; }
|
||||
if (height < 200) { height = 500; }
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||
}
|
||||
protected DrawningBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight)
|
||||
{
|
||||
if (width < _boatWidth) { return; }
|
||||
if (height < _boatHeight) { return; }
|
||||
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_boatWidth = boatWidth;
|
||||
_boatHeight = boatHeight;
|
||||
EntityBoat = new EntityBoat(speed, weight, bodyColor);
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0) { x = 0; }
|
||||
else if (x > _pictureWidth) { x = _pictureWidth; }
|
||||
if (y < 0) { y = 0; }
|
||||
else if (y > _pictureHeight) { y = _pictureHeight; }
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityBoat == null) return;
|
||||
switch (direction)
|
||||
{
|
||||
case DirectionType.Left:
|
||||
_startPosX -= (int)EntityBoat.Step;
|
||||
break;
|
||||
case DirectionType.Right:
|
||||
_startPosX += (int)EntityBoat.Step;
|
||||
break;
|
||||
case DirectionType.Up:
|
||||
_startPosY -= (int)EntityBoat.Step;
|
||||
break;
|
||||
case DirectionType.Down:
|
||||
_startPosY += (int)EntityBoat.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBoat == null) return;
|
||||
Pen pen = new(Color.Black);
|
||||
Brush BodyBrush = new SolidBrush(EntityBoat.BodyColor);
|
||||
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90);
|
||||
g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
|
||||
g.FillEllipse(BodyBrush, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
}
|
||||
}
|
||||
}
|
59
SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs
Normal file
59
SailBoat/SailBoat/DrawningObjects/DrawningSailBoat.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SailBoat.Entities;
|
||||
|
||||
namespace SailBoat.DrawningObjects
|
||||
{
|
||||
internal class DrawningSailBoat : DrawningBoat
|
||||
{
|
||||
|
||||
public DrawningSailBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody, int width, int height) : base(speed, weight, bodyColor, width, height, 180,125)
|
||||
{
|
||||
if(EntityBoat != null)
|
||||
{
|
||||
EntityBoat = new EntitySailBoat(speed, weight, bodyColor, additionalColor, sail, rainforcedBody);
|
||||
}
|
||||
}
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityBoat is not EntitySailBoat sailBoat) { return; }
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(sailBoat.AdditionalColor);
|
||||
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90);
|
||||
g.DrawLine(pen, _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
|
||||
g.DrawLine(pen, _startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
|
||||
g.FillEllipse(additionalBrush, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
g.DrawEllipse(pen, _startPosX + 15, _startPosY + 65, 95, 50);
|
||||
|
||||
base.DrawTransport(g);
|
||||
|
||||
if (sailBoat.RainforcedBody)
|
||||
{
|
||||
Brush rainforcedBody = new SolidBrush(Color.DarkGray);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 2, _startPosY + 65, 10, 50);
|
||||
g.DrawRectangle(pen, _startPosX + 2, _startPosY + 65, 10, 50);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 53, 90, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 53, 90, 10);
|
||||
g.FillRectangle(rainforcedBody, _startPosX + 15, _startPosY + 118, 90, 10);
|
||||
g.DrawRectangle(pen, _startPosX + 15, _startPosY + 118, 90, 10);
|
||||
}
|
||||
|
||||
if (sailBoat.Sail)
|
||||
{
|
||||
Brush mast = new SolidBrush(Color.Brown);
|
||||
g.FillRectangle(mast, _startPosX + 60, _startPosY, 4, 90);
|
||||
g.DrawRectangle(pen, _startPosX + 60, _startPosY, 4, 90);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY);
|
||||
g.DrawLine(pen, _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50);
|
||||
g.DrawLine(pen, _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
27
SailBoat/SailBoat/Entities/EntityBoat.cs
Normal file
27
SailBoat/SailBoat/Entities/EntityBoat.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.Entities
|
||||
{
|
||||
public class EntityBoat
|
||||
{
|
||||
|
||||
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 EntityBoat(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
}
|
24
SailBoat/SailBoat/Entities/EntitySailBoat.cs
Normal file
24
SailBoat/SailBoat/Entities/EntitySailBoat.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.Entities
|
||||
{
|
||||
public class EntitySailBoat : EntityBoat
|
||||
{
|
||||
public Color AdditionalColor { get; private set; }
|
||||
|
||||
public bool Sail { get; private set; }
|
||||
|
||||
public bool RainforcedBody { get; private set; }
|
||||
|
||||
public EntitySailBoat(int speed, double weight, Color bodyColor, Color additionalColor, bool sail, bool rainforcedBody) : base(speed, weight, bodyColor)
|
||||
{
|
||||
AdditionalColor = additionalColor;
|
||||
Sail = sail;
|
||||
RainforcedBody = rainforcedBody;
|
||||
}
|
||||
}
|
||||
}
|
82
SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs
Normal file
82
SailBoat/SailBoat/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.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 (IsTargetDestination())
|
||||
{
|
||||
_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 IsTargetDestination();
|
||||
|
||||
private bool MoveTo(DirectionType directionType)
|
||||
{
|
||||
if (_state != Status.InProgress) return false;
|
||||
if(_moveableObject?.CheckCanMove(directionType)?? false)
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
34
SailBoat/SailBoat/MovementStrategy/DrawningObjectBoat.cs
Normal file
34
SailBoat/SailBoat/MovementStrategy/DrawningObjectBoat.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using SailBoat.DrawningObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.MovementStrategy
|
||||
{
|
||||
public class DrawningObjectBoat : IMoveableObject
|
||||
{
|
||||
private readonly DrawningBoat? _drawningBoat = null;
|
||||
|
||||
public DrawningObjectBoat(DrawningBoat drawningBoat)
|
||||
{
|
||||
_drawningBoat = drawningBoat;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningBoat == null || _drawningBoat.EntityBoat == null) return null;
|
||||
return new ObjectParameters(_drawningBoat.GetPosX, _drawningBoat.GetPosY, _drawningBoat.GetWidth, _drawningBoat.GetHight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_drawningBoat?.EntityBoat?.Step ?? 0);
|
||||
|
||||
public bool CheckCanMove(DirectionType direction) => _drawningBoat?.CanMove(direction) ?? false;
|
||||
|
||||
public void MoveObject(DirectionType direction) => _drawningBoat?.MoveTransport(direction);
|
||||
}
|
||||
}
|
20
SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs
Normal file
20
SailBoat/SailBoat/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SailBoat.MovementStrategy
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
int GetStep { get; }
|
||||
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
40
SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs
Normal file
40
SailBoat/SailBoat/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.MovementStrategy
|
||||
{
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
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)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight;
|
||||
if(Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if(diffY < 0)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
48
SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs
Normal file
48
SailBoat/SailBoat/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.MovementStrategy
|
||||
{
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestination()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs
Normal file
39
SailBoat/SailBoat/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.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;
|
||||
}
|
||||
}
|
||||
}
|
17
SailBoat/SailBoat/MovementStrategy/Status.cs
Normal file
17
SailBoat/SailBoat/MovementStrategy/Status.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SailBoat.MovementStrategy
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
|
||||
InProgress,
|
||||
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user