Loading Configuration classes with json - UQcsse3200/2023-studio-2 GitHub Wiki
Loading Configuration classes
To load data for a new game area it is best to use the ConfigLoader class. This class allows for loading into a gameConfig, levelConfig, and gameAreaConfig from both a directory and file (only gameArea).
Loading from directory
Below is an example layout for the game levels directory:
assets/levels
| game.json
| gamestate.json
| global_assets.json
└─── earth
| | level.json
│ └─── main_area
│ | │ main.json
│ | └─── entities
│ | │ player.json
│ | │ ship.json
│ | │ ...
│ └─── hidden_area
│ │ main.json
│ └─── entities
│ │ player.json
│ │ spawners.json
│ │ ...
└─── moon
| level.json
└─── main_area
| ...
Using directories for loading game levels and maps improves debugging, flexibility and readability! Instead of using a single file to store a whole level, a level is broken into its game areas and a base level file.
Game files
The game requires 3 properties for a game config file, the levelNames, gameState and global assets. For the "levels" directory and the ConfigLoader
these are all stored in seperate .json files for readability and ease of debugging.
The game.json
file is used to outline each of the level names in the game. Note that the first level that will be shown is the first level in the levelNames list. The An example game.json
file is laid out below:
{
"levelNames": [
"earth",
"mars",
"pluto"
]
}
The gamestate.json
file is simply a mapping from string keys for the GameState
object to the value. These will be loaded into the GameState
object on startup. An example gameState.json
file is laid out below:
{
"nextPlanet": "glacial_desolation",
"currentPlanet": "verdant_oasis",
"extractorsMax/Solstite": 2
}
The global_assets.json
file is a file that represents an assetsConfig class. This lists out the textures, sounds and everything that needs to be loaded for every level. An example global_assets.json
file is laid out below:
{
"texturePaths": [
REQUIRED TEXTURE PATH,
REQUIRED TEXTURE PATH
],
"textureAtlasPaths": [
REQUIRED TEXTURE ATLAS PATH,
REQUIRED TEXTURE ATLAS PATH
],
"soundPaths": [
REQUIRED SOUND PATH,
REQUIRED SOUND PATH
],
"backgroundMusicPath": "sounds/BackgroundMusic.wav"
}
All game files are loaded using the ConfigLoader.loadGame()
method and return a gameConfig
.
Level files
The level.json
file is used to outline each of the game_areas in that level, and the next planet in the game sequence. Note that the first area that will be shown is the first area in the areaNames list.
{
"areaNames": [
"main_area",
"hidden_area"
],
"nextPlanet": "moon"
}
Level files are loaded using the ConfigLoader.loadLevel(levelName)
method and return a levelConfig
. An example level.json
file is laid out below:
Game area files
Each game area has a main.json
file and a folder of entities. The main.json
file is used to outline required textures, sounds, map layouts and win conditions. The entities folder contains a group of .json
files that outline all the entities to be spawned in the given mapGameArea.
In each game area, the .json
files contained are quite simple. The main.json file is laid out as below:
{
assets: {
"texturePaths": [
REQUIRED TEXTURE PATH,
REQUIRED TEXTURE PATH
],
"textureAtlasPaths": [
REQUIRED TEXTURE ATLAS PATH,
REQUIRED TEXTURE ATLAS PATH
],
"soundPaths": [
REQUIRED SOUND PATH,
REQUIRED SOUND PATH
],
"backgroundMusicPath": "sounds/BackgroundMusic.wav"
}
"mapName": "Planet Earth",
"terrainPath": "map/base.tmx",
"winConditions": [
{
"resource": "Solstite",
"threshold": 100
}
]
}
While each entity's .json
file is laid out as below:
{
"entityType": CONFIG_CLASS_NAME,
"entities": [
{
class: com.csse3200.game.entities.configs.CONFIG_CLASS_NAME,
CONFIG_INFO
},
{
MORE ENTITIES OF SAME CONFIG CLASS
}
]
}
For example:
{
"entityType": "ExtractorConfig",
"entities": [
{
class: com.csse3200.game.entities.configs.ExtractorConfig,
"resource": "Solstite",
"health": 200,
"tickRate": 1000,
"tickSize": 10,
"position": {
"x": 20,
"y": 30
}
},
{
class: com.csse3200.game.entities.configs.ExtractorConfig,
"resource": "Solstite",
"health": 200,
"tickRate": 1000,
"tickSize": 10,
"position": {
"x": 20,
"y": 30
}
}
]
}
NOTE: It is necessary that each object in the entities list has its class defined. Additionally, if only 1 entity is needing to be defined, it should still be within a list.
Loading from single file
Loading from a single file is generally not encouraged. As it increases complexity for a single file and is hard to debug - but in general it is just the combination of all the seperate files into a single .json file.
{
"assets": {
"texturePaths": [
REQUIRED TEXTURE PATH,
REQUIRED TEXTURE PATH
],
"textureAtlasPaths": [
REQUIRED TEXTURE ATLAS PATH,
REQUIRED TEXTURE ATLAS PATH
],
"soundPaths": [
REQUIRED SOUND PATH,
REQUIRED SOUND PATH
],
"backgroundMusicPath": "sounds/BackgroundMusic.wav"
}
"mapName": "Planet Earth",
"terrainPath": "map/base.tmx",
"winConditions": [
{
"resource": "Solstite",
"threshold": 100
}
],
"playerSpawn": {
"x": 10,
"y": 10
},
"areaEntityConfig": {
"PlayerConfig": [
{
class: com.csse3200.game.entities.configs.PlayerConfig,
"position": {
"x": 10,
"y": 40
},
"health": 100,
"baseAttack": 10,
"attackMultiplier": 1,
"isImmune": false,
"spritePath": "images/player/player.atlas"
}
],
"ExtractorConfig": [
{
class: com.csse3200.game.entities.configs.ExtractorConfig,
"resource": "Solstite",
"health": 200,
"tickRate": 1000,
"tickSize": 10,
"position": {
"x": 20,
"y": 30
}
},
{
class: com.csse3200.game.entities.configs.ExtractorConfig,
"resource": "Solstite",
"health": 200,
"tickRate": 1000,
"tickSize": 10,
"position": {
"x": 30,
"y": 30
}
}
]
}
}