PART TWO
MAP
CREATE A MAP FILE
Maps Folder
Create a folder at the project level called maps
Inside that folder, create a file called map1.txt
Map1
Create a text document filled with characters matching your world's height and width.
The recommended size is 30 wide by 15 tall.
In my example, periods represent dirt and commas represent grass.
You may use different characters if you wish!
MAP LOADER
World Class
Write a method named setTerrain(Cell cell, char code)
Use a series of conditionals to set the cell to the corresponding code.
Read File
Take a few minutes to review the basics:
In the World class, let's add a method called readFile().
Look at the code on the right, take some time to understand how it works:
We need to put it in a try/catch due to the risk of a FileNotFoundException. This is required by Java.
We loop through the rows and store each line in a String
Then we loop through the string to get each character
We then use that character code to set each tile in our 2D array.
World Constructor
In the World constructor, call readFile() instead of the lines that hardcoded specific lines of terrain from the last section.
CHECKPOINT
Run your code and see...
A mix of grass and dirt tiles matching your file
No gray tiles
Black bar still remains at the bottom