Skip to content
Browse files

Attacking foe colony visualized by red beam

  • Loading branch information...
1 parent 6aa756e commit 3d3ec70d7b20d9fcc405cb5180c2da617e886cfe @stevenschwenke committed Apr 9, 2013
View
13 src/main/java/de/stevenschwenke/java/javafx/simFX/business/UserInterface.java
@@ -41,7 +41,18 @@
* @param harvest
* Resource that gets harvested
*/
- public void notifyHarvest(Colony harvester, Resource harvest);
+ public void notifyHarvest(final Colony harvester, final Resource harvest);
+
+ /**
+ * Notifies the user interface of an attack between two {@link Colony}s.
+ *
+ * @param attacker
+ * @param attacked
+ * @param strength
+ * of the attack
+ */
+ public void notifyAttack(final Colony attacker, final Colony attacked,
+ long strength);
/**
* A change in the game state, for example when the player won, has to be
View
15 src/main/java/de/stevenschwenke/java/javafx/simFX/business/worldObjects/Colony.java
@@ -113,13 +113,18 @@ public void attack(Colony attackedColony) {
// clean.
}
- long energyAttacked = attackedColony.getAvailableEnergy();
+ long energyOfAttacked = attackedColony.getAvailableEnergy();
- if (energyAttacked > availableEnergy) {
- attackedColony.setAvailableEnergy(energyAttacked - availableEnergy);
+ if (energyOfAttacked > availableEnergy) {
+ attackedColony.setAvailableEnergy(energyOfAttacked
+ - availableEnergy);
+ World.getInstance().notifyAttack(this, attackedColony,
+ availableEnergy);
setAvailableEnergy(0);
- } else if (energyAttacked < availableEnergy) {
- setAvailableEnergy(availableEnergy - energyAttacked);
+ } else if (energyOfAttacked <= availableEnergy) {
+ setAvailableEnergy(availableEnergy - energyOfAttacked);
+ World.getInstance().notifyAttack(this, attackedColony,
+ energyOfAttacked);
attackedColony.destroy();
GameStateManager.getInstance().checkGameState();
}
View
13 src/main/java/de/stevenschwenke/java/javafx/simFX/business/worldObjects/World.java
@@ -127,6 +127,19 @@ public void notifyHarvest(Colony harvester, Resource harvest) {
}
/**
+ * Notifies the user interface of an attack between two {@link Colony}s.
+ *
+ * @param attacker
+ * @param attacked
+ * @param strength
+ * of the attack
+ */
+ public void notifyAttack(final Colony attacker, final Colony attacked,
+ long strength) {
+ userInterface.notifyAttack(attacker, attacked, strength);
+ }
+
+ /**
* @return all {@link Resource}s on the map
*/
public Set<Resource> getAllExistingResources() {
View
62 src/main/java/de/stevenschwenke/java/javafx/simFX/ui/javaFX/JavaFxApplication.java
@@ -175,7 +175,8 @@ public void run() {
line.setFill(null);
- line.setStroke(Color.RED);
+ line.setStroke(new Color(255 / 255, (double) (110d / 255d),
+ (double) (3d / 255d), 1));
line.setStrokeWidth(2);
@@ -249,4 +250,63 @@ public void notifyChangeInGameState(GameState newState) {
}
+ @Override
+ public void notifyAttack(final Colony attacker, final Colony attacked,
+ final long strength) {
+ Platform.runLater(new Runnable() {
+ @Override
+ public void run() {
+
+ final Line line = new Line(attacker.getPosition().getX(),
+ attacker.getPosition().getY(), attacked.getPosition()
+ .getX(), attacked.getPosition().getY());
+
+ line.setFill(null);
+
+ line.setStroke(Color.RED);
+
+ line.setStrokeWidth(2);
+
+ synchronized (rootGroup) {
+ rootGroup.getChildren().add(line);
+
+ FadeTransition ft = new FadeTransition(Duration
+ .millis(1300), line);
+ ft.setFromValue(1.0);
+ ft.setToValue(0);
+ ft.play();
+
+ // remove the line after the animation
+ ft.setOnFinished(new EventHandler<ActionEvent>() {
+ @Override
+ public void handle(ActionEvent event) {
+ rootGroup.getChildren().remove(line);
+ }
+ });
+ }
+
+ final Label labelStrength = new Label("-" + strength);
+ labelStrength.setTextFill(Color.RED);
+ labelStrength.setLayoutX(attacked.getPosition().getX());
+ labelStrength.setLayoutY(attacked.getPosition().getY() - 20);
+ synchronized (rootGroup) {
+ rootGroup.getChildren().add(labelStrength);
+
+ final TimerTask messageLabelStrengthTimerTask = new TimerTask() {
+ @Override
+ public void run() {
+ Platform.runLater(new Runnable() {
+ @Override
+ public void run() {
+ rootGroup.getChildren().remove(
+ labelStrength);
+ }
+ });
+ }
+ };
+ new Timer().schedule(messageLabelStrengthTimerTask, 2500);
+ }
+ };
+ });
+ }
}

0 comments on commit 3d3ec70

Please sign in to comment.
Something went wrong with that request. Please try again.