Customer Interaction - UQcsse3200/2024-studio-3 GitHub Wiki
This component allows the player to interact with customer entities within the game. It utilizes CustomerSensorComponent to detect whether a customer is within a certain range of the player. When the player is close enough, it triggers events for customer-related interactions.
The create() method initialises the CustomerSensorComponent and sets up event listeners for customer interactions. The component listens for collisions that signal the player is near a customer.
@Override
public void create() {
entity.getEvents().addListener("collisionStart", this::onCollisionStart);
entity.getEvents().addListener("collisionEnd", this::onCollisionEnd);
this.interactionComponent = entity.getComponent(InteractionComponent.class);
super.create();
}
This method is triggered when the player's sensor collides with a customer entity. It checks if the interaction is valid (for example, the player is within range of a customer) and adds the customer to the list of colliding fixtures if they are close enough.
public void onCollisionStart(Fixture me, Fixture other) {
if (interactionComponent.getFixture() != me) {
return;
}
if (!PhysicsLayer.contains(targetLayer, other.getFilterData().categoryBits)) {
return;
}
if (isWithinDistance(other, sensorDistance)) {
collidingFixtures.add(other);
}
updateFixtures();
}
This method updates the list of colliding fixtures (customers). It removes any customer entities that are no longer within range and updates the closestFixture and closestDistance variables to point to the nearest customer.
private void updateFixtures() {
Set<Fixture> toRemove = new HashSet<>();
for (Fixture fixture : collidingFixtures) {
float dist = getFixtureDistance(fixture);
if (dist > sensorDistance) {
toRemove.add(fixture);
} else if (closestDistance < sensorDistance || dist < closestDistance) {
closestDistance = dist;
closestFixture = fixture;
}
}
collidingFixtures.removeAll(toRemove);
}