Skip to content

Commit

Permalink
feat: Add Biomes Metrics Mode (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Nett <[email protected]>
  • Loading branch information
vedant-shroff and skaldarnar authored May 14, 2020
1 parent 682d7e0 commit e23cdf3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/main/java/org/terasology/biomesAPI/BiomeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.PlayerCharacterComponent;
import org.terasology.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
import org.terasology.physics.events.MovedEvent;
import org.terasology.registry.CoreRegistry;
import org.terasology.registry.In;
import org.terasology.registry.Share;
import org.terasology.world.WorldProvider;
import org.terasology.world.block.Block;
import org.terasology.world.chunks.CoreChunk;
import org.terasology.world.chunks.blockdata.ExtraBlockDataManager;
import org.terasology.world.chunks.blockdata.ExtraDataSystem;
import org.terasology.world.chunks.blockdata.RegisterExtraData;
import org.terasology.rendering.nui.NUIManager;
import org.terasology.rendering.nui.layers.ingame.metrics.DebugMetricsSystem;

import java.util.Collection;
import java.util.HashMap;
Expand All @@ -49,13 +55,23 @@ public class BiomeManager extends BaseComponentSystem implements BiomeRegistry {
@In
private EntityManager entityManager;

@In
private NUIManager nuiManager;

@In
private WorldProvider worldProvider;

@In
DebugMetricsSystem debugMetricsSystem;

private final Map<Short, Biome> biomeMap = new HashMap<>();

private static final Logger LOGGER = LoggerFactory.getLogger(BiomeManager.class);

private BiomesMetricsMode metricsMode;

private int biomeHashIndex;

@Override
public Optional<Biome> getBiome(Vector3i pos) {
return getBiome(pos.x, pos.y, pos.z);
Expand All @@ -79,7 +95,9 @@ public void setBiome(Biome biome, int x, int y, int z) {

@Override
public void setBiome(Biome biome, CoreChunk chunk, int relX, int relY, int relZ) {
setBiome(biome, chunk.chunkToWorldPosition(relX, relY, relZ));
Preconditions.checkArgument(biomeMap.containsKey(biome.biomeHash()), "Trying to use non-registered biome!");
biomeHashIndex = CoreRegistry.get(ExtraBlockDataManager.class).getSlotNumber("BiomesAPI.biomeHash");
chunk.setExtraData(biomeHashIndex, new Vector3i(relX, relY, relZ), biome.biomeHash());
}

@Override
Expand All @@ -89,14 +107,20 @@ public void setBiome(Biome biome, Vector3i pos) {

@Override
public void setBiome(Biome biome, CoreChunk chunk, Vector3i pos) {
setBiome(biome, chunk.chunkToWorldPosition(pos));
setBiome(biome, chunk, pos.x, pos.y, pos.z);
}

@Override
public <T extends Biome> List<T> getRegisteredBiomes(Class<T> biomeClass) {
return biomeMap.values().stream().filter(biomeClass::isInstance).map(biomeClass::cast).collect(Collectors.toList());
}

@Override
public void preBegin() {
metricsMode = new BiomesMetricsMode();
debugMetricsSystem.register(metricsMode);
}

/**
* Blocks have id, no matter what kind of blocks they are.
*/
Expand Down Expand Up @@ -128,11 +152,21 @@ public void checkBiomeChangeEvent(MovedEvent event, EntityRef entity) {
if (!oldBiomeOptional.isPresent()) {
return;
}

Biome newBiome = newBiomeOptional.get();
Biome oldBiome = oldBiomeOptional.get();
if (oldBiome != newBiome) {
entity.send(new OnBiomeChangedEvent(oldBiome, newBiome));
metricsMode.setBiome(newBiome.getId());
}
}
}

@ReceiveEvent(components = PlayerCharacterComponent.class)
public void checkPlayerSpawnedEvent(OnPlayerSpawnedEvent event, EntityRef entity, LocationComponent locationComponent) {
Vector3i spawnPos = new Vector3i(locationComponent.getWorldPosition());
getBiome(spawnPos)
.ifPresent(spawnBiome -> metricsMode.setBiome(spawnBiome.getId()));

}
}
52 changes: 52 additions & 0 deletions src/main/java/org/terasology/biomesAPI/BiomesMetricsMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.biomesAPI;

import org.terasology.rendering.nui.layers.ingame.metrics.MetricsMode;

/**
* Display the name of the biome the player is currently located in in the debug overlay.
* <p>
* Current biome is set when OnBiomeChanged event is triggered
* biomeName is polled whenever MetricsMode values are updated
*/
class BiomesMetricsMode extends MetricsMode {

private String biomeName;

BiomesMetricsMode() {
super("\n- Biome Info -");
}

@Override
public String getMetrics() {
return getName()+"\n"+biomeName;
}

@Override
public boolean isAvailable() {
return true;
}

@Override
public boolean isPerformanceManagerMode() {
return false;
}

public void setBiome(String biomeName) {
this.biomeName = biomeName;
}
}

0 comments on commit e23cdf3

Please sign in to comment.