From 123a91193f67e922e67dbff75971523cd8f825e7 Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Tue, 12 Jun 2018 13:52:39 +0200 Subject: [PATCH] push ships when building a new one --- .../buildings/workers/DockyardBuilding.java | 30 +++++++++---------- .../java/jsettlers/logic/movable/Movable.java | 18 +++++++++++ .../movable/interfaces/ILogicMovable.java | 2 ++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/jsettlers.logic/src/main/java/jsettlers/logic/buildings/workers/DockyardBuilding.java b/jsettlers.logic/src/main/java/jsettlers/logic/buildings/workers/DockyardBuilding.java index d428c70d32..3c5e40907f 100644 --- a/jsettlers.logic/src/main/java/jsettlers/logic/buildings/workers/DockyardBuilding.java +++ b/jsettlers.logic/src/main/java/jsettlers/logic/buildings/workers/DockyardBuilding.java @@ -21,6 +21,7 @@ import jsettlers.common.buildings.EBuildingType; import jsettlers.common.buildings.IBuilding; import jsettlers.common.buildings.stacks.RelativeStack; +import jsettlers.common.map.shapes.HexGridArea; import jsettlers.common.movable.EDirection; import jsettlers.common.movable.EShipType; import jsettlers.common.position.ShortPoint2D; @@ -30,6 +31,7 @@ import jsettlers.logic.buildings.stack.IRequestStack; import jsettlers.logic.buildings.stack.RequestStack; import jsettlers.logic.movable.Movable; +import jsettlers.logic.movable.interfaces.ILogicMovable; import jsettlers.logic.objects.ShipInConstructionMapObject; import jsettlers.logic.player.Player; @@ -37,6 +39,7 @@ * An extension to the worker building for dockyards */ public class DockyardBuilding extends WorkerBuilding implements IBuilding.IShipConstruction, IDockBuilding { + private static final int SHIP_PUSH_DISTANCE = 10; private EShipType orderedShipType = null; private ShipInConstructionMapObject ship = null; private DockPosition dockPosition = null; @@ -66,40 +69,37 @@ public void buildShipAction() { if (orderedShipType == null || isDestroyed()) { return; } - + pushExistingShips(); if (ship == null) { - pushShipAtShipPosition(); - // make new ship EDirection direction = dockPosition.getDirection().getNeighbor(-1); ship = new ShipInConstructionMapObject(orderedShipType, direction); grid.getMapObjectsManager().addMapObject(getShipPosition(), ship); } - ship.workOnShip(); - if (ship.isFinished()) { // replace ShipInConstructionMapObject with Movable Movable shipMovable = new Movable(super.grid.getMovableGrid(), orderedShipType.movableType, getShipPosition(), super.getPlayer()); shipMovable.setDirection(ship.getDirection()); removeShipInConstructionMapObject(); - - ship = null; orderedShipType = null; - pushShipAtShipPosition(); } } - private void pushShipAtShipPosition() { + private void pushExistingShips() { if (dockPosition == null) { return; } - - ShortPoint2D shipPosition = getShipPosition(); - Movable existingShip = (Movable) grid.getMovableGrid().getMovableAt(shipPosition.x, getShipPosition().y); - if (existingShip != null) { - existingShip.leavePosition(); - } + int shipX = getShipPosition().x; + int shipY = getShipPosition().y; + HexGridArea.stream(shipX, shipY, 0, SHIP_PUSH_DISTANCE) + .filterBounds(grid .getWidth(), grid.getHeight()) + .forEach((x,y)->{ + ILogicMovable existingShip = grid.getMovableGrid().getMovableAt(x, y); + if (existingShip != null && existingShip.isShip()) { + existingShip.leavePosition(dockPosition.getDirection().ordinal); + } + }); } private ShortPoint2D getShipPosition() { diff --git a/jsettlers.logic/src/main/java/jsettlers/logic/movable/Movable.java b/jsettlers.logic/src/main/java/jsettlers/logic/movable/Movable.java index 484e242fb7..7720a609dd 100644 --- a/jsettlers.logic/src/main/java/jsettlers/logic/movable/Movable.java +++ b/jsettlers.logic/src/main/java/jsettlers/logic/movable/Movable.java @@ -165,6 +165,24 @@ public void leavePosition() { } } + public void leavePosition(int direction) { + if (state != EMovableState.DOING_NOTHING || !enableNothingToDo) { + return; + } + + for (int i = 0; i < EDirection.NUMBER_OF_DIRECTIONS; i++) { + EDirection currDir = EDirection.VALUES[(i + direction) % EDirection.NUMBER_OF_DIRECTIONS]; + if (goInDirection(currDir, EGoInDirectionMode.GO_IF_ALLOWED_AND_FREE)) { + break; + } else { + ILogicMovable movableAtPos = grid.getMovableAt(currDir.getNextTileX(position.x), currDir.getNextTileY(position.y)); + if (movableAtPos != null) { + movableAtPos.push(this); + } + } + } + } + @Override public int timerEvent() { if (state == EMovableState.DEAD) { diff --git a/jsettlers.logic/src/main/java/jsettlers/logic/movable/interfaces/ILogicMovable.java b/jsettlers.logic/src/main/java/jsettlers/logic/movable/interfaces/ILogicMovable.java index 681c94d1aa..b903b13250 100644 --- a/jsettlers.logic/src/main/java/jsettlers/logic/movable/interfaces/ILogicMovable.java +++ b/jsettlers.logic/src/main/java/jsettlers/logic/movable/interfaces/ILogicMovable.java @@ -42,6 +42,8 @@ public interface ILogicMovable extends IScheduledTimerable, IPathCalculatable, I void leavePosition(); + void leavePosition(int direction); + boolean canOccupyBuilding(); void checkPlayerOfPosition(Player playerOfPosition);