Skip to content

Commit

Permalink
feat(JOML): migrate HeatTriggeringSystem (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored Jan 5, 2021
1 parent 80a8256 commit 604ae64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
18 changes: 8 additions & 10 deletions src/main/java/org/terasology/heat/HeatTriggeringSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.terasology.heat;

import org.joml.Vector3i;
import org.terasology.workstationCrafting.component.PortableWorkstationComponent;
import org.terasology.engine.Time;
import org.terasology.entitySystem.entity.EntityManager;
Expand All @@ -28,7 +29,6 @@
import org.terasology.heat.component.HeatConsumerComponent;
import org.terasology.heat.component.HeatProducerComponent;
import org.terasology.math.Side;
import org.terasology.math.geom.Vector3i;
import org.terasology.monitoring.PerformanceMonitor;
import org.terasology.registry.In;
import org.terasology.workstation.event.WorkstationStateChanged;
Expand Down Expand Up @@ -99,8 +99,8 @@ public void update(float delta) {
HeatProducerComponent.FuelSourceConsume fuelSourceConsume = fuelConsumedIterator.next();
// If the fuel no longer has any meaningful impact on the producer - remove it
if (fuelSourceConsume.startTime + fuelSourceConsume.burnLength < currentTime
&& HeatUtils.solveHeatEquation(fuelSourceConsume.heatProvided, 20, producer.temperatureLossRate,
currentTime - (fuelSourceConsume.startTime + fuelSourceConsume.burnLength)) < REMOVE_FUEL_THRESHOLD) {
&& HeatUtils.solveHeatEquation(fuelSourceConsume.heatProvided, 20, producer.temperatureLossRate,
currentTime - (fuelSourceConsume.startTime + fuelSourceConsume.burnLength)) < REMOVE_FUEL_THRESHOLD) {
fuelConsumedIterator.remove();
changed = true;
} else {
Expand All @@ -118,7 +118,6 @@ public void update(float delta) {
}



// TODO: TEST for PortableWorkstations.

PerformanceMonitor.startActivity("Heat - heat update2");
Expand Down Expand Up @@ -154,8 +153,8 @@ public void update(float delta) {
HeatProducerComponent.FuelSourceConsume fuelSourceConsume = fuelConsumedIterator.next();
// If the fuel no longer has any meaningful impact on the producer - remove it
if (fuelSourceConsume.startTime + fuelSourceConsume.burnLength < currentTime
&& HeatUtils.solveHeatEquation(fuelSourceConsume.heatProvided, 20, producer.temperatureLossRate,
currentTime - (fuelSourceConsume.startTime + fuelSourceConsume.burnLength)) < REMOVE_FUEL_THRESHOLD) {
&& HeatUtils.solveHeatEquation(fuelSourceConsume.heatProvided, 20, producer.temperatureLossRate,
currentTime - (fuelSourceConsume.startTime + fuelSourceConsume.burnLength)) < REMOVE_FUEL_THRESHOLD) {
fuelConsumedIterator.remove();
changed = true;
} else {
Expand All @@ -177,8 +176,8 @@ public void update(float delta) {
/**
* Store residual heat from removed producers into consumers.
*
* @param event The event corresponding to the situation before the producer was removed
* @param entity The heat producer being removed
* @param event The event corresponding to the situation before the producer was removed
* @param entity The heat producer being removed
* @param producer The heat producer component on the heat producer being removed
*/
@ReceiveEvent
Expand All @@ -188,8 +187,7 @@ public void beforeProducerRemoved(BeforeRemoveComponent event, EntityRef entity,
float heat = HeatUtils.calculateHeatForProducer(producer);

// If this is a portable Workstation entity, this will have neither. So return.
if (!entity.hasComponent(BlockComponent.class) && !entity.hasComponent(BlockRegionComponent.class))
{
if (!entity.hasComponent(BlockComponent.class) && !entity.hasComponent(BlockRegionComponent.class)) {
return;
}

Expand Down
30 changes: 15 additions & 15 deletions src/main/java/org/terasology/heat/HeatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package org.terasology.heat;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.engine.Time;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.heat.component.HeatConsumerComponent;
import org.terasology.heat.component.HeatProducerComponent;
import org.terasology.math.JomlUtil;
import org.terasology.math.Region3i;
import org.terasology.math.Side;
import org.terasology.math.geom.Vector3i;
import org.terasology.registry.CoreRegistry;
import org.terasology.world.BlockEntityRegistry;
import org.terasology.world.block.BlockComponent;
import org.terasology.world.block.BlockRegion;
import org.terasology.world.block.regions.BlockRegionComponent;

import java.util.Collections;
Expand Down Expand Up @@ -160,14 +160,14 @@ public static double calculateResidualHeatValue(long gameTime, HeatConsumerCompo
return residualHeat.baseHeat * Math.pow(Math.E, -1 * timeSinceHeatWasEstablished);
}

public static Region3i getEntityBlocks(EntityRef entityRef) {
public static BlockRegion getEntityBlocks(EntityRef entityRef) {
BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i blockPosition = blockComponent.getPosition();
return Region3i.createBounded(blockPosition, blockPosition);
Vector3i blockPosition = blockComponent.getPosition(new Vector3i());
return new BlockRegion(blockPosition);
}
BlockRegionComponent blockRegionComponent = entityRef.getComponent(BlockRegionComponent.class);
return JomlUtil.from(blockRegionComponent.region);
return blockRegionComponent.region;
}

/**
Expand All @@ -183,15 +183,15 @@ public static Map<Vector3i, Side> getPotentialHeatSourceBlocksForConsumer(Entity
return Collections.emptyMap();
}

Region3i entityBlocks = getEntityBlocks(consumer);
BlockRegion entityBlocks = getEntityBlocks(consumer);

Map<Vector3i, Side> result = new HashMap<>();

for (Vector3i entityBlock : entityBlocks) {
for (Vector3ic entityBlock : entityBlocks) {
for (Side heatDirection : consumerComp.heatDirections) {
Vector3i heatedBlock = new Vector3i(entityBlock);
heatedBlock.add(heatDirection.getVector3i());
if (!entityBlocks.encompasses(heatedBlock)) {
heatedBlock.add(heatDirection.direction());
if (!entityBlocks.contains(heatedBlock)) {
result.put(heatedBlock, heatDirection);
}
}
Expand All @@ -212,15 +212,15 @@ public static Map<Vector3i, Side> getPotentialHeatedBlocksForProducer(EntityRef
return Collections.emptyMap();
}

Region3i entityBlocks = getEntityBlocks(producer);
BlockRegion entityBlocks = getEntityBlocks(producer);

Map<Vector3i, Side> result = new HashMap<>();

for (Vector3i entityBlock : entityBlocks) {
for (Vector3ic entityBlock : entityBlocks) {
for (Side heatDirection : producerComp.heatDirections) {
Vector3i heatedBlock = new Vector3i(entityBlock);
heatedBlock.add(heatDirection.getVector3i());
if (!entityBlocks.encompasses(heatedBlock)) {
heatedBlock.add(heatDirection.direction());
if (!entityBlocks.contains(heatedBlock)) {
result.put(heatedBlock, heatDirection);
}
}
Expand Down

0 comments on commit 604ae64

Please sign in to comment.