-
Notifications
You must be signed in to change notification settings - Fork 7
Entity Indicator
A key aspect of the gameplay is the presence of hostile animals, which affect the player's performance and contradict the game's victory conditions. However, due to the large scale of the map, it is important to make the players aware of the presence of hostile animals. In particular, players should be made aware of the existence of these enemy entities. These enemies do not target the player, but aim to affect the players progress in the game.
An indicator will be shown on the edge of the screen to direct the player in the direction of the off-screen entity. This will alert the player of the entity and thus will allow them to take the necessary actions to stop the hostile entity.
To detect if an entity is on the screen this function was implemented in CameraComponent
. This checks if the entities centre position is within the view of the camera
public boolean entityOnScreen(Entity entity) {
Vector2 position = entity.getCenterPosition();
return camera.frustum.pointInFrustum(position.x, position.y, 0);
}
In the EntityIndicator
class, the indicator will only be visible if EntityOnScreen()
returns false
public void update() {
if (cameraComponent.entityOnScreen(entity)) {
indicator.setVisible(false);
} else {
indicator.setVisible(true);
updateIndicator();
}
}
If the entity is not on the screen, then the indicator will need to be updated to point in the position of the entity.
public void updateIndicator() {
float indicatorPosX;
float indicatorPosY;
// Get the latest entity position and transform it to a 3D vector in on-screen units based of the camera view
Vector2 entityPosition = entityToTractor.getCenterPosition();
Vector3 entityPos = new Vector3(entityPosition.x, entityPosition.y, 0);
cameraComponent.getCamera().project(entityPos);
// Get the center of the screen and adjust the x and y coordinates to correctly position indicator
Vector3 centerPosition = new Vector3((Gdx.graphics.getWidth() / 2f) + 73, (Gdx.graphics.getHeight() / 2f) + 54, 0);
// Get the distance vector to the entity, and normalise it, and get the angle to the entity
Vector3 toEntity = entityPos.cpy().sub(centerPosition);
toEntity.nor();
float angle = MathUtils.atan2(toEntity.y, toEntity.x);
// Calculate the new position for the indicator
float indicatorX = centerPosition.x + INDICATOR_DISTANCE * toEntity.x;
float indicatorY = centerPosition.y + INDICATOR_DISTANCE * toEntity.y;
// Set the position of the indicator
indicator.setPosition(indicatorX - indicator.getWidth() / 2f, indicatorY - indicator.getHeight() / 2f);
// Rotate the indicator to point toward the entity
// -90 is to adjust for initial rotation
indicator.setRotation(angle * MathUtils.radiansToDegrees - 90);
}
This will