I am having trouble thinking of what component should hold the attack and target data. In the below code components are holding a redundant data which I want to eliminate.
public class PlayerComponent implements Component {
public boolean attack = false;
public Vector2 target = new Vector2();
}
public class PoliceComponent implements Component {
public boolean attack = false;
public Vector2 target = new Vector2();
}
public class CriminalComponent implements Component {
public boolean attack = false;
public Vector2 target = new Vector2();
}
I ended up creating an CombatComponent
that holds the both attack and target, but I doubt there is more better way. The purpose is to have a weapon system the check if the player or enemy trigger attack and get its target.
private void processWeapon() {
for (Entity weapon: weaponEntities) {
AttachmentComponent attachment = Mapper.attachment.get(weapon);
if(playerFamily.matches(attachment.owner)) {
PlayerComponent player = Mapper.player.get(attachment.owner);
CombatComponent combat = Mapper.combat.get(attachment.owner);
if( combat.attack ) {
WeaponComponent weaponComponent = Mapper.weapon.get(weapon);
weaponComponent.use = true;
weaponComponent.target.set(player.target);
}
}
}
}
or the another way calling the WeaponSystem
to use the owner weapon.
getEngine().getSystem(WeaponSystem.class).useWeapon(ownerEntity, targetPosition);
WeaponComponent
andCombatComponent
into one. But that depends on what these two components actually do. \$\endgroup\$WeaponComponent
is an object, it doesn't attack itself right? So I need aComponent
that controls that weapon. \$\endgroup\$