I (and others in the development channel) have been stewing with the idea of managing Entities with components. Anyone who has ever worked with a modern game engine will have experience with this in some way or another. The premise is given your basic Entity, you add or “attach” objects or components to give the Entity functionality. It intrinsically means that all entities are fundamentally the same and only components make them unique from another.
Obviously code speaks better than words, here is an example of an Entity/Component relationship:
API Goodness:
interface Component<T extends ComponentHolder> {
T getHolder();
}
interface ComponentHolder<T extends Component> {
T add(Class<T> clazz);
@Nullable
T get(Class<T> clazz);
Collection<T> getAll();
}
interface EntityComponent extends Component<Entity> {
Metadata getData();
}
interface Entity extends ComponentHolder<EntityComponent> {
}
interface Health extends EntityComponent {
double getHealth();
void setHealth(double health);
}
Minor implementation stuffs:
// Might be its own class, might be on Minecraft's EntityLiving?
class HealthComponent implements Health {
// If its own class, can have a way to set this
Entity entity;
@Override
public Entity getHolder() {
return entity;
}
@Override
public Metadata getData() {
return entity.getData();
}
@Override
public double getHealth() {
// If data doesn't exist, return a fallback
getData().get("health", 50.5);
}
@Override
public void setHealth() {
getData().set("health", 50.5);
}
}
Phew, that is just basic stuff (and certaintly has room for improvement). The biggest change in this system is we move away from this:
interface Entity {
}
interface EntityLiving extends Entity {
}
…which is a top down design. Have the “parts” that make up Minecraft’s Enderman or Creeper separated into their own segments makes extending them a breeze. Even better, it makes adding new entities very easy in the future (if/when we support client-side content).
As with all structures, there are cons. In this case, it comes down to incompatibility with existing code. We’ve gotten hints from Mojang at the prospect of Minecraft converting to a component structure but obviously we have no ETAs nor a guarantee. At the same point, staying the course with a top-down design means that should Minecraft switch to components, its an API-break.
So, weighing all the factors…what is everyone’s thoughts on the matter? This decision needs to be made soon-ish before we are at the point where we break plugins “correcting” this.
Components or not?