Создание классов, дополнения классов (как во второй базовой)
This commit is contained in:
parent
dd3c707953
commit
64b0091916
70
AirBomber/AirBomber/AbstractStrategy.cs
Normal file
70
AirBomber/AirBomber/AbstractStrategy.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -15,8 +15,7 @@ namespace AirBomber
|
||||
protected int _startPosY;
|
||||
protected readonly int _airPlaneWidth = 150;
|
||||
protected readonly int _airPlaneHeight = 118;
|
||||
public DrawningAirPlane(int speed, double weight, Color bodyColor, int
|
||||
width, int height)
|
||||
public DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height)
|
||||
{
|
||||
if (width < _airPlaneWidth || height < _airPlaneHeight)
|
||||
{
|
||||
@ -26,8 +25,7 @@ namespace AirBomber
|
||||
_pictureHeight = height;
|
||||
EntityAirPlane = new EntityAirPlane(speed, weight, bodyColor);
|
||||
}
|
||||
protected DrawningAirPlane(int speed, double weight, Color bodyColor, int
|
||||
width, int height, int airPlaneWidth, int airPlaneHeight)
|
||||
protected DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height, int airPlaneWidth, int airPlaneHeight)
|
||||
{
|
||||
if (width < _airPlaneWidth || height < _airPlaneHeight)
|
||||
{
|
||||
|
31
AirBomber/AirBomber/DrawningObjectAirPlane.cs
Normal file
31
AirBomber/AirBomber/DrawningObjectAirPlane.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
public class DrawningObjectAirPlane : IMoveableObject
|
||||
{
|
||||
private readonly DrawningAirPlane? _drawningAirPlane = null;
|
||||
public DrawningObjectAirPlane(DrawningAirPlane drawningAirPlane)
|
||||
{
|
||||
_drawningAirPlane = drawningAirPlane;
|
||||
}
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drawningAirPlane == null || _drawningAirPlane.EntityAirPlane == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningAirPlane.GetPosX, _drawningAirPlane.GetPosY, _drawningAirPlane.GetWidth, _drawningAirPlane.GetHeight);
|
||||
}
|
||||
}
|
||||
public int GetStep => (int)(_drawningAirPlane?.EntityAirPlane?.Step ?? 0);
|
||||
public bool CheckCanMove(DirectionType direction) => _drawningAirPlane?.CanMove(direction) ?? false;
|
||||
public void MoveObject(DirectionType direction) => _drawningAirPlane?.MovePlane(direction);
|
||||
}
|
||||
}
|
@ -6,7 +6,17 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
internal class EntityAirPlane
|
||||
public class EntityAirPlane
|
||||
{
|
||||
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 EntityAirPlane(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
AirBomber/AirBomber/IMoveableObject.cs
Normal file
16
AirBomber/AirBomber/IMoveableObject.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
int GetStep { get; }
|
||||
bool CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
||||
}
|
29
AirBomber/AirBomber/ObjectParameters.cs
Normal file
29
AirBomber/AirBomber/ObjectParameters.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
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
AirBomber/AirBomber/Status.cs
Normal file
15
AirBomber/AirBomber/Status.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AirBomber
|
||||
{
|
||||
public enum Status
|
||||
{
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user