-
Notifications
You must be signed in to change notification settings - Fork 2
Player Interactions
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.
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.
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.
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);
}
}
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
}
}
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
}
}
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;
}
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.
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);
}
Hides the tooltip when no interactable objects are nearby.
private void hideTooltip() {
tooltipLabel.setText("");
table.setVisible(false);
}
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");
}
}
Inventory System
Scoring System
Food Recipes
Level System
Player Actions
Ordering System
Stations
Items
Map Design
Customers
Pause Menu
Upgrades
End of Day Display
Day Night Cycle
Moral System
Debug Terminal
Game Interactions Tutorial
Backstory Cutscenes
Entities and Components
Input Handling
Game Screens and Areas
Fire Extinguisher Handler Component
MainGameActions Create Docket Triggers
Main Game Order Button Display
BackstoryCutsceneDisplay Test Plan
Test Plan for MainGameOrderTicketDisplay
Test Plan for MainGameOrderBtnDisplay
Test Plan for DocketLineDisplay
Test plan for RandomComboService
Test plan for SpeedBoostUpgrade
Test plan for DancePartyUpgrade