Aufgabe 4.4 - updateStonesAndScore, allStonesBroken
This commit is contained in:
parent
2102f5c27b
commit
9210e43b6a
@ -154,7 +154,14 @@ public class Level extends Thread implements ILevel {
|
|||||||
|
|
||||||
// Call here the balls method for reacting on stones of the playground
|
// Call here the balls method for reacting on stones of the playground
|
||||||
if(getBall().hitsStone(getStones())) {
|
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
|
// update paddles position
|
||||||
@ -182,16 +189,16 @@ public class Level extends Thread implements ILevel {
|
|||||||
*/
|
*/
|
||||||
private void loadLevelData(int levelnr) {
|
private void loadLevelData(int levelnr) {
|
||||||
JSONReader reader = new JSONReader(String.format("res/Level%s.json", levelnr));
|
JSONReader reader = new JSONReader(String.format("res/Level%s.json", levelnr));
|
||||||
int[][] stoneTypes = reader.getStones2DArray();
|
int[][] stoneArray = reader.getStones2DArray();
|
||||||
lifeCounter = reader.getLifeCounter();
|
lifeCounter = reader.getLifeCounter();
|
||||||
|
|
||||||
// clear stones list, not needed but could cause problems when not done
|
// clear stones list, not needed but could cause problems when not done
|
||||||
stones.clear();
|
stones.clear();
|
||||||
|
|
||||||
// foreach column
|
// foreach column
|
||||||
for(int y = 0; y < stoneTypes.length; y++) {
|
for(int y = 0; y < stoneArray.length; y++) {
|
||||||
// foreach element in column x
|
// 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 tempPos = new Position(-1, -1);
|
||||||
// position calculation, equivalent to grid calculation in "view.Field"
|
// 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.setX(blockWidth * x);
|
||||||
tempPos.setY(blockHeight * y);
|
tempPos.setY(blockHeight * y);
|
||||||
Stone tempStone = new Stone(stoneTypes[y][x], tempPos);
|
Stone tempStone = new Stone(stoneArray[y][x], tempPos);
|
||||||
|
|
||||||
// add stone to list
|
// 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);
|
copy.addAll(stones);
|
||||||
return copy;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user