Player Interactions - UQcsse3200/2024-studio-3 GitHub Wiki
This component allows the player to interact with objects within the game. It utilises 'SensorComponent' to determine whether the player is in the chosen radius for them to be able to interact with this object. Currently, the key bind is set to 'E' for the player to interact, where an 'interact' event is triggered to be used with the relevant station.
SensorComponent
The sensor component uses the interactable service to gain knowledge of all of the interactable stations. It then finds the nearest one within a certain radius (if any exist within it), and updates the current interactable to be that entity. This entity is then notified of any events if necessary. It should be noted that the sensor component only updates what the nearest entity is if the player is moving.
Outline Feature
An outline effect has been added to highlight interactable objects when the player is nearby, giving a clear visual cue. This outline is dynamically applied based on proximity to the object. The OutlineComponent is responsible for adding and removing an outline effect on interactable objects.
Dynamic Outline Application in SensorComponent
The SensorComponent now interacts with the OutlineComponent to apply outlines to nearby interactable objects. This improves the visual feedback for the player.
private void addOutlineToFixture(Fixture fixture) {
BodyUserData userData = (BodyUserData) fixture.getBody().getUserData();
if (userData != null && userData.entity != null) {
Entity entity = userData.entity;
OutlineComponent outline = entity.getComponent(OutlineComponent.class);
if (outline != null) outline.setOutlined(true);
}
}
private void removeOutlineFromFixture(Fixture fixture) {
BodyUserData userData = (BodyUserData) fixture.getBody().getUserData();
if (userData != null && userData.entity != null) {
Entity entity = userData.entity;
OutlineComponent outline = entity.getComponent(OutlineComponent.class);
if (outline != null) outline.setOutlined(false);
}
}
Outline Rendering
In the OutlineComponent, the outline is dynamically rendered whenever the object is near the player.
@Override
public void render(SpriteBatch batch) {
if (isOutlined) {
addOutline(batch); // Apply the outline effect
} else {
removeOutline(batch); // Reset to normal rendering
}
}
PlayerActions
update() Method
Uses the update method in SensorComponent to check if there are any intractable objects near the player. It then uses 'getClosestFixture' to ensure that the player is able to interact with the closest fixture.
@Override
public void update() {
if (moving) {
updateSpeed();
}
// Check for the closest sensor
interactionSensor.update();
Fixture interactable = interactionSensor.getClosestFixture();
if (interactable != null) {
// This is where we know we can interact with an object
}
}
KeyboardPlayerInputComponent
public boolean keyDown(int keycode)
Checks if the player wants to interact with an object by clicking 'E'. If the interaction button is pressed, the 'isInteracting' flag is used freeze the player's movement whilst they are interacting with a station. The player becomes unfrozen 'interactionEnd' is triggered, either by manually pressing E again or the desired item is added to their inventory.
@Override
public boolean keyDown(int keycode) {
if (keycode == Keys.E) {
if (isInteracting) {
entity.getEvents().trigger("interactionEnd");
} else {
isInteracting = true;
entity.getEvents().trigger("interact");
}
return true;
}
return false;
}
TooltipsDisplay
The TooltipsDisplay
component provides visual feedback to the player when they are near an interactable object. It displays a tooltip indicating the key to press and the item or object name, ensuring clear interaction cues for players.
create()
Method
Initializes the TooltipsDisplay
component and sets up event listeners for showing and hiding the tooltip.
@Override
public void create() {
super.create();
addActors();
entity.getEvents().addListener("showTooltip", this::showTooltip);
entity.getEvents().addListener("hideTooltip", this::hideTooltip);
}
### `showTooltip(String tooltipText)` Method
Displays the tooltip with the given text when the player approaches an interactable object.
```java
private void showTooltip(String tooltipText) {
tooltipLabel.setText(tooltipText);
table.setVisible(true);
}
hideTooltip()
Method
Hides the tooltip when no interactable objects are nearby.
private void hideTooltip() {
tooltipLabel.setText("");
table.setVisible(false);
}
Integration with PlayerActions
The tooltip is triggered in the PlayerActions component based on the player's proximity to interactable objects. When the player is near an interactable object, the tooltip is displayed.
private void updateInteraction() {
interactionSensor.update();
Fixture interactable = interactionSensor.getClosestFixture();
if (interactable != null) {
String interactionKey = "Press E ";
String itemName = "Some Item";
entity.getEvents().trigger("showTooltip", interactionKey + ": " + itemName);
} else {
entity.getEvents().trigger("hideTooltip");
}
}