Skip to content

Commit

Permalink
BUGFIX :: Infected city observer
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmethalac committed Dec 20, 2020
1 parent 7da8868 commit 40d2b8c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/controllers/observers/InfectedCityObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.scene.layout.VBox;
import javafx.scene.shape.MeshView;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;
import models.City;
Expand All @@ -19,19 +23,28 @@ public class InfectedCityObserver extends Observer {
private ArrayList<MeshView> viruses = new ArrayList<>();
private int[] coordinates;
private Group sceneItems;
private VBox gameLog;
private boolean prevInfected;

public InfectedCityObserver(City city, int[] coordinates, Group sceneItems){
public InfectedCityObserver(City city, int[] coordinates, Group sceneItems, VBox gameLog){
this.subject = city;
this.subject.attach(this);
this.coordinates = coordinates;
this.sceneItems = sceneItems;
this.gameLog = gameLog;
this.prevInfected = false;
}

@Override
public void update() {
if ( sceneItems != null) {
if (((City) subject).isInfected()) {
if (viruses.isEmpty()) {
if (((City) subject).isInfected() && !prevInfected) {
Text text = new Text(((City) subject).getName() + " is infected!");
text.setFont(Font.font("Arial", FontWeight.BOLD, 20));
if ( gameLog.getChildren().size() > 5) {
gameLog.getChildren().subList(0, gameLog.getChildren().size() - 5).clear();
}
gameLog.getChildren().add(text);
int angle = 0;
while ( angle <= 360 ){
MeshView virus = MeshImporter.getVirus();
Expand All @@ -56,10 +69,18 @@ public void update() {
viruses.add(virus);
angle += 90;
}
}
} else {
prevInfected = true;
}
if ( !((City) subject).isInfected() && prevInfected){
sceneItems.getChildren().removeAll(viruses);
viruses.clear();
Text text = new Text(((City) subject).getName() + " is disinfected from virus!");
text.setFont(Font.font("Arial", FontWeight.BOLD, 20));
if ( gameLog.getChildren().size() > 5) {
gameLog.getChildren().subList(0, gameLog.getChildren().size() - 5).clear();
}
gameLog.getChildren().add(text);
prevInfected = false;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/scenecontrollers/GameSceneController.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,8 @@ public static void handlePirateRegionPopup(){
e.printStackTrace();
}
}

public VBox getGameLog() {
return gameLog;
}
}
2 changes: 1 addition & 1 deletion src/controllers/scenecontrollers/TableController.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void initializeRegions() {
}
new ColorObserver(regions.get(i), group);
new BuildingObserver(regions.get(i), group);
new InfectedCityObserver((City) regions.get(i), coordinates.get(i), sceneItems);
new InfectedCityObserver((City) regions.get(i), coordinates.get(i), sceneItems, gameSceneController.getGameLog());
} else if ( regions.get(i) instanceof ChanceRegion){
region.setMaterial(new PhongMaterial(Color.BLACK));
group.add(getQuestionMark(coordinates.get(i)[0], coordinates.get(i)[1]));
Expand Down

0 comments on commit 40d2b8c

Please sign in to comment.