Skip to content

Interaction Prompt

abhijithvarma03 edited this page Sep 14, 2023 · 11 revisions

To enable the interaction prompt to show up once the player goes within the interactable range of an entity that is, near the entity, the DistanceCheckComponent and the ProximityControllerComponent work together to make that happen.

DistanceCheckComponent

The DistanceCheckComponent class extends the Component class and takes 2 arguments. The first is distance, which is of type float, this is essentially how much you want the interactable distance to be for the prompt to show up. The second is label, which is an instance of the class InteractLabel.

This is usually initialized with the .addComponent method with the entity that you want the player to interact with. For example:

extractor.addComponent(new DistanceCheckComponent(5f, interactLabel));

ProximityControllerComponent

The ProximityControllerComponentclass extends the Component class and can be initialized without any arguments. This needs to be added to the PlayerFactory via .addComponent for the interaction prompt to show up once the player goes near an interactable entity.

player.addComponent(new ProximityControllerComponent());

The checkAllEntitiesProximity() must then be called in the game loop so that the interaction prompt can keep popping up and disappearing in terms of the player's position with respect to the entity.

ProximityControllerComponent proximityController = player.getComponent(ProximityControllerComponent.class);
proximityController.checkAllEntitiesProximity();
Clone this wiki locally