I’d like to know how to turn text config files into Java code.
I am working on a game and am using an Entity/Component approach where the Components are simply data containers (no processing logic). Given that they are so simple, I would like to be able to specify them in a text file and then convert them to classes.
For instance, if my text file looked like this:
Health
- maxHealth int
- currentHealth int
Position
- x float
- y float
- z float
It would create a Health Component that looked like this:
import framework.*;
class Health extends Component {
public final int maxHealth;
public final int currentHealth;
public Health(Entity entity, int maxHealth, int currentHealth) {
this.entity = entity;
this.maxHealth = maxHealth;
this. currentHeath = currentHealth;
}
}
It would also create an Enum of component ids for each component. This will make it easier to add new components later.
Converting the text file into something that looks like java code is not difficult, but I am not sure how to get the class instantiated along with the everything else. I have the feeling that involves using the Class Loader but while I have looked up some information on class loaders, I am a bit stuck as where to begin.
Both for the sake of performance and programming convenience, I want these to be actual classes not hashmap based properties. I don’t want to end up writing code that looks like:
int a = (Integer)entity.getComponent("health").getProperty("currentHealth");
Any help would be greatly appreciated.