-
Notifications
You must be signed in to change notification settings - Fork 9
Saveable Component
The saveable component is a simple adapter for entities that allows the game to save all entities easily. To use this component it must be added to a Entity and then called using .save()
.
The SaveableComponent is a generic component - meaning that the type is defined by the type of entity config that will be returned by the .save()
method (the type T
must extend BaseEntityConfig
). When calling the constructor a simple function must be defined as one of the parameters which returns the relevant config class that represents the current state of the entity. An example instantiation onto a player entity would look as follows:
player.addComponent(new SaveableComponent<>(p -> {
PlayerConfig playerConfig = config;
playerConfig.position = p.getGridPosition();
playerConfig.health = p.getComponent(CombatStatsComponent.class).getHealth();
playerConfig.maxHealth = p.getComponent(CombatStatsComponent.class).getMaxHealth();
return playerConfig;
}, PlayerConfig.class));
In the above example we use the variable config, which was the original config this player was created with to reduce the amount of duplication for fields that don't change thruoghout the execution of the program - such as spritePaths, etc.
To save an entity simply call player.getComponent(SaveableComponent.class).save()
and it will return the respective config file which then can be written to a .json file using the FileLoader.writeClass
method.
Escape Earth Game
Interaction Controller and Interactable Components
Game and Entity Configuration Files
Loading Game Configuration Files