Skip to content

Commit

Permalink
Merge pull request #133 from Terasology/chore/reduce-log-spam
Browse files Browse the repository at this point in the history
chore(minimap): reduce log noise from CharacterOverlay
  • Loading branch information
DarkWeird authored Jan 29, 2021
2 parents fabb1e8 + 4fb9a73 commit cc06b5d
Showing 1 changed file with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,72 @@
import org.terasology.utilities.Assets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

/**
* This class is used to add character overlays to the minimap based on the citizen type
* and position
* This class is used to add character overlays to the minimap based on the citizen type and position
*/


public class CharacterOverlay implements MinimapOverlay {
private static final Logger logger = LoggerFactory.getLogger(CharacterOverlay.class);
private static final int ICON_SIZE = 16;

private Vector2i iconSize = new Vector2i(ICON_SIZE, ICON_SIZE);

private Logger logger = LoggerFactory.getLogger(CharacterOverlay.class);

private ArrayList<EntityRef> Citizens;

private final Map<String, Optional> map = new HashMap<String, Optional>();
private ArrayList<EntityRef> citizens;
private final Map<String, Texture> overlays = new HashMap<>();


/**
* This constructor sets creates the list of citizens
*/
public CharacterOverlay() {
this.Citizens = new ArrayList<EntityRef>();
this.map.put("MetalRenegades:marketCitizen", Assets.getTexture("MetalRenegades:marketGooey"));
this.map.put("MetalRenegades:badCitizen", Assets.getTexture("MetalRenegades:badCitizen"));
this.map.put("MetalRenegades:goodCitizen", Assets.getTexture("MetalRenegades:goodCitizen"));
this.map.put("MetalRenegades:gooeyCitizen", Assets.getTexture("MetalRenegades:gooeyCitizen"));
this.map.put("MetalRenegades:neutralGooey", Assets.getTexture("MetalRenegades:neutralGooey"));
this.map.put("MetalRenegades:nocturnalGooey", Assets.getTexture("MetalRenegades:nocturnalGooey"));
this.map.put("MetalRenegades:scaredGooey", Assets.getTexture("MetalRenegades:scaredGooey"));
this.map.put("MetalRenegades:angryGooey", Assets.getTexture("MetalRenegades:angryGooey"));
this.map.put("MetalRenegades:friendlyGooey", Assets.getTexture("MetalRenegades:friendlyGooey"));
this.map.put("MetalRenegades:enemyGooey", Assets.getTexture("MetalRenegades:enemyIcon"));
this.citizens = new ArrayList<EntityRef>();
registerOverlay("MetalRenegades:marketCitizen", () -> Assets.getTexture("MetalRenegades:marketGooey"));
registerOverlay("MetalRenegades:badCitizen", () -> Assets.getTexture("MetalRenegades:badCitizen"));
registerOverlay("MetalRenegades:goodCitizen", () -> Assets.getTexture("MetalRenegades:goodCitizen"));
registerOverlay("MetalRenegades:gooeyCitizen", () -> Assets.getTexture("MetalRenegades:gooeyCitizen"));
registerOverlay("MetalRenegades:neutralGooey", () -> Assets.getTexture("MetalRenegades:neutralGooey"));
registerOverlay("MetalRenegades:nocturnalGooey", () -> Assets.getTexture("MetalRenegades:nocturnalGooey"));
registerOverlay("MetalRenegades:scaredGooey", () -> Assets.getTexture("MetalRenegades:scaredGooey"));
registerOverlay("MetalRenegades:angryGooey", () -> Assets.getTexture("MetalRenegades:angryGooey"));
registerOverlay("MetalRenegades:friendlyGooey", () -> Assets.getTexture("MetalRenegades:friendlyGooey"));
registerOverlay("MetalRenegades:enemyGooey", () -> Assets.getTexture("MetalRenegades:enemyIcon"));
}

private void registerOverlay(final String citizenType, Supplier<Optional<Texture>> textureSupplier) {
Optional<Texture> texture = textureSupplier.get();
if (texture.isPresent()) {
overlays.put(citizenType, texture.get());
} else {
logger.warn("No icon found for citizen '{}'", citizenType);
}
}

@Override
public void render(Canvas canvas, Rectanglei worldRect) {

Collection<EntityRef> citizens = this.Citizens;

for (EntityRef entity : citizens) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (locationComponent == null) {
logger.error("Cannot find location component for Citizen: ");
return;
logger.warn("Cannot render overlay for entity '{}' - missing LocationComponent", entity.getId());
continue;
}

Vector2f location = new Vector2f(locationComponent.getLocalPosition().x(), locationComponent.getLocalPosition().z());
Vector2i mapPoint = RectUtility.map(worldRect, canvas.getRegion(), new Vector2i((int) location.x, (int) location.y), new Vector2i());
Vector2i iconCenter = new Vector2i((int) (mapPoint.x - (iconSize.x / 2.0f)), (int) (mapPoint.y - (iconSize.y / 2.0f)));

if (isInside(iconCenter, canvas.getRegion())) {
Rectanglei region = RectUtility.createFromMinAndSize((int) iconCenter.x, (int) iconCenter.y, (int) iconSize.x, (int) iconSize.y);
String citizenType = entity.getParentPrefab().getName();
if (this.map.get(citizenType) != null) {
Optional<Texture> icon = this.map.get(citizenType);
if (icon.isPresent()) {
canvas.drawTexture(icon.get(), region);
} else {
logger.error("No icon found for citizen" + citizenType);
}
String citizenType = entity.getParentPrefab().getName();
Optional<Texture> icon = Optional.ofNullable(overlays.get(citizenType));
if (icon.isPresent()) {
Vector2f location = new Vector2f(locationComponent.getLocalPosition().x(),
locationComponent.getLocalPosition().z());
Vector2i mapPoint = RectUtility.map(worldRect, canvas.getRegion(), new Vector2i((int) location.x,
(int) location.y), new Vector2i());
Vector2i iconCenter = new Vector2i((int) (mapPoint.x - (iconSize.x / 2.0f)),
(int) (mapPoint.y - (iconSize.y / 2.0f)));
if (isInside(iconCenter, canvas.getRegion())) {
Rectanglei region = RectUtility.createFromMinAndSize(iconCenter.x, iconCenter.y, iconSize.x,
iconSize.y);
canvas.drawTexture(icon.get(), region);
}
}
}
Expand All @@ -91,7 +91,7 @@ public void render(Canvas canvas, Rectanglei worldRect) {
* Checks if the citizen to be drawn lies inside the visible region in the minimap
*
* @param point: the coordinates of the point to be checked
* @param box: limits
* @param box: limits
* @return whether point is to be drawn or not
*/
private boolean isInside(Vector2i point, Rectanglei box) {
Expand All @@ -106,7 +106,7 @@ private boolean isInside(Vector2i point, Rectanglei box) {
*/

public void addCitizen(EntityRef entityRef) {
this.Citizens.add(entityRef);
this.citizens.add(entityRef);
}

/**
Expand All @@ -116,7 +116,7 @@ public void addCitizen(EntityRef entityRef) {
*/

public void removeCitizen(EntityRef entityRef) {
this.Citizens.remove(entityRef);
this.citizens.remove(entityRef);

}

Expand Down

0 comments on commit cc06b5d

Please sign in to comment.