Skip to content

Proximity Activation Component

Rowan Gray edited this page Aug 31, 2023 · 3 revisions

First see Components.

The ProximityActivationComponent is a custom component used to call an entry method when a tracked entity enters a defined radius and an exit method when a tracked entity exits a defined radius.

An entity will only call the entry method once for the period of time it is within the defined radius. Similarly, an entity outside of the defined radius will only call the exit method once (immediately after exiting the radius).

The default state upon instantiation is for all entities to be outside of the defined radius. This means that an entry method will be called on the first update if any of the entities begin within the radius, but an exit method will not be called for any entities that begin outside of the radius.

Constructors

The ProximityActivationComponent is created by providing a radius, entity to track, enter function, and exit function.

ProximityActivationComponent(float radius, Entity entity, ProximityFunc entered, ProximityFunc exited)

ProximityActivationComponent(float radius, List<Entity> entities, ProximityFunc entered, ProximityFunc exited) 

The enter and exit function should take in a single Entity as the parameter and return nothing (void).

public interface ProximityFunc {
    void call(Entity entity);
}

Usage

Begin by importing the ProximityActivationComponent as follows.

import com.csse3200.game.components.ProximityActivationComponent;

Define an enter and exit function which take in an Entity and return void.

public void enterFunction(Entity entity) {
    ...
}

public void exitFunction(Entity entity) {
    ...
}

Instantiate ProximityActivationComponent with a single entity to track.

Entity entityToTrack = new Entity();
float radius = 1.5f;

ProximityActivationComponent pac = new ProximityActivationComponent(radius, entityToTrack, this::enterFunction, this::exitFunction);

or instantiate ProximityActivationComponent with a list of entities to track.

List<Entity> entitiesToTrack = new ArrayList<>();
float radius = 1.5f;

// add as entities as required
entitiesToTrack.add(new Entity());
entitiesToTrack.add(new Entity());
...
entitiesToTrack.add(new Entity());

ProximityActivationComponent pac = new ProximityActivationComponent(radius, entitiesToTrack, this::enterFunction, this::exitFunction);

Finally add the component to an entity.

Entity entity = new Entity().addComponent(pac);

Testing

Testing was implemented using JUnit and Mockito. There is at least one test for each function within the proximity component. The main points of focus for testing in this component was checking that the enter and exit functions were called at the appropriate time. This was done by mocking enter and exit functions and verifying whether they were called when expected.

Clone this wiki locally