Skip to content

Commit

Permalink
roadView now asks the controller for the state
Browse files Browse the repository at this point in the history
  • Loading branch information
davi-bart committed Feb 16, 2024
1 parent cc7552d commit db9b2a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/main/java/it/unibo/view/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ private void redrawTile(final TilePosition coords) {
private List<Line> drawRoads() {
final List<Line> roads = new ArrayList<>();
this.controller.getBoardController().getAllRoadPositions().forEach(pos -> roads
.add(new RoadView(controller, pos, () -> playerColors.get(controller.getCurrentPlayerName()))));
.add(new RoadView(controller, pos, (p) -> {
Optional<String> playerName = controller.getBoardController().getRoadOwner(p);
return playerName.isPresent() ? playerColors.get(playerName.get()) : Color.LIGHTGRAY;
})));
return roads;
}

Expand Down
33 changes: 14 additions & 19 deletions src/main/java/it/unibo/view/RoadView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibo.view;

import java.util.function.Supplier;
import java.util.Optional;
import java.util.function.Function;

import org.apache.commons.lang3.tuple.Pair;

Expand All @@ -16,41 +17,36 @@
public final class RoadView extends Line {
private final MainController controller;
private final RoadPosition roadPosition;
private Color currentColor;
private final Supplier<Color> getCurrentColor;
private boolean built;
private final Function<RoadPosition, Color> getRoadColor;

/**
* Constructor.
*
* @param controller the main controller
* @param roadPosition the position of the property
* @param getCurrentColor the supplier of the current color
* @param controller the main controller
* @param roadPosition the position of the road
* @param getRoadColor get the color of the property
*/
public RoadView(final MainController controller, final RoadPosition roadPosition,
final Supplier<Color> getCurrentColor) {
final Function<RoadPosition, Color> getRoadColor) {
this.controller = controller;
this.roadPosition = roadPosition;
this.currentColor = Color.LIGHTGRAY;
this.getCurrentColor = getCurrentColor;
built = false;
this.getRoadColor = getRoadColor;
draw();
}

private void draw() {
setLine(roadPosition);
if (!built) {
super.setStroke(currentColor);
Optional<String> roadOwner = controller.getBoardController().getRoadOwner(roadPosition);
if (!roadOwner.isPresent()) {
super.setStroke(getRoadColor.apply(roadPosition));
super.setEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
if (controller.canBuildRoad(roadPosition)) {
this.currentColor = getCurrentColor.get();
controller.buildRoad(roadPosition);
built = true;
draw();
}
});
} else {
super.setStroke(currentColor);
super.setStroke(getRoadColor.apply(roadPosition));
super.setEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
});
}
Expand All @@ -59,9 +55,8 @@ private void draw() {
private void setLine(final RoadPosition position) {
final Pair<Double, Double> pos = Utility.getPositionFromTile(position.getCoordinates().getRow(),
position.getCoordinates().getCol());
final var endpoints = Utility
.getRoadCoordinates(Utility.HEXAGON_RADIUS * (2 - Math.sqrt(3) / 2), pos.getLeft(), pos.getRight(),
position.getDirection());
final var endpoints = Utility.getRoadCoordinates(Utility.HEXAGON_RADIUS * (2 - Math.sqrt(3) / 2), pos.getLeft(),
pos.getRight(), position.getDirection());
super.setStartX(endpoints.getLeft().getLeft());
super.setStartY(endpoints.getLeft().getRight());
super.setEndX(endpoints.getRight().getLeft());
Expand Down

0 comments on commit db9b2a2

Please sign in to comment.