Currently I'm working with LibGDX and using Ashley for ECS. Everything is fine, until i need to remove groups of related components.
Say we have a player that may use different weapons. For now, in my game a weapon is described by one or more components.
For example we have weapons, each is described by its own components:
weapon1: [ c0, c1, c2 ];
weapon2: [ c3, c4 ];
weapon3: [ c5, c6, c7 ];
It's ok when I want to add a weapon. I know which components are required and can put them to entity. The issue comes when I need to replace one weapon with another.
I can deal with if-else when it's only 3-5 weapons. But what if there are 20?
For now, my idea is to have additional component called WeaponComponent
.
Which will contain a family (I mean Ashley Family instance) that contains all information i need to remove the unnecessary components.
But for me it looks more like a workaround, not a solution.
So, does anybody know how to deal with this? Are there any other approaches?
UPDATE
OK, For now I have tried few implementations. And here is the one that seems to be the best for me. I have combined object-oriented and entity-component designes. The WeaponComponent
holds a reference to a Weapon
implementation. The Weapon
implementations know exaclty which components to attach/remove to/from an entity.
interface Weapon {
void attachTo(Entity entity);
void removeFrom(Entity entity);
}
class WeaponComponent {
private Entity owner;
private Weapon weapon;
public void setWeapon(Weapon weapon) {
if(this.weapon != null)
this.weapon.removeFrom(owner);
if(weapon != null) {
this.weapon = weapon;
this.weapon.attachTo(owner);
}
}
}
So, using this design, changing weapons is very easy now. And I don't have to care what weapon is already attached, and how to remove it:
engity.getComponent(WeaponComponent.class).setWeapon(whatEver);
.