Skip to content

Commit

Permalink
Fuel (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzos authored Mar 19, 2024
1 parent 7b09edb commit 81c8561
Show file tree
Hide file tree
Showing 20 changed files with 328 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.AnimationState;
Expand All @@ -35,7 +36,8 @@
import static whocraft.tardis_refined.common.util.TardisHelper.isInArsArea;

public class TardisClientData {

public static int FOG_TICK_DELTA = 0; // This is for the fading in and out of the fog.
private static int MAX_FOG_TICK_DELTA = 2 * 20; // This is for adjusting how fast the fog will fade in and out.

private final ResourceKey<Level> levelKey;
public AnimationState ROTOR_ANIMATION = new AnimationState();
Expand All @@ -61,6 +63,8 @@ public ResourceKey<Level> getLevelKey() {
private boolean isCrashing = false;
private boolean isOnCooldown = false;
private float flightShakeScale = 0;
private double fuel = 0;
private double maximumFuel = 0;

//Not saved to disk, no real reason to be
private int nextAmbientNoiseCall = 40;
Expand Down Expand Up @@ -161,6 +165,38 @@ public float flightShakeScale() {
return flightShakeScale;
}

public double getFuel() {
return fuel;
}
public void setFuel(double fuel) {
this.fuel = fuel;
}
public double getMaximumFuel() {
return maximumFuel;
}
public void setMaximumFuel(double fuel) {
this.maximumFuel = fuel;
}

/**
* Higher means more fog, lower means less fog
* @return 0 -> 1 float based off fog tick delta
*/
public static float getFogTickDelta() {
return 1f - (float) FOG_TICK_DELTA / (float) MAX_FOG_TICK_DELTA;
}
public static void tickFog(boolean hasFuel) {
if (!hasFuel && (FOG_TICK_DELTA <= MAX_FOG_TICK_DELTA) && (FOG_TICK_DELTA > 0)) {
FOG_TICK_DELTA--; // Fading in the fog
return;
}

if (hasFuel && (FOG_TICK_DELTA != MAX_FOG_TICK_DELTA)) {
FOG_TICK_DELTA++; // Fading out the fog
return;
}
}

/**
* Serializes the Tardis instance to a CompoundTag.
*
Expand All @@ -182,6 +218,9 @@ public CompoundTag serializeNBT() {

compoundTag.putString(NbtConstants.TARDIS_CURRENT_HUM, humEntry.getIdentifier().toString());

compoundTag.putDouble(NbtConstants.FUEL, fuel);
compoundTag.putDouble(NbtConstants.MAXIMUM_FUEL, maximumFuel);

return compoundTag;
}

Expand All @@ -204,6 +243,9 @@ public void deserializeNBT(CompoundTag compoundTag) {
shellPattern = new ResourceLocation(compoundTag.getString("shellPattern"));

setHumEntry(TardisHums.getHumById(new ResourceLocation(compoundTag.getString(NbtConstants.TARDIS_CURRENT_HUM))));

fuel = compoundTag.getDouble(NbtConstants.FUEL);
maximumFuel = compoundTag.getDouble(NbtConstants.MAXIMUM_FUEL);
}

/**
Expand Down Expand Up @@ -290,6 +332,10 @@ public void tickClientside() {
player.setYHeadRot(player.getYHeadRot() + (player.getRandom().nextFloat() - 0.5f) * flightShakeScale);
}
}

if (isThisTardis) {
tickFog(fuel != 0);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void tick() {
}
}

if (tardisClientData.getFuel() == 0f) {
volume = 0F;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public void tick(ServerLevel level) {
tardisClientData.setShellTheme(aestheticHandler.getShellTheme());
tardisClientData.setShellPattern(aestheticHandler.shellPattern().id());
tardisClientData.setHumEntry(interiorManager.getHumEntry());
tardisClientData.setFuel(pilotingManager.getFuel());
tardisClientData.setMaximumFuel(pilotingManager.getMaximumFuel());

tardisClientData.sync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.FogRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import whocraft.tardis_refined.client.TardisClientData;
import whocraft.tardis_refined.common.util.DimensionUtil;
import whocraft.tardis_refined.common.util.TardisHelper;
import whocraft.tardis_refined.registry.DimensionTypes;

@Mixin(FogRenderer.class)
public class FogRendererMixin {
Expand All @@ -29,12 +35,29 @@ private static void setupFog(Camera camera, FogRenderer.FogMode fogMode, float f
}
}

@Inject(at = @At("HEAD"), cancellable = true, method = "levelFogColor()V")
@Inject(at = @At("HEAD"), cancellable = true, method = "setupFog")
private static void setupColor(CallbackInfo callbackInfo) {
if (Minecraft.getInstance().player != null) {
BlockPos blockPosition = Minecraft.getInstance().player.blockPosition();

ClientLevel level = Minecraft.getInstance().level;

if (level.dimensionTypeId() != DimensionTypes.TARDIS) return;

TardisClientData reactions = TardisClientData.getInstance(level.dimension());

if (TardisClientData.getFogTickDelta() > 0.0f) {
float delta = TardisClientData.getFogTickDelta();

RenderSystem.setShaderFogColor(0, 0, 0, 1); // This sets the fog to a pitch black
RenderSystem.setShaderFogStart(Mth.lerp(delta, 16f, -8f)); // This positions the fog based off the delta
RenderSystem.setShaderFogEnd(16);
RenderSystem.setShaderFogShape(FogShape.SPHERE);

callbackInfo.cancel();
}

if (TardisHelper.isInArsArea(blockPosition)) {
TardisClientData reactions = TardisClientData.getInstance(Minecraft.getInstance().level.dimension());
Vec3 fogColor = reactions.fogColor(reactions.isCrashing() || reactions.isInDangerZone());
RenderSystem.setShaderFogColor((float) fogColor.x, (float) fogColor.y, (float) fogColor.z);
callbackInfo.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
Expand All @@ -13,6 +14,7 @@
import whocraft.tardis_refined.common.network.MessageType;
import whocraft.tardis_refined.common.network.TardisNetwork;
import whocraft.tardis_refined.common.tardis.TardisDesktops;
import whocraft.tardis_refined.common.tardis.manager.TardisInteriorManager;
import whocraft.tardis_refined.common.tardis.manager.TardisPilotingManager;
import whocraft.tardis_refined.common.tardis.themes.DesktopTheme;
import whocraft.tardis_refined.registry.SoundRegistry;
Expand Down Expand Up @@ -52,11 +54,20 @@ public void handle(MessageContext context) {
level.ifPresent(x -> {
TardisLevelOperator.get(x).ifPresent(operator -> {
TardisPilotingManager pilotManager = operator.getPilotingManager();
TardisInteriorManager interiorManager = operator.getInteriorManager();

if (!pilotManager.isInFlight()) {
operator.getInteriorManager().prepareDesktop(desktopTheme);
boolean inFlight = pilotManager.isInFlight();
boolean hasFuel = interiorManager.hasEnoughFuel();

if (!inFlight && hasFuel) {
interiorManager.prepareDesktop(desktopTheme);
pilotManager.removeFuel(interiorManager.getRequiredFuel());
} else {
x.playSound(null, context.getPlayer(), SoundRegistry.TARDIS_SINGLE_FLY.get(), SoundSource.BLOCKS, 10f, 0.25f);
if (inFlight)
x.playSound(null, context.getPlayer(), SoundRegistry.TARDIS_SINGLE_FLY.get(), SoundSource.BLOCKS, 10f, 0.25f);

if (!hasFuel)
x.playSound(null, context.getPlayer(), SoundRegistry.SCREWDRIVER_CONNECT.get(), SoundSource.BLOCKS, 10f, 0.25f); // Sound should be changed
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import whocraft.tardis_refined.common.network.MessageContext;
import whocraft.tardis_refined.common.network.MessageType;
import whocraft.tardis_refined.common.network.TardisNetwork;
import whocraft.tardis_refined.common.tardis.manager.AestheticHandler;
import whocraft.tardis_refined.common.tardis.manager.TardisExteriorManager;
import whocraft.tardis_refined.common.util.PlayerUtil;
import whocraft.tardis_refined.constants.ModMessages;
import whocraft.tardis_refined.patterns.ShellPattern;
Expand Down Expand Up @@ -55,9 +57,10 @@ public void toBytes(FriendlyByteBuf buf) {
public void handle(MessageContext context) {
Optional<ServerLevel> level = Optional.ofNullable(context.getPlayer().getServer().levels.get(resourceKey));
level.flatMap(TardisLevelOperator::get).ifPresent(y -> {
if(Upgrades.CHAMELEON_CIRCUIT_SYSTEM.get().isUnlocked(y.getUpgradeHandler())) {
if(Upgrades.CHAMELEON_CIRCUIT_SYSTEM.get().isUnlocked(y.getUpgradeHandler()) && y.getExteriorManager().hasEnoughFuelForShellChange()) {
y.setShellTheme(this.shellTheme, false);
y.getAestheticHandler().setShellPattern(pattern);
y.getPilotingManager().removeFuel(y.getExteriorManager().getFuelForShellChange());
} else {
PlayerUtil.sendMessage(context.getPlayer(), ModMessages.HARDWARE_OFFLINE, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package whocraft.tardis_refined.common.tardis.control.ship;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import whocraft.tardis_refined.common.capability.TardisLevelOperator;
import whocraft.tardis_refined.common.entity.ControlEntity;
import whocraft.tardis_refined.common.items.KeyItem;
Expand All @@ -16,10 +19,18 @@ public class MonitorControl extends Control {
@Override
public boolean onRightClick(TardisLevelOperator operator, ConsoleTheme theme, ControlEntity controlEntity, Player player) {
if (!player.level().isClientSide()){
ItemStack hand = player.getMainHandItem();

// Temporary for testing purposes
if (hand.is(Items.EXPERIENCE_BOTTLE)) {
operator.getPilotingManager().setFuel(operator.getPilotingManager().getMaximumFuel());
player.sendSystemMessage(Component.literal("Fueled up!"));
return true;
}

boolean isSyncingKey = false;
if (PlayerUtil.isInMainHand(player, ItemRegistry.KEY.get())){
KeyItem key = (KeyItem)player.getMainHandItem().getItem();
if (key.interactMonitor(player.getMainHandItem(),player, controlEntity, player.getUsedItemHand()))
if (hand.getItem() instanceof KeyItem key){
if (key.interactMonitor(hand,player, controlEntity, player.getUsedItemHand()))
isSyncingKey = true;
}
if (!isSyncingKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class AestheticHandler extends BaseHandler {
private ShellPattern shellPattern = ShellPatterns.DEFAULT;
private HumEntry currentHum = TardisHums.getDefaultHum();


public AestheticHandler(TardisLevelOperator tardisLevelOperator) {
super();
this.tardisOperator = tardisLevelOperator;
Expand Down Expand Up @@ -106,7 +107,6 @@ public void setShellTheme(ResourceLocation theme, boolean setupTardis, TardisNav

}


public void updateInteriorDoors(ResourceLocation theme) {
if (tardisOperator.getInternalDoor() != null) {
BlockPos internalDoorPos = tardisOperator.getInternalDoor().getDoorPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* External Shell data.
**/
public class TardisExteriorManager extends BaseHandler {
private double fuelForShellChange = 15; // Amount of fuel required to change the shell

private final TardisLevelOperator operator;
private TardisNavLocation lastKnownLocation = TardisNavLocation.ORIGIN;
Expand Down Expand Up @@ -216,4 +217,28 @@ public boolean isExitLocationSafe() {
return false;
}


/**
* Returns whether a Tardis has enough fuel to perform an interior change
* @return true if the Tardis has enough fuel
*/
public boolean hasEnoughFuelForShellChange() {
return this.operator.getPilotingManager().getFuel() >= this.getFuelForShellChange();
}

/**
* The amount of fuel required to change the exterior shell
* @return double amount of fuel to be removed
*/
public double getFuelForShellChange() {
return this.fuelForShellChange;
}

/**
* Sets the amount of fuel required to change the exterior shell
* @param fuel the amount of fuel
*/
private void setFuelForShellChange(double fuel) {
this.fuelForShellChange = fuel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,17 @@ public int getBlocksPerRequest(double distance) {

// All the logic related to the in-flight events of the TARDIS.
public void tick() {
if (this.operator.getPilotingManager().isInFlight() && !this.operator.getPilotingManager().isAutoLandSet()) {
TardisPilotingManager pilot = this.operator.getPilotingManager();

if (!this.operator.getPilotingManager().isCrashing()) {
if (pilot.isInFlight() && !pilot.isAutoLandSet()) {

if (!pilot.isCrashing()) {
ticksSincePrompted++;

if (controlRequestCooldown > 0) controlRequestCooldown--;

// Prepare the next control for highlighting.
if (!isWaitingForControlResponse && controlRequestCooldown == 0 && this.controlResponses < this.requiredControlRequests && !operator.getPilotingManager().isAutoLandSet()) {
if (!isWaitingForControlResponse && controlRequestCooldown == 0 && this.controlResponses < this.requiredControlRequests) {

// Record what control type needs pressing.
this.controlPrompt = possibleControls.get(operator.getLevel().random.nextInt(possibleControls.size()-1));
Expand Down Expand Up @@ -235,7 +237,7 @@ public void tick() {

}

if (this.isInDangerZone) {
if (this.isInDangerZone || pilot.isOutOfFuel()) {
tickDangerLevels();
}
}
Expand Down
Loading

0 comments on commit 81c8561

Please sign in to comment.