Skip to content

Commit

Permalink
Properly update the tiles' room IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonihele committed Jan 12, 2025
1 parent e65ab94 commit c3cb735
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/toniarts/openkeeper/game/component/MapTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package toniarts.openkeeper.game.component;

import com.simsilica.es.EntityComponent;
import com.simsilica.es.EntityId;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;

import com.simsilica.es.EntityId;
import toniarts.openkeeper.tools.convert.map.Tile;

/**
Expand Down Expand Up @@ -63,6 +62,7 @@ public MapTile(MapTile mapTile) {
this.bridgeTerrainType = mapTile.bridgeTerrainType;
this.p = mapTile.p;
this.index = mapTile.index;
this.room = mapTile.room;

if (mapTile.selection != null && !mapTile.selection.isEmpty()) {
this.selection = HashMap.newHashMap(4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public void build(Vector2f start, Vector2f end, short playerId, short roomId) {
substractGoldCapacityFromPlayer(firstInstance); // Important to update the gold here
firstInstance.addCoordinates(instancePlots);
for (Point p : instancePlots) {
mapController.getRoomCoordinates().put(p, firstInstance);
mapController.addRoomCoordinate(firstInstance, p);
}

// Update the merged room
Expand All @@ -416,7 +416,7 @@ public void build(Vector2f start, Vector2f end, short playerId, short roomId) {
updatableTiles.addAll(Arrays.asList(WorldUtils.getSurroundingTiles(mapController.getMapData(), p, true)));
if (!firstInstance.equals(instance)) {
firstInstance.addCoordinate(p);
mapController.getRoomCoordinates().put(p, firstInstance);
mapController.addRoomCoordinate(firstInstance, p);
}
}
}
Expand All @@ -425,7 +425,7 @@ public void build(Vector2f start, Vector2f end, short playerId, short roomId) {
}

// Update
mapController.updateRooms(updatableTiles.toArray(new Point[0]));
mapController.updateRooms(updatableTiles.toArray(Point[]::new));

// New room, calculate gold capacity
RoomInstance instance = mapController.getRoomCoordinates().get(instancePlots.get(0));
Expand Down
8 changes: 8 additions & 0 deletions src/toniarts/openkeeper/game/controller/IMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,12 @@ public interface IMapController extends IMapInformation<IMapTileController>, IGa
*/
public int getPlayerSkeletonCapacity(short playerId);

/**
* Add a coordinate to a room
*
* @param roomInstance roomInstance to add
* @param point coordinate to add
*/
public void addRoomCoordinate(RoomInstance roomInstance, Point point);

}
14 changes: 9 additions & 5 deletions src/toniarts/openkeeper/game/controller/MapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,10 @@ public void removeRoomInstances(RoomInstance... instances) {
roomController.remove();

roomControllers.remove(instance);
for (Point p : instance.getCoordinates()) {
roomCoordinates.remove(p);
}

// Remove room from the tiles
for (Point roomCoordinate : instance.getCoordinates()) {
roomCoordinates.remove(roomCoordinate);

// Remove room from the tile
IMapTileController roomTile = mapData.getTile(roomCoordinate);
if (roomTile.getRoomId().equals(roomController.getEntityId())) {
roomTile.setRoomId(null);
Expand Down Expand Up @@ -831,4 +829,10 @@ public int getPlayerSkeletonCapacity(short playerId) {
public boolean isSolid(Point p) {
return mapInformation.isSolid(p);
}

@Override
public void addRoomCoordinate(RoomInstance roomInstance, Point point) {
roomCoordinates.put(point, roomInstance);
getMapData().getTile(point).setRoomId(getRoomController(roomInstance).getEntityId());
}
}

0 comments on commit c3cb735

Please sign in to comment.