Aufgabe 4.1 - loadLevelData
This commit is contained in:
parent
1b829fba1e
commit
2870a5fc9a
@ -2,6 +2,9 @@ package break_out.model;
|
|||||||
|
|
||||||
|
|
||||||
import break_out.Constants;
|
import break_out.Constants;
|
||||||
|
import break_out.controller.JSONReader;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class contains information about the running game
|
* This class contains information about the running game
|
||||||
@ -46,6 +49,16 @@ public class Level extends Thread implements ILevel {
|
|||||||
*/
|
*/
|
||||||
private Paddle paddleTop, paddleBottom;
|
private Paddle paddleTop, paddleBottom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The stones of the level
|
||||||
|
*/
|
||||||
|
private ArrayList<Stone> stones = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The life counter of the level
|
||||||
|
*/
|
||||||
|
private int lifeCounter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The constructor creates a new level object and needs the current game object,
|
* The constructor creates a new level object and needs the current game object,
|
||||||
* the number of the level to be created and the current score
|
* the number of the level to be created and the current score
|
||||||
@ -163,7 +176,32 @@ public class Level extends Thread implements ILevel {
|
|||||||
* @param levelnr The number X for the LevelX.json file
|
* @param levelnr The number X for the LevelX.json file
|
||||||
*/
|
*/
|
||||||
private void loadLevelData(int levelnr) {
|
private void loadLevelData(int levelnr) {
|
||||||
|
JSONReader reader = new JSONReader(String.format("res/Level%s.json", levelnr));
|
||||||
|
int[][] stoneTypes = 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++) {
|
||||||
|
// foreach element in column x
|
||||||
|
for(int x = 0; x < stoneTypes[y].length; x++) {
|
||||||
|
Position tempPos = new Position(-1, -1);
|
||||||
|
// position calculation, equivalent to grid calculation in "view.Field"
|
||||||
|
|
||||||
|
// size of grid blocks
|
||||||
|
int blockWidth = Constants.SCREEN_WIDTH / Constants.SQUARES_X;
|
||||||
|
int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y;
|
||||||
|
|
||||||
|
tempPos.setX(blockWidth * x);
|
||||||
|
tempPos.setY(blockHeight * y);
|
||||||
|
Stone tempStone = new Stone(stoneTypes[y][x], tempPos);
|
||||||
|
|
||||||
|
// add stone to list
|
||||||
|
stones.add(tempStone);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -192,6 +230,13 @@ public class Level extends Thread implements ILevel {
|
|||||||
this.levelFinished = finished;
|
this.levelFinished = finished;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The getter for the levels stones
|
||||||
|
* @return stones The stones of the level
|
||||||
|
*/
|
||||||
|
public ArrayList<Stone> getStones() {
|
||||||
|
return stones;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user