Skip to content

Proximity Trigger Component

Rowan Gray edited this page Oct 5, 2023 · 1 revision

First see Components.

The ProximityTriggerComponent is a custom component used to call a function when there is at least one entity with the given flag component within the given radius.

If there are multiple entities with the given flag within the radius, the function is only called once. This will occur for every game tick there exists at least one entity within the range.

Constructors

The ProximityTriggerComponent is created by providing a radius, a flag component, and a trigger function.

public ProximityTriggerComponent(Class<? extends Component> flagComponent, float radius, TriggerFunc func)

The trigger function must take in no parameters and return nothing.

public interface TriggerFunc {
    void call();
}

Usage

Begin by importing the ProximityTriggerComponent as follows.

import com.csse3200.game.components.ProximityTriggerComponent;

Define a trigger function which takes no return parameters and returns void. This function will be called if there is an entity in the given range.

public void triggerFunction() {
    ...
}

Instantiate ProximityTriggerComponent and provide the component the entity must have. We will instantiate the component to track entities with a CombatStatsComponent within a range of 5.

ProximityActivationComponent pac = new ProximityActivationComponent(CombatStatsComponent.class, 5, this::triggerFunction);

Finally add the component to an entity.

Entity entity = new Entity().addComponent(pac);
Clone this wiki locally