1
0
Fork 0

minor comments and refactoring in break_out.model.Level

This commit is contained in:
rxbn_ 2020-01-25 00:01:15 +01:00
parent 87048abd6f
commit f6e8e6fcc5
1 changed files with 15 additions and 8 deletions

View File

@ -155,13 +155,13 @@ 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(); updateStonesAndScore();
System.out.println("count: "+stones.size()); //System.out.println("count: "+stones.size());
} }
// if all stones are broken, go to next level // if all stones are broken, go to next level
if(allStonesBroken()) { if(allStonesBroken()) {
// next level // next level
System.out.println("next level"); //System.out.println("next level");
} }
// update paddles position // update paddles position
@ -189,16 +189,18 @@ 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[][] stoneArray = reader.getStones2DArray(); int[][] stoneTypes = reader.getStones2DArray();
// life counter
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 < stoneArray.length; y++) { for(int y = 0; y < stoneTypes.length; y++) {
// foreach element in column x // foreach element in column x
for(int x = 0; x < stoneArray[y].length; x++) { for(int x = 0; x < stoneTypes[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"
@ -208,7 +210,7 @@ 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(stoneArray[y][x], tempPos); Stone tempStone = new Stone(stoneTypes[y][x], tempPos);
// add stone to list // add stone to list
if(tempStone.getType() != 0) if(tempStone.getType() != 0)
@ -258,10 +260,16 @@ public class Level extends Thread implements ILevel {
* The updater for the levels stones and the player score * The updater for the levels stones and the player score
*/ */
private void updateStonesAndScore() { private void updateStonesAndScore() {
// hit stone
Stone stone = getBall().getHitStone(); Stone stone = getBall().getHitStone();
// add value to score
score += stone.getValue(); score += stone.getValue();
// set new type
stone.setType(stone.getType()-1); stone.setType(stone.getType()-1);
// stones type is 0? remove it!
if(stone.getType() == 0) { if(stone.getType() == 0) {
stones.remove(stone); stones.remove(stone);
} }
@ -272,8 +280,7 @@ public class Level extends Thread implements ILevel {
* @return true when all stones are broken * @return true when all stones are broken
*/ */
private boolean allStonesBroken() { private boolean allStonesBroken() {
if(stones.isEmpty()) return true; return stones.isEmpty();
else return false;
} }
} }