|
1 | 1 | package model.battle.managers;
|
2 | 2 |
|
| 3 | +import model.Cell; |
| 4 | +import model.New_Plants.Plant; |
| 5 | +import model.New_Zombies.Zombie; |
| 6 | +import model.battle.Battle; |
| 7 | +import model.battle.PlantInGame; |
| 8 | +import model.battle.ZombieInGame; |
| 9 | +import model.card.CardOfZombie; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Random; |
| 15 | + |
3 | 16 | public class ZombieBattleManager extends BattleManager {
|
4 | 17 |
|
| 18 | + private final int plantsCount = 18; |
| 19 | + private final HashMap<String, Integer> plantsAndTheirCounts = new HashMap<>(); |
| 20 | + |
| 21 | + public ZombieBattleManager() { |
| 22 | + this.initializePlantsAndTheirCountNeeded(); |
| 23 | + this.setPlantsInMap(); |
| 24 | + } |
| 25 | + |
5 | 26 |
|
6 | 27 | @Override
|
7 | 28 | public void manage() {
|
| 29 | + Battle.getRunningBattle().getBattleComponents().manage(); |
| 30 | + this.checkForPlantsWin(); |
| 31 | + this.checkForZombiesWin(); |
| 32 | + Battle.getRunningBattle().incrementTurn(); |
| 33 | + } |
| 34 | + |
| 35 | + private void initializePlantsAndTheirCountNeeded() { |
| 36 | + this.plantsAndTheirCounts.put("Explode-o-nut", 3); |
| 37 | + this.plantsAndTheirCounts.put("Scaredy-shroom", 6); |
| 38 | + this.plantsAndTheirCounts.put("SnowPea", 2); |
| 39 | + this.plantsAndTheirCounts.put("Cabbage-pult", 2); |
| 40 | + this.plantsAndTheirCounts.put("Threepeater", 1); |
| 41 | + this.plantsAndTheirCounts.put("GatlingPea", 1); |
| 42 | + this.plantsAndTheirCounts.put("PotatoMine", 3); |
| 43 | + } |
| 44 | + |
| 45 | + private void setPlantsInMap() { |
| 46 | + ArrayList<Cell> randomCells = this.getRandomCells(this.plantsCount); |
| 47 | + for (Map.Entry element : this.plantsAndTheirCounts.entrySet()) { |
| 48 | + for (int i = 0; i < (int) element.getValue(); i++) { |
| 49 | + Plant plant = Plant.getPlant((String) element.getKey()); |
| 50 | + new PlantInGame(plant, randomCells.get(0)); |
| 51 | + randomCells.remove(0); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private ArrayList<Cell> getRandomCells(int randomCellsCount) { |
| 57 | + ArrayList<Cell> randomCells = new ArrayList<>(); |
| 58 | + Random random = new Random(); |
| 59 | + |
| 60 | + for (int i = 0; i < randomCellsCount; i++) { |
| 61 | + int randomRow = random.nextInt(6); |
| 62 | + int randomColumn = 2 * random.nextInt(9) + 1; |
| 63 | + Cell cell = Battle.getRunningBattle().getMap().getCell(randomRow, randomColumn); |
| 64 | + while (randomCells.contains(cell)) { |
| 65 | + randomRow = random.nextInt(6); |
| 66 | + randomColumn = 2 * random.nextInt(9) + 1; |
| 67 | + cell = Battle.getRunningBattle().getMap().getCell(randomRow, randomColumn); |
| 68 | + } |
| 69 | + randomCells.add(cell); |
| 70 | + } |
| 71 | + return randomCells; |
| 72 | + } |
| 73 | + |
| 74 | + private void insertZombie(int count) { |
| 75 | + int coinsNeeded = count * 10 * (((CardOfZombie) Battle.getRunningBattle().getSelectedCard()).getZombie().getHealthPoint()); |
| 76 | + if (Battle.getRunningBattle().getZombies().getSun() >= coinsNeeded) { |
| 77 | + if (this.getNumberOfZombiesInACellRow(Battle.getRunningBattle().getSelectedCell()) + count <= 2) { |
| 78 | + for (int i = 0; i < count; i++) { |
| 79 | + Zombie zombie = ((CardOfZombie) Battle.getRunningBattle().getSelectedCard()).getZombie(); |
| 80 | + new ZombieInGame(zombie, Battle.getRunningBattle().getSelectedCell()); |
| 81 | + } |
| 82 | + } else { |
| 83 | + // TODO should print a error that this row can maximum have 2 zombies |
| 84 | + System.out.println("A Row can have maximum of 2 zombies in a wave"); |
| 85 | + } |
| 86 | + } else { |
| 87 | + // TODO should print a error that player doesn't have enough coins |
| 88 | + System.out.println("You Don't have enough Coins"); |
| 89 | + } |
| 90 | + |
| 91 | + } |
8 | 92 |
|
| 93 | + private int getNumberOfZombiesInACellRow(Cell cell) { |
| 94 | + int counter = 0; |
| 95 | + for (int i = 0; i < Battle.getRunningBattle().getMap().getCells()[cell.getRow()].length; i++) { |
| 96 | + counter += Battle.getRunningBattle().getMap().getCells()[cell.getRow()][i].getZombies().size(); |
| 97 | + } |
| 98 | + return counter; |
9 | 99 | }
|
10 | 100 |
|
11 | 101 |
|
|
0 commit comments