Skip to content

Commit

Permalink
fix: make ClimateConditions completely optional
Browse files Browse the repository at this point in the history
  • Loading branch information
naalit committed Aug 17, 2021
1 parent 6f5e8ff commit d11aa62
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
24 changes: 24 additions & 0 deletions src/main/java/org/terasology/gf/EnvironmentParametersSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.gf;

import org.joml.Vector3ic;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.registry.In;
import org.terasology.engine.registry.Share;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.terasology.gf.util.LocalParameters;

@RegisterSystem
@Share(EnvironmentParametersSystem.class)
public class EnvironmentParametersSystem extends BaseComponentSystem {
@In
private ClimateConditionsSystem climateConditionsSystem;

public LocalParameters createLocalParameters(Vector3ic position) {
return new EnvironmentLocalParameters(climateConditionsSystem, position);
}
}
19 changes: 14 additions & 5 deletions src/main/java/org/terasology/gf/PlantGrowingSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package org.terasology.gf;

import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.core.Time;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
Expand All @@ -33,7 +33,8 @@
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockComponent;
import org.terasology.gf.generator.PlantGrowthDefinition;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.terasology.gf.util.LocalParameters;
import org.terasology.gf.util.StaticLocalParameters;
import org.terasology.randomUpdate.RandomUpdateEvent;

/**
Expand All @@ -57,15 +58,23 @@ public class PlantGrowingSystem extends BaseComponentSystem {
@In
private DelayManager delayManager;
@In
private ClimateConditionsSystem environmentSystem;
private EnvironmentParametersSystem environmentSystem;

private LocalParameters createLocalParameters(Vector3ic position) {
if (environmentSystem != null) {
return environmentSystem.createLocalParameters(position);
} else {
return new StaticLocalParameters();
}
}

@ReceiveEvent
public void updatePlant(DelayedActionTriggeredEvent event, EntityRef plant, LivingPlantComponent plantComponent, BlockComponent blockComponent) {
if (event.getActionId().equals(UPDATE_PLANT_ACTION_ID)) {
PerformanceMonitor.startActivity("GrowingFlora - Updating plant");
try {
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type);
Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant);
Long updateDelay = plantDefinition.requestedUpdatePlant(worldProvider, createLocalParameters(blockComponent.getPosition()), blockEntityRegistry, plant);
if (updateDelay != null) {
delayManager.addDelayedAction(plant, UPDATE_PLANT_ACTION_ID, updateDelay);
}
Expand All @@ -80,7 +89,7 @@ public void randomPlantUpdate(RandomUpdateEvent event, EntityRef plant, LivingPl
PerformanceMonitor.startActivity("GrowingFlora - Updating plant");
try {
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(plantComponent.type);
if (plantDefinition.randomUpdatePlant(worldProvider, new EnvironmentLocalParameters(environmentSystem, blockComponent.getPosition()), blockEntityRegistry, plant)) {
if (plantDefinition.randomUpdatePlant(worldProvider, createLocalParameters(blockComponent.getPosition()), blockEntityRegistry, plant)) {
if (delayManager.hasDelayedAction(plant, UPDATE_PLANT_ACTION_ID)) {
delayManager.cancelDelayedAction(plant, UPDATE_PLANT_ACTION_ID);
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/terasology/gf/SaplingInitializeSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.joml.Vector3ic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.climateConditions.ClimateConditionsSystem;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
Expand All @@ -33,7 +32,8 @@
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockComponent;
import org.terasology.gf.generator.PlantGrowthDefinition;
import org.terasology.gf.util.EnvironmentLocalParameters;
import org.terasology.gf.util.LocalParameters;
import org.terasology.gf.util.StaticLocalParameters;

/**
* @author Marcin Sciesinski <marcins78@gmail.com>
Expand All @@ -52,7 +52,15 @@ public class SaplingInitializeSystem extends BaseComponentSystem {
@In
private DelayManager delayManager;
@In
private ClimateConditionsSystem climateConditionsSystem;
private EnvironmentParametersSystem environmentSystem;

private LocalParameters createLocalParameters(Vector3ic position) {
if (environmentSystem != null) {
return environmentSystem.createLocalParameters(position);
} else {
return new StaticLocalParameters();
}
}

// To avoid stack overflow
private boolean processingEvent;
Expand All @@ -70,10 +78,10 @@ public void plantedSapling(OnAddedComponent event, EntityRef sapling, LivingPlan
if (!processingEvent) {
processingEvent = true;
try {
Vector3i blockLocation = blockComponent.getPosition(new Vector3i());
Vector3ic blockLocation = blockComponent.getPosition();
String saplingType = livingPlant.type;
PlantGrowthDefinition plantDefinition = plantRegistry.getPlantGrowthDefinition(saplingType);
Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, new EnvironmentLocalParameters(climateConditionsSystem, blockLocation), blockEntityRegistry, sapling);
Long updateDelay = plantDefinition.initializePlantedPlant(worldProvider, createLocalParameters(blockLocation), blockEntityRegistry, sapling);
EntityRef blockEntity = blockEntityRegistry.getBlockEntityAt(blockLocation);
if (blockEntity.hasComponent(PlantedSaplingComponent.class)) {
blockEntity.removeComponent(PlantedSaplingComponent.class);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/terasology/gf/util/StaticLocalParameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.gf.util;

public class StaticLocalParameters implements LocalParameters {

@Override
public float getTemperature() {
return 0.5f;
}

@Override
public float getHumidity() {
return 0.5f;
}
}

0 comments on commit d11aa62

Please sign in to comment.