From 77f0c3372d8365736ebf01ee0ec4eed43ccd1692 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Mon, 16 Oct 2023 20:27:30 +0400 Subject: [PATCH] Done --- .../DrawningObjects/DrawningLocomotive.java | 63 +++++++++++++ .../DrawningObjects/DrawningMonorail.java | 33 ++----- .../DrawningObjects/DrawningWheels.java | 58 +++++++----- .../DrawningObjects/DrawningWheelsCart.java | 93 +++++++++++++++++++ .../DrawningObjects/DrawningWheelsOrn.java | 89 ++++++++++++++++++ src/MonorailHard/DrawningObjects/IDraw.java | 12 +++ .../Entities/EntityLocomotive.java | 20 ++++ .../DrawningObjectMonorail.java | 36 +++++++ .../MovementStrategy/MoveToBorder.java | 36 +++++++ .../MovementStrategy/MoveToCenter.java | 53 +++++++++++ 10 files changed, 446 insertions(+), 47 deletions(-) create mode 100644 src/MonorailHard/DrawningObjects/DrawningLocomotive.java create mode 100644 src/MonorailHard/DrawningObjects/DrawningWheelsCart.java create mode 100644 src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java create mode 100644 src/MonorailHard/DrawningObjects/IDraw.java create mode 100644 src/MonorailHard/Entities/EntityLocomotive.java create mode 100644 src/MonorailHard/MovementStrategy/DrawningObjectMonorail.java create mode 100644 src/MonorailHard/MovementStrategy/MoveToBorder.java create mode 100644 src/MonorailHard/MovementStrategy/MoveToCenter.java diff --git a/src/MonorailHard/DrawningObjects/DrawningLocomotive.java b/src/MonorailHard/DrawningObjects/DrawningLocomotive.java new file mode 100644 index 0000000..6c3c2ae --- /dev/null +++ b/src/MonorailHard/DrawningObjects/DrawningLocomotive.java @@ -0,0 +1,63 @@ +package MonorailHard.DrawningObjects; + +import MonorailHard.Entities.EntityLocomotive; + +import javax.swing.*; +import java.awt.*; + +public class DrawningLocomotive extends DrawningMonorail{ + public DrawningLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb, + int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor, JPanel monorailPanel){ + super(speed, weight, bodyColor, wheelColor, tireColor, width, height, monorailPanel); + if(EntityMonorail() != null){ + EntityMonorail = new EntityLocomotive(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine, + magniteRail, additionalColor); + } + } + + @Override + public void DrawMonorail(){ + if (!(EntityMonorail instanceof EntityLocomotive)) + { + return; + } + super.DrawMonorail(); + int dif = _monorailWidth / 10; + _monorailWidth -= dif; + EntityLocomotive _locomotive = (EntityLocomotive) EntityMonorail; + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + //вторая кабина + if (_locomotive.SecondCabine()) { + int[] pointsSecondCabineX = {_startPosX + _monorailWidth / 20 * 19, + _startPosX + _monorailWidth + dif, + _startPosX + _monorailWidth + dif, + _startPosX + _monorailWidth / 20 * 19}; + int[] pointsSecondCabineY = {_startPosY + _monorailHeight / 10, + _startPosY + _monorailHeight / 5 * 2, + _startPosY + _monorailHeight / 10 * 7, + _startPosY + _monorailHeight / 10 * 7}; + g2d.setColor(((EntityLocomotive) EntityMonorail).AdditionalColor()); + g2d.fillPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length); + Rectangle Rect = new Rectangle(); + Rect.x = _startPosX + _monorailWidth / 20 * 19; + Rect.y = _startPosY + _monorailHeight / 25 * 4 + _monorailHeight / 50 * 3; + Rect.width = _monorailWidth / 120 * 6; + Rect.height = _monorailHeight / 50 * 7; + g2d.setColor(Color.WHITE); + g2d.fillRect(Rect.x, Rect.y, Rect.width, Rect.height); + g2d.setColor(Color.BLUE); + g2d.drawRect(Rect.x, Rect.y, Rect.width, Rect.height); + } + + _monorailWidth+=dif; + + //магнитная линия + if (_locomotive.MagniteRail()) + { + g2d.setColor(Color.BLACK); + g2d.drawLine(_startPosX, _startPosY + _monorailHeight, _startPosX + _monorailWidth, _startPosY + _monorailHeight); + } + } +} diff --git a/src/MonorailHard/DrawningObjects/DrawningMonorail.java b/src/MonorailHard/DrawningObjects/DrawningMonorail.java index 85f4ed4..ba9d2a3 100644 --- a/src/MonorailHard/DrawningObjects/DrawningMonorail.java +++ b/src/MonorailHard/DrawningObjects/DrawningMonorail.java @@ -19,7 +19,7 @@ public class DrawningMonorail { protected int _startPosY; protected int _monorailWidth = 133; protected int _monorailHeight = 50; - public DrawningWheels DrawningWheels; + protected IDraw DrawningWheels; protected int wheelSz; public EntityMonorail EntityMonorail(){ @@ -39,7 +39,7 @@ public class DrawningMonorail { _pictureHeight = height; EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor); int dif = _monorailWidth / 10; - DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); Random rand = new Random(); DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6)); } @@ -60,8 +60,10 @@ public class DrawningMonorail { _monorailHeight = monorailHeight; EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor); int dif = _monorailWidth / 10; - DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); - DrawningWheels.ChangeWheelsNumb(2); + DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + Random rand = new Random(); + DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6)); + } public void SetPosition(int x, int y){ @@ -127,8 +129,8 @@ public class DrawningMonorail { _startPosY += (int)EntityMonorail.Step(); break; } - DrawningWheels.CurX = _startPosX; - DrawningWheels.CurY = _startPosY; + DrawningWheels.ChangeX(_startPosX); + DrawningWheels.ChangeY(_startPosY); } public void DrawMonorail(){ @@ -181,25 +183,6 @@ public class DrawningMonorail { g2d.setColor(Color.BLACK); g2d.drawPolygon(xPointsArrDoor, yPointsArrDoor, xPointsArrDoor.length); - -//передняя часть тележки - int[] xPointsArrFrontCart = { _startPosX + _monorailWidth / 10 * 4, _startPosX + _monorailWidth / 10 * 2, - _startPosX, _startPosX + _monorailWidth / 10 * 4, - _startPosX + _monorailWidth / 10 * 4}; - int[] yPointsArrFrontCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7, - _startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9, - _startPosY + _monorailHeight / 10 * 7}; - g2d.setColor(Color.BLACK); - g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length); - -//задняя часть тележки - int[] xPointsArrBackCart = {_startPosX + _monorailWidth / 10 * 6, _startPosX + _monorailWidth / 10 * 9, - _startPosX + _monorailWidth, _startPosX + _monorailWidth / 10 * 6}; - int[] yPointsArrBackCart = { _startPosY + _monorailHeight / 10 * 7, _startPosY + _monorailHeight / 10 * 7, - _startPosY + _monorailHeight / 10 * 9, _startPosY + _monorailHeight / 10 * 9}; - - g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); - //левое окно Rectangle leftRect = new Rectangle(); leftRect.x = _startPosX + _monorailWidth / 10 * 2; diff --git a/src/MonorailHard/DrawningObjects/DrawningWheels.java b/src/MonorailHard/DrawningObjects/DrawningWheels.java index 20938f4..460bbaf 100644 --- a/src/MonorailHard/DrawningObjects/DrawningWheels.java +++ b/src/MonorailHard/DrawningObjects/DrawningWheels.java @@ -5,13 +5,13 @@ import MonorailHard.NumberType; import javax.swing.*; import java.awt.*; -public class DrawningWheels { +public class DrawningWheels implements IDraw{ private int WheelSz; JPanel MonorailPanel; private NumberType WheelsNumb; private Color WheelColor, TireColor; private int Width, Height; - public int CurX, CurY; + protected int CurX, CurY; public int WheelSz(){ return WheelSz; } @@ -26,7 +26,12 @@ public class DrawningWheels { WheelSz = Height - Height * 7 / 10; MonorailPanel = monorailPanel; } - + public void ChangeX(int x){ + CurX = x; + } + public void ChangeY(int y){ + CurY = y; + } public void ChangeWheelsNumb(int x){ if(x <= 2) WheelsNumb = NumberType.Two; @@ -40,34 +45,43 @@ public class DrawningWheels { return WheelsNumb; } - public void DrawWheels(){ + public void DrawWheel(int x, int y){ Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); g2d.setColor(WheelColor); - g2d.fillOval( CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz); + g2d.fillOval( x, y , WheelSz, WheelSz); g2d.setColor(TireColor); - g2d.drawOval(CurX + Width / 10, CurY + Height / 10 * 7, WheelSz, WheelSz); - g2d.setColor(WheelColor); - g2d.fillOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz); - g2d.setColor(TireColor); - g2d.drawOval(CurX + Width / 10 * 8, CurY + Height / 10 * 7, WheelSz, WheelSz); + g2d.drawOval(x, y, WheelSz, WheelSz); + } + public void DrawWheels(){ + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + //передняя часть тележки + int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, + CurX, CurX + Width / 10 * 4, + CurX + Width / 10 * 4}; + int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9, + CurY + Height / 10 * 7}; + g2d.setColor(Color.BLACK); + g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length); + +//задняя часть тележки + int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9, + CurX + Width, CurX + Width / 10 * 6}; + int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9}; + + g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); + + DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); //3 колеса if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) - { - g2d.setColor(WheelColor); - g2d.fillOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz); - g2d.setColor(TireColor); - g2d.drawOval(CurX + Width / 10 * 6, CurY + Height / 10 * 7, WheelSz, WheelSz); - } + DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); //4 колеса if (WheelsNumb == NumberType.Four) - { - g2d.setColor(WheelColor); - g2d.fillOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz); - g2d.setColor(TireColor); - g2d.drawOval(CurX + Width / 10 * 3, CurY + Height / 10 * 7, WheelSz, WheelSz); - } + DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); } diff --git a/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java b/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java new file mode 100644 index 0000000..4994f71 --- /dev/null +++ b/src/MonorailHard/DrawningObjects/DrawningWheelsCart.java @@ -0,0 +1,93 @@ +package MonorailHard.DrawningObjects; + +import MonorailHard.NumberType; + +import javax.swing.*; +import java.awt.*; + +public class DrawningWheelsCart implements IDraw{ + private int WheelSz; + JPanel MonorailPanel; + private NumberType WheelsNumb; + private Color WheelColor, TireColor; + private int Width, Height; + public int CurX, CurY; + public int WheelSz(){ + return WheelSz; + } + + public DrawningWheelsCart(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + Width = width; + Height = height; + CurX = curX; + CurY = curY; + WheelColor = wheelColor; + TireColor = tireColor; + WheelSz = Height - Height * 7 / 10; + MonorailPanel = monorailPanel; + } + + public void ChangeWheelsNumb(int x){ + if(x <= 2) + WheelsNumb = NumberType.Two; + if(x == 3) + WheelsNumb = NumberType.Three; + if(x >= 4) + WheelsNumb = NumberType.Four; + } + + public NumberType WheelsNumb(){ + return WheelsNumb; + } + + public void ChangeX(int x){ + CurX = x; + } + public void ChangeY(int y){ + CurY = y; + } + public void DrawWheel(int x, int y){ + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + g2d.setColor(WheelColor); + g2d.fillOval( x, y , WheelSz, WheelSz); + g2d.setColor(TireColor); + g2d.drawOval(x, y, WheelSz, WheelSz); + } + + public void DrawWheels(){ + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + //передняя часть тележки + int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, + CurX, CurX + Width / 10 * 4, + CurX + Width / 10 * 4}; + int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9, + CurY + Height / 10 * 7}; + g2d.setColor(Color.BLACK); + g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length); + g2d.setColor(TireColor); + g2d.drawLine(CurX + Width / 10 * 2, CurY + Height / 10 * 7,CurX + Width / 10 * 4, CurY + Height / 10 * 9); + g2d.drawLine(CurX + Width / 10 * 4, CurY + Height / 10 * 7,CurX + Width / 10 * 2, CurY + Height / 10 * 9); + +//задняя часть тележки + int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9, + CurX + Width, CurX + Width / 10 * 6}; + int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9}; + g2d.setColor(Color.BLACK); + g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); + g2d.setColor(TireColor); + g2d.drawLine(CurX + Width / 10 * 6, CurY + Height / 10 * 7,CurX + Width / 10 * 9, CurY + Height / 10 * 9); + g2d.drawLine(CurX + Width / 10 * 9, CurY + Height / 10 * 7,CurX + Width / 10 * 6, CurY + Height / 10 * 9); + + DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); +//3 колеса + if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) + DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); + +//4 колеса + if (WheelsNumb == NumberType.Four) + DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); + } +} diff --git a/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java b/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java new file mode 100644 index 0000000..bb81b8d --- /dev/null +++ b/src/MonorailHard/DrawningObjects/DrawningWheelsOrn.java @@ -0,0 +1,89 @@ +package MonorailHard.DrawningObjects; + +import MonorailHard.NumberType; + +import javax.swing.*; +import java.awt.*; + +public class DrawningWheelsOrn implements IDraw{ + private int WheelSz; + JPanel MonorailPanel; + private NumberType WheelsNumb; + private Color WheelColor, TireColor; + private int Width, Height; + public int CurX, CurY; + public int WheelSz(){ + return WheelSz; + } + + public DrawningWheelsOrn(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + Width = width; + Height = height; + CurX = curX; + CurY = curY; + WheelColor = wheelColor; + TireColor = tireColor; + WheelSz = Height - Height * 7 / 10; + MonorailPanel = monorailPanel; + } + + public void ChangeWheelsNumb(int x){ + if(x <= 2) + WheelsNumb = NumberType.Two; + if(x == 3) + WheelsNumb = NumberType.Three; + if(x >= 4) + WheelsNumb = NumberType.Four; + } + + public NumberType WheelsNumb(){ + return WheelsNumb; + } + + public void ChangeX(int x){ + CurX = x; + } + public void ChangeY(int y){ + CurY = y; + } + public void DrawWheel(int x, int y){ + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + g2d.setColor(WheelColor); + g2d.fillOval( x, y , WheelSz, WheelSz); + g2d.setColor(TireColor); + g2d.drawOval(x, y, WheelSz, WheelSz); + g2d.drawLine(x, y + WheelSz / 2, x + WheelSz, y + WheelSz / 2); + g2d.drawLine(x + WheelSz / 2, y, x + WheelSz / 2, y + WheelSz); + } + + public void DrawWheels(){ + Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics(); + //передняя часть тележки + int[] xPointsArrFrontCart = { CurX + Width / 10 * 4, CurX + Width / 10 * 2, + CurX, CurX + Width / 10 * 4, + CurX + Width / 10 * 4}; + int[] yPointsArrFrontCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9, + CurY + Height / 10 * 7}; + g2d.setColor(Color.BLACK); + g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length); + +//задняя часть тележки + int[] xPointsArrBackCart = {CurX + Width / 10 * 6, CurX + Width / 10 * 9, + CurX + Width, CurX + Width / 10 * 6}; + int[] yPointsArrBackCart = { CurY + Height / 10 * 7, CurY + Height / 10 * 7, + CurY + Height / 10 * 9, CurY + Height / 10 * 9}; + + g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length); + + DrawWheel(CurX + Width / 10, CurY + Height / 10 * 7); + DrawWheel(CurX + Width / 10 * 8, CurY + Height / 10 * 7); +//3 колеса + if (WheelsNumb == NumberType.Three || WheelsNumb == NumberType.Four) + DrawWheel(CurX + Width / 10 * 6, CurY + Height / 10 * 7); + +//4 колеса + if (WheelsNumb == NumberType.Four) + DrawWheel(CurX + Width / 10 * 3, CurY + Height / 10 * 7); + } +} diff --git a/src/MonorailHard/DrawningObjects/IDraw.java b/src/MonorailHard/DrawningObjects/IDraw.java new file mode 100644 index 0000000..7dfe11a --- /dev/null +++ b/src/MonorailHard/DrawningObjects/IDraw.java @@ -0,0 +1,12 @@ +package MonorailHard.DrawningObjects; + +import MonorailHard.NumberType; + +public interface IDraw { + public void ChangeWheelsNumb(int x); + public NumberType WheelsNumb(); + public void DrawWheels(); + public void DrawWheel(int x, int y); + public void ChangeX(int x); + public void ChangeY(int y); +} diff --git a/src/MonorailHard/Entities/EntityLocomotive.java b/src/MonorailHard/Entities/EntityLocomotive.java new file mode 100644 index 0000000..6d78f78 --- /dev/null +++ b/src/MonorailHard/Entities/EntityLocomotive.java @@ -0,0 +1,20 @@ +package MonorailHard.Entities; + +import java.awt.*; + +public class EntityLocomotive extends EntityMonorail{ + private Color AdditionalColor; + private boolean SecondCabine; + private boolean MagniteRail; + public Color AdditionalColor(){return AdditionalColor;}; + public boolean SecondCabine(){return SecondCabine;}; + public boolean MagniteRail(){return MagniteRail;}; + public EntityLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb, + boolean secondCabine, boolean magniteRail, Color additionalColor) { + super(speed, weight, bodyColor, wheelColor, tireColor); + AdditionalColor = additionalColor; + SecondCabine = secondCabine; + MagniteRail = magniteRail; + } + +} diff --git a/src/MonorailHard/MovementStrategy/DrawningObjectMonorail.java b/src/MonorailHard/MovementStrategy/DrawningObjectMonorail.java new file mode 100644 index 0000000..597ac76 --- /dev/null +++ b/src/MonorailHard/MovementStrategy/DrawningObjectMonorail.java @@ -0,0 +1,36 @@ +package MonorailHard.MovementStrategy; + +import MonorailHard.DirectionType; +import MonorailHard.DrawningObjects.DrawningMonorail; + +public class DrawningObjectMonorail implements IMoveableObject{ + private final DrawningMonorail _drawningMonorail; + public DrawningObjectMonorail(DrawningMonorail drawningMonorail){ + _drawningMonorail = drawningMonorail; + } + + public ObjectParameters GetObjectParameters(){ + if(_drawningMonorail == null || _drawningMonorail.EntityMonorail() == null) + return null; + return new ObjectParameters(_drawningMonorail.GetPosX(), _drawningMonorail.GetPosY(), + _drawningMonorail.GetWidth(), _drawningMonorail.GetHeight()); + } + + public int GetStep(){ + if(_drawningMonorail.EntityMonorail() == null) + return 0; + return (int)_drawningMonorail.EntityMonorail().Step(); + } + + public boolean CheckCanMove(DirectionType direction){ + if(_drawningMonorail == null) + return false; + return _drawningMonorail.CanMove(direction); + } + + public void MoveObject(DirectionType direction){ + if(_drawningMonorail == null) + return; + _drawningMonorail.MoveTransport(direction); + } +} diff --git a/src/MonorailHard/MovementStrategy/MoveToBorder.java b/src/MonorailHard/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..78c0bb0 --- /dev/null +++ b/src/MonorailHard/MovementStrategy/MoveToBorder.java @@ -0,0 +1,36 @@ +package MonorailHard.MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + int a = FieldWidth(); + int b = FieldHeight(); + int q = GetStep(); + 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 = 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(); + } + } + } +} + diff --git a/src/MonorailHard/MovementStrategy/MoveToCenter.java b/src/MonorailHard/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..a152edc --- /dev/null +++ b/src/MonorailHard/MovementStrategy/MoveToCenter.java @@ -0,0 +1,53 @@ +package MonorailHard.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.ObjectMiddleHorizontal >= FieldWidth() / 2 && + objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) && + + ((objParams.ObjectMiddleVertical <= FieldHeight() / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 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(); + } + } + } +}