From 9210e43b6ac875f0d1e211868b6e98252726eee9 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Sun, 5 Jan 2020 14:40:52 +0100 Subject: [PATCH] Aufgabe 4.4 - updateStonesAndScore, allStonesBroken --- src/break_out/model/Level.java | 40 +++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 54edab2..261ffe8 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -154,7 +154,14 @@ public class Level extends Thread implements ILevel { // Call here the balls method for reacting on stones of the playground if(getBall().hitsStone(getStones())) { + updateStonesAndScore(); + System.out.println("count: "+stones.size()); + } + // if all stones are broken, go to next level + if(allStonesBroken()) { + // next level + System.out.println("next level"); } // update paddles position @@ -182,16 +189,16 @@ public class Level extends Thread implements ILevel { */ private void loadLevelData(int levelnr) { JSONReader reader = new JSONReader(String.format("res/Level%s.json", levelnr)); - int[][] stoneTypes = reader.getStones2DArray(); + int[][] stoneArray = reader.getStones2DArray(); lifeCounter = reader.getLifeCounter(); // clear stones list, not needed but could cause problems when not done stones.clear(); // foreach column - for(int y = 0; y < stoneTypes.length; y++) { + for(int y = 0; y < stoneArray.length; y++) { // foreach element in column x - for(int x = 0; x < stoneTypes[y].length; x++) { + for(int x = 0; x < stoneArray[y].length; x++) { Position tempPos = new Position(-1, -1); // position calculation, equivalent to grid calculation in "view.Field" @@ -201,10 +208,11 @@ public class Level extends Thread implements ILevel { tempPos.setX(blockWidth * x); tempPos.setY(blockHeight * y); - Stone tempStone = new Stone(stoneTypes[y][x], tempPos); + Stone tempStone = new Stone(stoneArray[y][x], tempPos); // add stone to list - stones.add(tempStone); + if(tempStone.getType() != 0) + stones.add(tempStone); } } } @@ -245,6 +253,28 @@ public class Level extends Thread implements ILevel { copy.addAll(stones); return copy; } + + /** + * The updater for the levels stones and the player score + */ + private void updateStonesAndScore() { + Stone stone = getBall().getHitStone(); + score += stone.getValue(); + stone.setType(stone.getType()-1); + + if(stone.getType() == 0) { + stones.remove(stone); + } + } + + /** + * checks whether all stones are broken + * @return true when all stones are broken + */ + private boolean allStonesBroken() { + if(stones.isEmpty()) return true; + else return false; + } }