0
\$\begingroup\$

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);
\$\endgroup\$
2
  • \$\begingroup\$ Seems OK to me. Maybe it could make sense to merge WeaponComponent and CombatComponent into one. But that depends on what these two components actually do. \$\endgroup\$
    – Philipp
    Commented Sep 7, 2016 at 8:14
  • \$\begingroup\$ @Philipp because WeaponComponent is an object, it doesn't attack itself right? So I need a Component that controls that weapon. \$\endgroup\$
    – ronscript
    Commented Sep 7, 2016 at 10:32

2 Answers 2

1
\$\begingroup\$

The notion of a target I believe can go beyond the scope of just combat.

For example, perhaps you need to interact with another player and trade an in-game item between them. You typically click on the targetted player, right click their portrait and select 'Trade'. Another scenario may be to click on an NPC and right click to display a specific dialog window.

In either of the above scenarios, there is the concept an TradeSystem and InteractionSystem that are all based around what the player currently has targetted (selected).

The combat system would be no different. It checks to see whether the current entity has a target, displays an error perhaps if not otherwise uses the target component to obtain the target entity reference and performs the appropriate combat actions.

\$\endgroup\$
1
\$\begingroup\$

Not sure if this exactly meets your needs, but you could also create a separate POJO that encapsulates attack and target and use this inside {Player|Police|Criminal}Component (composition!). You'd still have a little duplication (instantiating this POJO in each of the three classes), but not all duplication is bad, and it's just one line.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .