Anonymous avatar Anonymous committed 9bde261

Added credit handling - can only buy tower if you have enough credit.

Comments (0)

Files changed (3)

defender/src/main/java/com/fxexperience/games/defender/Game.java

         log.info("Starting new game with difficulty '{}'", difficulty);
         setDifficulty(difficulty);
         stopCurrentLevel();
+        player.setCredits(100);
+        player.setLives(3);
         nextLevel = 0;
         nextLevel();
     }
             if (currentLevel != null && towerFactory != null) {
                 log.info("Adding new tower '{}' at ({},{}) ",
                         new Object[]{towerFactory.getTowerName(), event.getX(), event.getY()});
-                Tower tower = towerFactory.createTower();
-                if (currentLevel.canPlaceTowerAt(tower, event.getX(), event.getY())) {
-                    tower.setTranslateX(event.getX());
-                    tower.setTranslateY(event.getY());
-                    currentLevel.getTowers().add(tower);
+
+                if (player.getCredits() >= towerFactory.getCreditCost()) {
+                    player.deductCredits(towerFactory.getCreditCost());
+                    Tower tower = towerFactory.createTower();
+                    if (currentLevel.canPlaceTowerAt(tower, event.getX(), event.getY())) {
+                        tower.setTranslateX(event.getX());
+                        tower.setTranslateY(event.getY());
+                        currentLevel.getTowers().add(tower);
+                    }
+                } else {
+                    // todo indicate to the user (e..g big red X, plat a sound, show a message, etc)
+                    log.info("Not enough credits to buy Tower");
                 }
+
             }
         }
     }

defender/src/main/java/com/fxexperience/games/defender/Player.java

     public void addCredits(int creditValue) {
         setCredits(getCredits() + creditValue);
     }
+
+    public void deductCredits(int credits) {
+        setCredits(Math.max(0, getCredits() - credits));
+    }
 }

defender/src/main/java/com/fxexperience/games/defender/tower/TowerFactory.java

 
         return tower;
     }
+
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.