Создание классов, дополнения классов (как во второй базовой)

This commit is contained in:
malimova 2023-11-26 16:39:03 +04:00
parent dd3c707953
commit 64b0091916
7 changed files with 174 additions and 5 deletions

View 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;
}
}
}

View File

@ -15,8 +15,7 @@ namespace AirBomber
protected int _startPosY; protected int _startPosY;
protected readonly int _airPlaneWidth = 150; protected readonly int _airPlaneWidth = 150;
protected readonly int _airPlaneHeight = 118; protected readonly int _airPlaneHeight = 118;
public DrawningAirPlane(int speed, double weight, Color bodyColor, int public DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height)
width, int height)
{ {
if (width < _airPlaneWidth || height < _airPlaneHeight) if (width < _airPlaneWidth || height < _airPlaneHeight)
{ {
@ -26,8 +25,7 @@ namespace AirBomber
_pictureHeight = height; _pictureHeight = height;
EntityAirPlane = new EntityAirPlane(speed, weight, bodyColor); EntityAirPlane = new EntityAirPlane(speed, weight, bodyColor);
} }
protected DrawningAirPlane(int speed, double weight, Color bodyColor, int protected DrawningAirPlane(int speed, double weight, Color bodyColor, int width, int height, int airPlaneWidth, int airPlaneHeight)
width, int height, int airPlaneWidth, int airPlaneHeight)
{ {
if (width < _airPlaneWidth || height < _airPlaneHeight) if (width < _airPlaneWidth || height < _airPlaneHeight)
{ {

View 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);
}
}

View File

@ -6,7 +6,17 @@ using System.Threading.Tasks;
namespace AirBomber 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;
}
} }
} }

View 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);
}
}

View 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;
}
}
}

View 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
}
}