Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
GameNoodle/src/level/LevelHandler.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (64 sloc)
1.54 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package level; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.List; | |
import entities.Entity; | |
import level.tiles.Tile; | |
// Creates and manages the level when the game is running. | |
public class LevelHandler { | |
private Tile[][] tiles = new Tile[30][30]; | |
private List<Entity> entities; | |
private int width; | |
private int height; | |
private String levelName; | |
private String levelPath; | |
public LevelHandler(String levelPath) { | |
if (levelPath != null) { | |
this.levelPath = levelPath; | |
this.loadLevelFromFile(); | |
} | |
} | |
// TODO: Make work, preferably using JSON | |
private void loadLevelFromFile() { | |
try { | |
FileReader fileReader = new FileReader(levelPath); | |
BufferedReader bufferedReader = new BufferedReader(fileReader); | |
levelName = bufferedReader.readLine(); | |
String line = ""; | |
int yMarker = 0; | |
while((line = bufferedReader.readLine()) != null) { | |
int xMarker = 0; | |
for (String currentID : line.split("\\s")) { | |
tiles[yMarker][xMarker] = Tile.getTile(Integer.parseInt(currentID)); | |
xMarker++; | |
} | |
yMarker++; | |
} | |
bufferedReader.close(); | |
} catch (IOException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
public void tick() { | |
for (Tile[] tileArray : tiles) { | |
for (Tile t : tileArray) { | |
t.tick(); | |
} | |
} | |
for (Entity e : entities) { | |
e.tick(); | |
} | |
} | |
public String getLevelName() { | |
return levelName; | |
} | |
public void printTiles() { | |
for (Tile[] y : tiles) { | |
for (Tile x : y) { | |
System.out.print(x + " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |