Very nice information here.
Do you recommend serializing components using json, then add them to the entity rather than serializing the entire entity with the components?
The first one seems cleaner, as a serialized character at the moment has literally over 1000 lines in the json file.
However, I don’t want to have to read the data in every time I spawn a new character or object, as this will make it slow.
For reference, this is what a basic character looks like:
private static Entity createHaldor(Vector2 position) {
Entity e = new Entity();
PhysicsComponent physicsComponent = new PhysicsComponent(
BodyType.DynamicBody, true, 2);
DrawableComponent drawableComponent = new DrawableComponent(new Sprite(
VRD.ASSETS.get("sprites/01/jump.png",
Texture.class)), DepthBuffers.VD);
SizeComponent sizeComponent = new SizeComponent(1.5f,
MathHelper.getRatioY(1.5f, drawableComponent.getSprite()
.getWidth(), drawableComponent.getSprite().getHeight()));
MotorComponent motorComponent = new MotorComponent();
AnimationComponent a = new AnimationComponent();
LabelComponent labelComponent = new LabelComponent();
ContactComponent contactComponent = new ContactComponent();
GroundColliderComponent groundColliderComponent = new GroundColliderComponent();
DirectionComponent directionComponent = new DirectionComponent();
AirborneStateComponent airborneComponent = new AirborneStateComponent();
groundColliderComponent.transform.setPosition(new Vector2(0, -.6f));
groundColliderComponent.transform.setParent(physicsComponent.transform);
groundColliderComponent.width = 0.35f;
groundColliderComponent.height = 0.25f;
labelComponent.name = "Haldor";
labelComponent.internalName = "haldor";
a.current = AnimationCache.getAnimation(labelComponent.internalName,
"jump");
motorComponent.maxSpeed = 4f;
motorComponent.accelerationRateX = 25f;
PolygonShape poly = new PolygonShape();
poly.setAsBox(0.75f / 2, 1.5f / 2);
CircleShape circle = new CircleShape();
circle.setRadius(0.375f);
circle.setPosition(new Vector2(0, -.375f));
physicsComponent.addNewFixtureDef(FixtureData.BUILDER.name("foot")
.density(0.25f).restitution(0f).friction(0f).shape(circle)
.dataToSet(contactComponent).build());
circle = new CircleShape();
circle.setRadius(0.365f);
circle.setPosition(new Vector2(0, .375f));
physicsComponent.addNewFixtureDef(FixtureData.BUILDER.name("head")
.density(0.25f).restitution(0f).friction(0f).shape(circle)
.dataToSet(contactComponent).build());
physicsComponent.transform.setPosition(position);
drawableComponent.transform.setParent(physicsComponent.transform);
drawableComponent.transform.setPosition(new Vector2(-sizeComponent
.getWidth() / 2, -sizeComponent.getHeight() / 2));
drawableComponent.transform.setAngleAxis(0, 0, 1, 0);
e.add(sizeComponent).add(physicsComponent)
.add(drawableComponent).add(motorComponent)
.add(contactComponent).add(a).add(labelComponent)
.add(directionComponent).add(groundColliderComponent).add(airborneComponent);
return e;
}
You can probably see why it might be beneficial to read the parameters from a file lol.
EDIT: I was considering using Lua, as this will allow me to have components configured in a script outside of the code base. I thought about this approach given the fact I won’t be maintaining this project (or won’t be the only one) in the long run.