Skip to content

Commit a34105b

Browse files
committed
all city actions added
1 parent ff34909 commit a34105b

File tree

10 files changed

+141
-19
lines changed

10 files changed

+141
-19
lines changed

commands guide.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ The order of flags doesn't matter in each command below that contains dashes
6161

6262
25) city remove citizen from work on **_x_** **_y_**
6363
26) city lock citizen on **_x_** **_y_**
64-
27) city choose production **_unit name_**
65-
28) city purchase unit **_unit name_**
64+
27) city choose production **_unit name_** XXXXXXX
65+
28) city purchase unit **_unit name_** XXXXXXXXXXXX
6666
29) city buy hex **_x_** **_y_**
67-
30) city attack **_x_** **_y_** (choosing building and buying building added in phase 2)
67+
30) city attack **_x_** **_y_** xxxxxxx
68+
(choosing building and buying building added in phase 2) XXXXXXX
6869

6970
#### <li> Map Commands
7071

src/main/java/controller/GameMenuController.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public class GameMenuController {
1919

2020
public String getAvailableBuildingsForCity() {
2121
if (selectedCity == null)
22-
return "no city is selected";
22+
return "error : no city is selected";
2323
if (selectedCity.getOwner() != Game.getGame().getSelectedCivilization())
24-
return "city not yours";
24+
return "error : city not yours";
2525
StringBuilder ans = new StringBuilder();
2626
for (Building availableBuilding : selectedCity.getAvailableBuildings()) {
27-
ans.append("building name : ").append(availableBuilding.name()).append(" production cost: ").append(availableBuilding.productionCost).append("\n");
27+
ans.append(availableBuilding.name).append(" ").append(availableBuilding.productionCost).append("\n");
2828
}
2929
if (ans.length() != 0)
3030
ans.deleteCharAt(ans.length() - 1);
@@ -494,6 +494,8 @@ public String attackTo(int x, int y) {
494494
return Controller.addNotification(Game.getGame().getTurnNumber(), "you can't attack yourself");
495495
((MilitaryUnit) selectedUnit).attackTo(target);
496496

497+
discard(true);
498+
discard(false);
497499
return Controller.addNotification(Game.getGame().getTurnNumber(), "attack is done");
498500
}
499501

@@ -667,7 +669,7 @@ public String removeCitizenFromWork(int x, int y) {
667669
return Controller.addNotification(Game.getGame().getTurnNumber(), "no city is selected");
668670
if (!selectedCity.getOwner().equals(Game.getGame().getSelectedCivilization()))
669671
return Controller.addNotification(Game.getGame().getTurnNumber(), "city not yours");
670-
if (Game.getGame().map.map.get(x).get(y).getCity().equals(selectedCity))
672+
if (!Game.getGame().map.map.get(x).get(y).getCity().equals(selectedCity))
671673
return Controller.addNotification(Game.getGame().getTurnNumber(), "hex not in this city");
672674

673675
if (!Game.getGame().map.map.get(x).get(y).isAnyCitizenWorking())

src/main/java/enums/Building.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public enum Building {
5151

5252
public static Building getBuildingByName(String name) {
5353
for (Building building : Building.values()) {
54-
if (name.equalsIgnoreCase(building.name()))
54+
if (name.equalsIgnoreCase(building.name))
5555
return building;
5656
}
5757
return null;

src/main/java/enums/UnitName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public enum UnitName {
5555
PANZER("panzer", 450, "Armored", 60, 0, 1, 5,
5656
Resource.NULL),
5757
TANK("tank", 450, "Armored", 50, 0, 1, 4, Resource.NULL),
58-
CITYUNIT("cityunit", 0, "city", 14, 14, 2, 0, Resource.NULL);
58+
CITYUNIT("cityunit", 0, "city", 14, 14, 2, 1, Resource.NULL);
5959

6060
private final String name;
6161
private final int cost;

src/main/java/model/City.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,14 @@ public void createUnitInCity(UnitName unitName) {
375375
public void lockCitizenToHex(int x, int y) {
376376

377377
Game.getGame().map.map.get(x).get(y).setAnyCitizenWorking(true);
378-
unemployedCitizens++;
378+
unemployedCitizens--;
379379

380380
}
381381

382382
public void removeCitizenFromHex(int x, int y) {
383383

384384
Game.getGame().map.map.get(x).get(y).setAnyCitizenWorking(false);
385-
unemployedCitizens--;
385+
unemployedCitizens++;
386386
}
387387

388388
public void buyHex(int x, int y) {

src/main/java/model/Civilization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Civilization {
2020
private ArrayList<Resource> openedResources = new ArrayList<>();
2121
private ArrayList<Feature> openedFeatures = new ArrayList<>();
2222
private ArrayList<Improvement> openedImprovements = new ArrayList<>();
23-
private ArrayList<Building> openedBuildings = new ArrayList<>();
23+
private ArrayList<Building> openedBuildings = new ArrayList<>(Arrays.asList(Building.MONUMENT));
2424
private ArrayList<Resource> strategicResources = new ArrayList<>();
2525
private HashSet<Resource> luxuryResources = new HashSet<>();
2626
private City capital;

src/main/java/model/GlobalThings.java

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ public class GlobalThings {
2525

2626
public static Image FOG_OF_WAR_IMAGE = new Image(GlobalThings.class.getResource("/tiles/fog of war.png").toExternalForm());
2727
public static Image RUINS_IMAGE = new Image(GlobalThings.class.getResource("/icons/ruins.png").toExternalForm());
28+
public static Image CITY_IMAGE = new Image(GlobalThings.class.getResource("/tiles/city.png").toExternalForm());
2829
}

src/main/java/model/Map.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,11 @@ private int percentInMiddleY(int y) {
366366

367367
public boolean hasTheCityAround(int x, int y, City city) {
368368
for (NeighborHex neighborHex : NeighborHex.values()) {
369-
if (map.get((2 * x + y % 2 + neighborHex.xDiff) / 2).get(y + neighborHex.yDiff).getCity().equals(city))
370-
return true;
369+
int nx = (2 * x + y % 2 + neighborHex.xDiff) / 2;
370+
int ny = y + neighborHex.yDiff;
371+
if (validCoordinateInArray(nx, ny))
372+
if (city.equals(map.get(nx).get(ny).getCity()))
373+
return true;
371374
}
372375
return false;
373376
}
@@ -383,8 +386,9 @@ public boolean isInACity(Unit unit) {
383386
}
384387
return false;
385388
}
386-
public boolean isCenterOfCity(Hex hex){
387-
return isCenterOfCity(hex.getCoordinatesInArray().get('x'),hex.getCoordinatesInArray().get('y'));
389+
390+
public boolean isCenterOfCity(Hex hex) {
391+
return isCenterOfCity(hex.getCoordinatesInArray().get('x'), hex.getCoordinatesInArray().get('y'));
388392
}
389393

390394
public boolean isCenterOfCity(int x, int y) {

src/main/java/view/GameMenu.java

+117-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class GameMenu extends Menu implements Initializable {
7070
@FXML
7171
private ScrollPane mapScrollPane;
7272

73+
private boolean isSelectingTile = false;
74+
private int codeForFunction = 0;// 1:remove citizen from work 2:lock citizen 3:buy hex
75+
7376
@Override
7477
public void initialize(URL location, ResourceBundle resources) {
7578
mapScrollPane.setContent(map);
@@ -136,6 +139,8 @@ public Group graphicalHex(Hex hex) {
136139
ImageView hexView;
137140
if (hex.getHexVisibility() == HexVisibility.FOG_OF_WAR) {
138141
hexView = new ImageView(GlobalThings.FOG_OF_WAR_IMAGE);
142+
} else if (hex.getCity() != null && hex.getCity().getOwner().getVisibilityMap().isCenterOfCity(hex)) {
143+
hexView = new ImageView(GlobalThings.CITY_IMAGE);
139144
} else if (hex.getFeature() != Feature.NULL) {
140145
hexView = new ImageView(hex.getFeature().image);
141146
} else {
@@ -145,13 +150,21 @@ public Group graphicalHex(Hex hex) {
145150
hexView.setFitHeight(144);
146151

147152
hexView.setOnMouseClicked(event -> {
148-
153+
if (isSelectingTile) {
154+
doFunctionForCode(hex.getCoordinatesInArray().get('x'), hex.getCoordinatesInArray().get('y'));
155+
return;
156+
}
149157
if (event.getButton() == MouseButton.SECONDARY) {
150158
if (controller.getSelectedUnit() != null) {
151159
createPopupAndGlowForNode(controller.attackTo(hex.getCoordinatesInArray().get('x'),
152160
hex.getCoordinatesInArray().get('y')), null, false, true);
153161
fillMap();
162+
} else if (controller.getSelectedCity() != null) {
163+
createPopupAndGlowForNode(controller.cityAttackTo(hex.getCoordinatesInArray().get('x'),
164+
hex.getCoordinatesInArray().get('y')), null, false, true);
165+
fillMap();
154166
}
167+
155168
} else {
156169
if (controller.getSelectedUnit() != null) {
157170
createPopupAndGlowForNode(controller.moveSelectedUnitTo(hex.getCoordinatesInArray().get('x'), hex.getCoordinatesInArray().get('y')), null, false, false);
@@ -179,6 +192,23 @@ public Group graphicalHex(Hex hex) {
179192
return group;
180193
}
181194

195+
private void doFunctionForCode(int x, int y) {
196+
isSelectingTile = false;
197+
switch (codeForFunction) {
198+
case 1:
199+
createPopupAndGlowForNode(controller.removeCitizenFromWork(x, y), null, false, false);
200+
break;
201+
case 2:
202+
createPopupAndGlowForNode(controller.lockCitizenToHex(x, y), null, false, false);
203+
break;
204+
case 3:
205+
createPopupAndGlowForNode(controller.buyHex(x, y), null, false, false);
206+
break;
207+
}
208+
fillMap();
209+
codeForFunction = 0;
210+
}
211+
182212
private void addCity(Hex hex, Group group) {
183213
if (hex.getCity() != null) {
184214
if (hex.getCity().getOwner().getVisibilityMap().isCenterOfCity(hex)) {
@@ -212,7 +242,7 @@ private void addResourceAndRuin(Hex hex, Group group) {
212242

213243
private void createCityPage(City city) {
214244
Popup popup = new Popup();
215-
245+
popupVBox.getChildren().clear();
216246
popupLabel.setText(controller.selectCity(city.getName()));
217247
popupHBox.setVisible(true);
218248
popup.getContent().add(popupHBox);
@@ -223,7 +253,18 @@ private void createCityPage(City city) {
223253
addOptions(false);
224254
popup.show(window);
225255

226-
popup.setOnAutoHide(event -> controller.discard(false));
256+
popup.setOnAutoHide(event ->
257+
new Thread(new Runnable() {
258+
@Override
259+
public void run() {
260+
try {
261+
Thread.sleep(300);
262+
} catch (InterruptedException e) {
263+
e.printStackTrace();
264+
}
265+
controller.discard(false);
266+
}
267+
}).start());
227268
}
228269

229270
private String hexInfo(Hex hex) {
@@ -528,6 +569,79 @@ public void handle(ActionEvent event) {
528569
}
529570
});
530571
popupVBox.getChildren().add(button);
572+
button = new Button("buy building");
573+
button.setOnAction(event -> {
574+
String message = controller.getAvailableBuildingsForCity();
575+
if (message.startsWith("error"))
576+
createPopupAndGlowForNode(message, null, false, false);
577+
else if (message.length() == 0)
578+
createPopupAndGlowForNode("no building is possible now", null, false, false);
579+
else {
580+
String[] buildings = message.split("\n");
581+
popupVBox.getChildren().clear();
582+
for (String building : buildings) {
583+
Button unitButton = new Button(building.split(" ")[0]);
584+
unitButton.setTooltip(new Tooltip(building));
585+
unitButton.setOnAction(event12 -> {
586+
String message1 = controller.buyBuilding(building.split(" ")[0]);
587+
updateAll();
588+
createPopupAndGlowForNode(message1, null, false, false);
589+
});
590+
popupVBox.getChildren().add(unitButton);
591+
}
592+
}
593+
});
594+
popupVBox.getChildren().add(button);
595+
button = new Button("choose building");
596+
button.setOnAction(event -> {
597+
String message = controller.getAvailableBuildingsForCity();
598+
if (message.startsWith("error"))
599+
createPopupAndGlowForNode(message, null, false, false);
600+
else if (message.length() == 0)
601+
createPopupAndGlowForNode("no building is possible now", null, false, false);
602+
else {
603+
String[] buildings = message.split("\n");
604+
popupVBox.getChildren().clear();
605+
for (String building : buildings) {
606+
Button unitButton = new Button(building.split(" ")[0]);
607+
unitButton.setTooltip(new Tooltip(building));
608+
unitButton.setOnAction(event12 -> {
609+
String message1 = controller.buildBuilding(building.split(" ")[0]);
610+
updateAll();
611+
createPopupAndGlowForNode(message1, null, false, false);
612+
});
613+
popupVBox.getChildren().add(unitButton);
614+
}
615+
}
616+
});
617+
popupVBox.getChildren().add(button);
618+
button = new Button("buy hex");
619+
button.setOnAction(new EventHandler<ActionEvent>() {
620+
@Override
621+
public void handle(ActionEvent event) {
622+
isSelectingTile = true;
623+
codeForFunction = 3;
624+
}
625+
});
626+
popupVBox.getChildren().add(button);
627+
button = new Button("lock citizen");
628+
button.setOnAction(new EventHandler<ActionEvent>() {
629+
@Override
630+
public void handle(ActionEvent event) {
631+
isSelectingTile = true;
632+
codeForFunction = 2;
633+
}
634+
});
635+
popupVBox.getChildren().add(button);
636+
button = new Button("remove citizen");
637+
button.setOnAction(new EventHandler<ActionEvent>() {
638+
@Override
639+
public void handle(ActionEvent event) {
640+
isSelectingTile = true;
641+
codeForFunction = 1;
642+
}
643+
});
644+
popupVBox.getChildren().add(button);
531645

532646
}
533647
}

src/main/resources/tiles/city.png

11.6 KB
Loading

0 commit comments

Comments
 (0)