From b1bc08fc7974c40b8d6cac60ed41ebc0de1705e8 Mon Sep 17 00:00:00 2001 From: MayDayR Date: Fri, 15 Dec 2023 20:07:38 +0300 Subject: [PATCH] Initial commit --- src/Drawnings/DrawningBus.java | 139 ++++++++++++++++++++ src/Drawnings/DrawningDoorsCircle.java | 10 ++ src/Drawnings/DrawningDoorsRect.java | 46 +++++++ src/Drawnings/DrawningDoorsStar.java | 12 ++ src/Drawnings/DrawningDoubleDeckerBus.java | 54 ++++++++ src/Drawnings/IDrawningDoors.java | 10 ++ src/Entities/EntityDoubleDeckerBus.java | 20 +++ src/MovementStrategy/AbstractStrategy.java | 76 +++++++++++ src/MovementStrategy/DrawningObjectBus.java | 24 ++++ src/MovementStrategy/IMoveableObject.java | 8 ++ src/MovementStrategy/MoveToBorder.java | 53 ++++++++ src/MovementStrategy/MoveToCenter.java | 53 ++++++++ src/MovementStrategy/ObjectParameters.java | 25 ++++ src/MovementStrategy/Status.java | 7 + 14 files changed, 537 insertions(+) create mode 100644 src/Drawnings/DrawningBus.java create mode 100644 src/Drawnings/DrawningDoorsCircle.java create mode 100644 src/Drawnings/DrawningDoorsRect.java create mode 100644 src/Drawnings/DrawningDoorsStar.java create mode 100644 src/Drawnings/DrawningDoubleDeckerBus.java create mode 100644 src/Drawnings/IDrawningDoors.java create mode 100644 src/Entities/EntityDoubleDeckerBus.java create mode 100644 src/MovementStrategy/AbstractStrategy.java create mode 100644 src/MovementStrategy/DrawningObjectBus.java create mode 100644 src/MovementStrategy/IMoveableObject.java create mode 100644 src/MovementStrategy/MoveToBorder.java create mode 100644 src/MovementStrategy/MoveToCenter.java create mode 100644 src/MovementStrategy/ObjectParameters.java create mode 100644 src/MovementStrategy/Status.java diff --git a/src/Drawnings/DrawningBus.java b/src/Drawnings/DrawningBus.java new file mode 100644 index 0000000..b9e9d36 --- /dev/null +++ b/src/Drawnings/DrawningBus.java @@ -0,0 +1,139 @@ +package Drawnings; + +import java.awt.*; +import java.util.Random; +import Entities.*; +import MovementStrategy.*; + +public class DrawningBus { + + public EntityBus entityBus; + public IDrawningDoors _doors; + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + private int _busWidth = 185; + private int _busHeight = 115; + + public int GetPosX() { return _startPosX; } + public int GetPosY() { return _startPosY; } + public int GetWidth() { return _busWidth; } + public int GetHeight() { return _busHeight; } + + public DrawningBus(int speed, float weight, Color bodyColor, int countDoors, int width, int height) { + if (width < _busHeight || height < _busWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityBus = new EntityBus(speed, weight, bodyColor); + + Random random = new Random(); + switch (random.nextInt(0,3)) { + case 0: + _doors = new DrawningDoorsRect(); + break; + case 1: + _doors = new DrawningDoorsStar(); + break; + case 2: + _doors = new DrawningDoorsCircle(); + break; + default: + _doors = new DrawningDoorsRect(); + break; + } + _doors.SetCount(countDoors); + } + + public void SetPosition (int x, int y) { + if (x + _busWidth > _pictureWidth || y + _busHeight > _pictureHeight) { + _startPosX = _pictureWidth - _busWidth; + _startPosY = _pictureHeight - _busHeight; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + + public boolean CanMove(Direction direction) + { + if (entityBus == null) + { + return false; + } + switch (direction) + { + case Left: + return _startPosX - entityBus.Step > 0; + case Right: + return _startPosX + _busWidth + entityBus.Step < _pictureWidth; + case Up: + return _startPosY - entityBus.Step > 0; + case Down: + return _startPosY + _busHeight + entityBus.Step < _pictureHeight; + default: + return false; + } + } + + public void MoveTransport(Direction direction){ + if (entityBus == null) { + return; + } + + switch (direction) { + case Left: + _startPosX -= entityBus.Step; + break; + case Right: + _startPosX += entityBus.Step; + break; + case Up: + _startPosY -= entityBus.Step; + break; + case Down: + _startPosY += entityBus.Step; + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (entityBus == null) { + return; + } + //тело + g.setColor(entityBus.getBodyColor()); + g.fillRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.fillRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.fillRect(_startPosX + 10, _startPosY + 52, 137, 46); + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 147, _startPosY + 52, 21, 20); + g.drawRect(_startPosX + 147, _startPosY + 71, 30, 27); + g.drawRect(_startPosX + 10, _startPosY + 52, 137, 46); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 150, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 55, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 55, 15, 15); + + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); + g.drawLine(_startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); + + //колёса + g.fillOval(_startPosX + 23, _startPosY + 88, 25, 25); + g.fillOval(_startPosX + 123, _startPosY + 88, 25, 25); + g.setColor(Color.gray); + g.fillOval(_startPosX + 25, _startPosY + 90, 21, 21); + g.fillOval(_startPosX + 125, _startPosY + 90, 21, 21); + + // двери + _doors.Draw(g, _startPosX, _startPosY); + } +} diff --git a/src/Drawnings/DrawningDoorsCircle.java b/src/Drawnings/DrawningDoorsCircle.java new file mode 100644 index 0000000..bc03cf1 --- /dev/null +++ b/src/Drawnings/DrawningDoorsCircle.java @@ -0,0 +1,10 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningDoorsCircle extends DrawningDoorsRect { + protected void drawDoor(Graphics2D g, int posX, int posY){ + g.setColor(Color.gray); + g.fillOval(posX, posY, 12, 20); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoorsRect.java b/src/Drawnings/DrawningDoorsRect.java new file mode 100644 index 0000000..25462f9 --- /dev/null +++ b/src/Drawnings/DrawningDoorsRect.java @@ -0,0 +1,46 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawningDoorsRect implements IDrawningDoors { + private CountDoors _wheel; + + public CountDoors getCount() + { + return _wheel; + } + public void SetCount (int count) { + switch (count) { + case 2: + _wheel = CountDoors.Three; + break; + case 3: + _wheel = CountDoors.Four; + break; + case 4: + _wheel = CountDoors.Five; + break; + default: + _wheel = CountDoors.Three; + break; + } + } + protected void drawDoor(Graphics2D g, int posX, int posY){ + g.setColor(Color.gray); + g.fillRect(posX,posY, 12, 20); + } + public void Draw (Graphics2D g, int _startPosX, int _startPosY) { + drawDoor(g,_startPosX + 40, _startPosY + 75); + drawDoor(g, _startPosX + 60, _startPosY + 75); + drawDoor(g, _startPosX + 80, _startPosY + 75); + if (_wheel == CountDoors.Three) { + return; + } + drawDoor(g, _startPosX + 100, _startPosY + 75); + if (_wheel == CountDoors.Four) { + return; + } + drawDoor(g, _startPosX + 120, _startPosY + 75); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoorsStar.java b/src/Drawnings/DrawningDoorsStar.java new file mode 100644 index 0000000..4038828 --- /dev/null +++ b/src/Drawnings/DrawningDoorsStar.java @@ -0,0 +1,12 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningDoorsStar extends DrawningDoorsRect { + protected void drawDoor(Graphics2D g, int posX, int posY) { + int[] StarX = {posX + 6, posX + 8, posX + 12, posX + 8, posX + 6, posX + 4, posX, posX + 4}; + int[] StarY = {posY, posY + 6, posY + 10, posY + 14, posY + 20, posY + 14, posY + 10, posY + 6}; + g.setColor(Color.gray); + g.fillPolygon(StarX, StarY, StarX.length); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawningDoubleDeckerBus.java b/src/Drawnings/DrawningDoubleDeckerBus.java new file mode 100644 index 0000000..892ce5d --- /dev/null +++ b/src/Drawnings/DrawningDoubleDeckerBus.java @@ -0,0 +1,54 @@ +package Drawnings; + +import Entities.EntityDoubleDeckerBus; + +import java.awt.*; + +public class DrawningDoubleDeckerBus extends DrawningBus { + + public DrawningDoubleDeckerBus(int speed, float weight, Color bodyColor, int countWheels, Color additionalColor, boolean isSecondFloor, boolean isStairs, int width, int height) + { + super(speed, weight, bodyColor, countWheels, width, height); + if (entityBus != null) { + entityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, isSecondFloor, isStairs); + } + } + @Override + public void DrawTransport(Graphics2D g) { + + if (entityBus == null) + { + return; + } + super.DrawTransport(g); + + Color additionalColor = ((EntityDoubleDeckerBus) entityBus).getAdditionalColor(); + // 2 этаж + if (((EntityDoubleDeckerBus) entityBus).IsSecondFloor()) { + g.setColor(additionalColor); + g.fillRect(_startPosX + 7, _startPosY + 12, 172, 40); //большой прямоугольник + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 7, _startPosY + 12, 172, 40); + g.drawLine(_startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); + + g.setColor(Color.blue); + g.fillRect(_startPosX + 15, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 42, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 69, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 96, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 123, _startPosY + 15, 15, 15); + g.fillRect(_startPosX + 150, _startPosY + 15, 15, 15); + } + // лестница + if (((EntityDoubleDeckerBus) entityBus).IsStairs()) { + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); + g.drawLine(_startPosX + 10, _startPosY + 58, _startPosX + 34, _startPosY + 58); + g.drawLine(_startPosX + 10, _startPosY + 64, _startPosX + 34, _startPosY + 64); + g.drawLine(_startPosX + 10, _startPosY + 72, _startPosX + 34, _startPosY + 72); + g.drawLine(_startPosX + 10, _startPosY + 80, _startPosX + 34, _startPosY + 80); + g.drawLine(_startPosX + 10, _startPosY + 88, _startPosX + 34, _startPosY + 88); + g.drawLine(_startPosX + 10, _startPosY + 94, _startPosX + 34, _startPosY + 94); + } + } +} diff --git a/src/Drawnings/IDrawningDoors.java b/src/Drawnings/IDrawningDoors.java new file mode 100644 index 0000000..4564d6e --- /dev/null +++ b/src/Drawnings/IDrawningDoors.java @@ -0,0 +1,10 @@ +package Drawnings; + +import java.awt.*; +import Entities.CountDoors; + +public interface IDrawningDoors { + public CountDoors getCount(); + public void SetCount (int count); + public void Draw (Graphics2D g, int _startPosX, int _startPoxY); +} \ No newline at end of file diff --git a/src/Entities/EntityDoubleDeckerBus.java b/src/Entities/EntityDoubleDeckerBus.java new file mode 100644 index 0000000..85744a2 --- /dev/null +++ b/src/Entities/EntityDoubleDeckerBus.java @@ -0,0 +1,20 @@ +package Entities; + +import java.awt.*; + +public class EntityDoubleDeckerBus extends EntityBus { + private Color AdditionalColor; + private boolean IsSecondFloor; + private boolean IsStairs; + + public EntityDoubleDeckerBus(int speed, float weight, Color bodyColor, Color additionalColor, boolean isSecondFloor, boolean isStairs) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + IsSecondFloor = isSecondFloor; + IsStairs = isStairs; + } + + public Color getAdditionalColor() { return AdditionalColor; } + public boolean IsSecondFloor() { return IsSecondFloor; } + public boolean IsStairs() { return IsStairs; } +} diff --git a/src/MovementStrategy/AbstractStrategy.java b/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..742c4df --- /dev/null +++ b/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,76 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private Status _state = Status.NotInit; + protected int FieldWidth; + protected int FieldHeight; + 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 boolean MoveLeft() { return MoveTo(Direction.Left); } + protected boolean MoveRight() { return MoveTo(Direction.Right); } + protected boolean MoveUp() { return MoveTo(Direction.Up); } + protected boolean MoveDown() { return MoveTo(Direction.Down); } + + // параметры + protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); } + // шаг + protected int GetStep() + { + if (_state != Status.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + // перемещение + protected abstract void MoveToTarget(); + + // достигнута ли цель + protected abstract boolean IsTargetDestination(); + + // попытка перемещения по направлению + private boolean MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/MovementStrategy/DrawningObjectBus.java b/src/MovementStrategy/DrawningObjectBus.java new file mode 100644 index 0000000..2858468 --- /dev/null +++ b/src/MovementStrategy/DrawningObjectBus.java @@ -0,0 +1,24 @@ +package MovementStrategy; +import Drawnings.*; + +public class DrawningObjectBus implements IMoveableObject { + private DrawningBus _drawningBus = null; + + public DrawningObjectBus(DrawningBus drawningBus) + { + _drawningBus = drawningBus; + } + + public ObjectParameters GetObjectPosition() + { + if (_drawningBus == null || _drawningBus.entityBus == null) + { + return null; + } + return new ObjectParameters(_drawningBus.GetPosX(), _drawningBus.GetPosY(), _drawningBus.GetWidth(), _drawningBus.GetHeight()); + } + + public int GetStep() { return (int) _drawningBus.entityBus.Step; } + public boolean CheckCanMove(Direction direction) { return _drawningBus.CanMove(direction); } + public void MoveObject(Direction direction) { _drawningBus.MoveTransport(direction); } +} diff --git a/src/MovementStrategy/IMoveableObject.java b/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ffd427e --- /dev/null +++ b/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(Direction direction); + void MoveObject(Direction direction); +} diff --git a/src/MovementStrategy/MoveToBorder.java b/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..08e2b44 --- /dev/null +++ b/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,53 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder() <= FieldWidth && + objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && + objParams.DownBorder() + GetStep() >= FieldHeight; + } + + // движение к цели + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX < 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + var diffY = FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY < 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/src/MovementStrategy/MoveToCenter.java b/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..1b278cb --- /dev/null +++ b/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,53 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + @Override + protected boolean 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; + } + + // движение к цели + @Override + protected 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(); + } + } + } +} diff --git a/src/MovementStrategy/ObjectParameters.java b/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..a741f5a --- /dev/null +++ b/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,25 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + public int LeftBorder() { return _x; } + public int TopBorder() { return _y; } + public int RightBorder() { return _x + _width; } + public int DownBorder() { return _y + _height; } + + + public int ObjectMiddleHorizontal () { return _x + _width / 2; } + public int ObjectMiddleVertical () { return _y + _height / 2; } + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/src/MovementStrategy/Status.java b/src/MovementStrategy/Status.java new file mode 100644 index 0000000..586b538 --- /dev/null +++ b/src/MovementStrategy/Status.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum Status { + NotInit, + InProgress, + Finish +}