Initial commit
This commit is contained in:
parent
05308d07cb
commit
b1bc08fc79
139
src/Drawnings/DrawningBus.java
Normal file
139
src/Drawnings/DrawningBus.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
10
src/Drawnings/DrawningDoorsCircle.java
Normal file
10
src/Drawnings/DrawningDoorsCircle.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
46
src/Drawnings/DrawningDoorsRect.java
Normal file
46
src/Drawnings/DrawningDoorsRect.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
12
src/Drawnings/DrawningDoorsStar.java
Normal file
12
src/Drawnings/DrawningDoorsStar.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
54
src/Drawnings/DrawningDoubleDeckerBus.java
Normal file
54
src/Drawnings/DrawningDoubleDeckerBus.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
src/Drawnings/IDrawningDoors.java
Normal file
10
src/Drawnings/IDrawningDoors.java
Normal file
@ -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);
|
||||
}
|
20
src/Entities/EntityDoubleDeckerBus.java
Normal file
20
src/Entities/EntityDoubleDeckerBus.java
Normal file
@ -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; }
|
||||
}
|
76
src/MovementStrategy/AbstractStrategy.java
Normal file
76
src/MovementStrategy/AbstractStrategy.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
24
src/MovementStrategy/DrawningObjectBus.java
Normal file
24
src/MovementStrategy/DrawningObjectBus.java
Normal file
@ -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); }
|
||||
}
|
8
src/MovementStrategy/IMoveableObject.java
Normal file
8
src/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(Direction direction);
|
||||
void MoveObject(Direction direction);
|
||||
}
|
53
src/MovementStrategy/MoveToBorder.java
Normal file
53
src/MovementStrategy/MoveToBorder.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
53
src/MovementStrategy/MoveToCenter.java
Normal file
53
src/MovementStrategy/MoveToCenter.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
25
src/MovementStrategy/ObjectParameters.java
Normal file
25
src/MovementStrategy/ObjectParameters.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
7
src/MovementStrategy/Status.java
Normal file
7
src/MovementStrategy/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user