1. Daniel Zwolenski
  2. fx-games

Commits

zonski  committed 9bde261

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

  • Participants
  • Parent commits c4254fb
  • Branches default

Comments (0)

Files changed (3)

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

View file
  • Ignore whitespace
         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");
                 }
+
             }
         }
     }

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

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

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

View file
  • Ignore whitespace
 
         return tower;
     }
+
 }