Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Commit

Permalink
push ships when building a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
rpolzer authored and andreas-eberle committed Jun 25, 2018
1 parent e383126 commit 123a911
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,13 +31,15 @@
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;

/**
* 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;
Expand Down Expand Up @@ -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() {
Expand Down
18 changes: 18 additions & 0 deletions jsettlers.logic/src/main/java/jsettlers/logic/movable/Movable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface ILogicMovable extends IScheduledTimerable, IPathCalculatable, I

void leavePosition();

void leavePosition(int direction);

boolean canOccupyBuilding();

void checkPlayerOfPosition(Player playerOfPosition);
Expand Down

0 comments on commit 123a911

Please sign in to comment.