Skip to content

Entity Interactions

Isaac Robinson edited this page Aug 31, 2023 · 2 revisions

The InteractionControllerComponent - InteractableComponent system has been designed to allow any entity to interact with another, based on their specific use cases. The InteractionControllerComponent is attached to entities that are needing to be able to interact with other entities, while the InteractableComponent is attached to entities that can be interacted with in some way.

image

InteractionControllerComponent

To initialise a InteractionControllerComponent a new component simply needs to be added to an entity. The single parameter effectAll determines if interactions will effect all entities within reach of the entity, or if only the closest will be interacted with.

Entity entity = new Entity().addComponent(new InteractionControllerComponent(false));

Then to call the interaction event simply call entity.interact();.

InteractableComponent

The InteractableComponent takes 2 parameters on creation, a Consumer<Entity> runOnInteract function and a float distance. The runOnInteract consumer is a function that passes the entity calling the interaction and can perform any functionality desired from that. The distance is how close any entity has to be to interact with the entity attaching this component to.

An example interaction could dispose of the interactable entity if the player interacts within 10 units:

Entity checkpoint = new Entity().addComponent(new InteractableComponent(entity -> Gdx.app.postRunnable(Entity::dispose), 10);

Note that if the action makes use of another component (such as a CombatStatsComponent) you may need to add the component after the first initialisation of the entity.

Entity enemy = new Entity().addComponent(new CombatStatsComponent(health, baseAttack, attackMultiplier, isImmune));
//Sets enemies health to 0
enemy.addComponent(new Interactable(entity -> { enemy.getComponent(CombatStatsComponent.class).setHealth(0); }, 10));
Clone this wiki locally