Skip to content

Commit

Permalink
Hums (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeryn99 authored Jan 8, 2024
1 parent 5a96b64 commit 26b2d84
Show file tree
Hide file tree
Showing 84 changed files with 951 additions and 283 deletions.
10 changes: 7 additions & 3 deletions common/src/main/java/whocraft/tardis_refined/TardisRefined.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.slf4j.Logger;
import whocraft.tardis_refined.client.TRParticles;
import whocraft.tardis_refined.common.capability.upgrades.Upgrades;
import whocraft.tardis_refined.common.hum.TardisHums;
import whocraft.tardis_refined.common.network.TardisNetwork;
import whocraft.tardis_refined.common.network.messages.SyncConsolePatternsMessage;
import whocraft.tardis_refined.common.network.messages.SyncDesktopsMessage;
import whocraft.tardis_refined.common.network.messages.SyncShellPatternsMessage;
import whocraft.tardis_refined.common.network.messages.sync.SyncConsolePatternsMessage;
import whocraft.tardis_refined.common.network.messages.sync.SyncDesktopsMessage;
import whocraft.tardis_refined.common.network.messages.sync.SyncHumsMessage;
import whocraft.tardis_refined.common.network.messages.sync.SyncShellPatternsMessage;
import whocraft.tardis_refined.common.tardis.TardisDesktops;
import whocraft.tardis_refined.common.tardis.themes.ConsoleTheme;
import whocraft.tardis_refined.common.tardis.themes.ShellTheme;
Expand Down Expand Up @@ -58,10 +60,12 @@ public static void init() {
TardisDesktops.getReloadListener().setSyncPacket(TardisNetwork.NETWORK, SyncDesktopsMessage::new);
ConsolePatterns.getReloadListener().setSyncPacket(TardisNetwork.NETWORK, SyncConsolePatternsMessage::new);
ShellPatterns.getReloadListener().setSyncPacket(TardisNetwork.NETWORK, SyncShellPatternsMessage::new);
TardisHums.getReloadListener().setSyncPacket(TardisNetwork.NETWORK, SyncHumsMessage::new);
/* Need to register a default list of entries because on Fabric Cardinal Components classloads the TardisClientData class early on, before datapack entries have been added.
We will use these as fallback values when looking up patterns.
*/
ConsolePatterns.registerDefaultPatterns();
ShellPatterns.registerDefaultPatterns();
TardisHums.registerDefaultHums();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import whocraft.tardis_refined.client.screen.CancelDesktopScreen;
import whocraft.tardis_refined.client.screen.MonitorScreen;
import whocraft.tardis_refined.client.screen.selections.ShellSelectionScreen;
import whocraft.tardis_refined.client.screen.upgrades.UpgradesScreen;
import whocraft.tardis_refined.client.screen.waypoints.CoordInputType;
import whocraft.tardis_refined.client.screen.waypoints.WaypointListScreen;
Expand Down Expand Up @@ -60,4 +62,8 @@ public static void displayUpgradesScreen(CompoundTag upgradeTag) {
Minecraft.getInstance().setScreen(new UpgradesScreen(upgradeHandlerClient));
}
}

public static void openShellSelection(ResourceLocation currentShell) {
Minecraft.getInstance().setScreen(new ShellSelectionScreen(currentShell));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.sounds.SoundManager;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.AnimationState;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import whocraft.tardis_refined.client.sounds.HumSoundManager;
import whocraft.tardis_refined.client.sounds.LoopingSound;
import whocraft.tardis_refined.common.network.messages.SyncTardisClientDataMessage;
import whocraft.tardis_refined.client.sounds.QuickSimpleSound;
import whocraft.tardis_refined.common.hum.HumEntry;
import whocraft.tardis_refined.common.hum.TardisHums;
import whocraft.tardis_refined.common.network.messages.sync.SyncTardisClientDataMessage;
import whocraft.tardis_refined.common.tardis.themes.ShellTheme;
import whocraft.tardis_refined.constants.NbtConstants;
import whocraft.tardis_refined.patterns.ShellPatterns;
import whocraft.tardis_refined.registry.DimensionTypes;

import java.util.List;
import java.util.Map;

import static whocraft.tardis_refined.common.util.TardisHelper.isInArsArea;
Expand Down Expand Up @@ -51,13 +61,28 @@ public ResourceKey<Level> getLevelKey() {
private boolean isCrashing = false;
private boolean isOnCooldown = false;
private float flightShakeScale = 0;

//Not saved to disk, no real reason to be
private int nextAmbientNoiseCall = 40;


private ResourceLocation shellTheme = ShellTheme.FACTORY.getId();
private ResourceLocation shellPattern = ShellPatterns.DEFAULT.id();

private HumEntry humEntry = TardisHums.getDefaultHum();

public ResourceLocation getShellTheme() {
return shellTheme;
}

public HumEntry getHumEntry() {
return humEntry;
}

public void setHumEntry(HumEntry humEntry) {
this.humEntry = humEntry;
}

public void setShellTheme(ResourceLocation shellTheme) {
this.shellTheme = shellTheme;
}
Expand Down Expand Up @@ -155,6 +180,8 @@ public CompoundTag serializeNBT() {
compoundTag.putString("shellTheme", shellTheme.toString());
compoundTag.putString("shellPattern", shellPattern.toString());

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

return compoundTag;
}

Expand All @@ -176,6 +203,7 @@ public void deserializeNBT(CompoundTag compoundTag) {
shellTheme = new ResourceLocation(compoundTag.getString("shellTheme"));
shellPattern = new ResourceLocation(compoundTag.getString("shellPattern"));

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

/**
Expand Down Expand Up @@ -204,6 +232,10 @@ public void tickClientside() {


if (Minecraft.getInstance().player.level().dimensionTypeId() == DimensionTypes.TARDIS) {

ClientLevel tardisLevel = Minecraft.getInstance().level;
boolean isThisTardis = levelKey == tardisLevel.dimension();

createWorldAmbience(Minecraft.getInstance().player);


Expand All @@ -218,26 +250,47 @@ public void tickClientside() {
}
}

if(!soundManager.isActive(LoopingSound.HUM_TEST)){
soundManager.play(LoopingSound.HUM_TEST);

if (isThisTardis && humEntry != null && !humEntry.getSound().toString().equals(HumSoundManager.getCurrentRawSound().getLocation().toString()) || !soundManager.isActive(HumSoundManager.getCurrentSound())) {
HumSoundManager.playHum(SoundEvent.createVariableRangeEvent(humEntry.getSound()));
}
}

if (isThisTardis && tardisLevel.getGameTime() % nextAmbientNoiseCall == 0) {
nextAmbientNoiseCall = tardisLevel.random.nextInt(400, 2400);
List<ResourceLocation> ambientSounds = humEntry.getAmbientSounds();
if (!ambientSounds.isEmpty()) {
LocalPlayer player = Minecraft.getInstance().player;
RandomSource randomSource = tardisLevel.random;

if (LoopingSound.shouldMinecraftMusicStop(soundManager)) {
Minecraft.getInstance().getMusicManager().stopPlaying();
}
ResourceLocation randomSoundLocation = ambientSounds.get(randomSource.nextInt(ambientSounds.size()));
SoundEvent randomSoundEvent = SoundEvent.createVariableRangeEvent(randomSoundLocation);

QuickSimpleSound simpleSoundInstance = new QuickSimpleSound(randomSoundEvent, SoundSource.AMBIENT);
simpleSoundInstance.setVolume(0.3F);

double randomX = player.getX() + (randomSource.nextDouble() - 0.5) * 100;
double randomY = player.getY() + (randomSource.nextDouble() - 0.5) * 100;
double randomZ = player.getZ() + (randomSource.nextDouble() - 0.5) * 100;

// Responsible for screen-shake. Not sure of a better solution at this point in time.
if (isInDangerZone || isCrashing) {
if (Minecraft.getInstance().player.level().dimension() == levelKey) {
var player = Minecraft.getInstance().player;
player.setXRot(player.getXRot() + (player.getRandom().nextFloat() - 0.5f) * flightShakeScale);
player.setYHeadRot(player.getYHeadRot() + (player.getRandom().nextFloat() - 0.5f) * flightShakeScale);
simpleSoundInstance.setLocation(new Vec3(randomX, randomY, randomZ));
Minecraft.getInstance().getSoundManager().play(simpleSoundInstance);
}
}
}

if (LoopingSound.shouldMinecraftMusicStop(soundManager)) {
Minecraft.getInstance().getMusicManager().stopPlaying();
}


// Responsible for screen-shake. Not sure of a better solution at this point in time.
if (isInDangerZone || isCrashing) {
if (Minecraft.getInstance().player.level().dimension() == levelKey) {
var player = Minecraft.getInstance().player;
player.setXRot(player.getXRot() + (player.getRandom().nextFloat() - 0.5f) * flightShakeScale);
player.setYHeadRot(player.getYHeadRot() + (player.getRandom().nextFloat() - 0.5f) * flightShakeScale);
}
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import whocraft.tardis_refined.client.screen.components.GenericMonitorSelectionList;
import whocraft.tardis_refined.client.screen.components.SelectionListEntry;
import whocraft.tardis_refined.client.screen.selections.DesktopSelectionScreen;
import whocraft.tardis_refined.client.screen.selections.HumSelectionScreen;
import whocraft.tardis_refined.client.screen.selections.SelectionScreen;
import whocraft.tardis_refined.client.screen.selections.ShellSelectionScreen;
import whocraft.tardis_refined.client.screen.waypoints.CoordInputType;
import whocraft.tardis_refined.common.capability.upgrades.UpgradeHandler;
import whocraft.tardis_refined.common.capability.upgrades.Upgrades;
import whocraft.tardis_refined.common.network.messages.screens.C2SRequestShellSelection;
import whocraft.tardis_refined.common.network.messages.waypoints.C2SOpenCoordinatesDisplayMessage;
import whocraft.tardis_refined.common.network.messages.waypoints.RequestWaypointsMessage;
import whocraft.tardis_refined.common.tardis.TardisNavLocation;
Expand Down Expand Up @@ -71,7 +72,7 @@ public GenericMonitorSelectionList createSelectionList() {
selectionList.setRenderBackground(false);

if(Upgrades.CHAMELEON_CIRCUIT_SYSTEM.get().isUnlocked(upgradeHandler)){
selectionList.children().add(new SelectionListEntry(Component.translatable(ModMessages.UI_EXTERNAL_SHELL), entry -> Minecraft.getInstance().setScreen(new ShellSelectionScreen()), leftPos));
selectionList.children().add(new SelectionListEntry(Component.translatable(ModMessages.UI_EXTERNAL_SHELL), entry -> new C2SRequestShellSelection().send(), leftPos));
}

if(Upgrades.INSIDE_ARCHITECTURE.get().isUnlocked(upgradeHandler)){
Expand All @@ -87,6 +88,9 @@ public GenericMonitorSelectionList createSelectionList() {
selectionList.children().add(new SelectionListEntry(Component.translatable(ModMessages.UI_MONITOR_UPLOAD_COORDS), entry -> new C2SOpenCoordinatesDisplayMessage(CoordInputType.TRAVEL).send(), leftPos));
}

selectionList.children().add(new SelectionListEntry(Component.translatable(ModMessages.UI_MONITOR_SELECT_HUM), entry -> Minecraft.getInstance().setScreen(new HumSelectionScreen()), leftPos));


if(selectionList.children().isEmpty()){
noUpgrades = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package whocraft.tardis_refined.client.screen.selections;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import whocraft.tardis_refined.TardisRefined;
import whocraft.tardis_refined.client.screen.components.GenericMonitorSelectionList;
import whocraft.tardis_refined.client.screen.components.SelectionListEntry;
import whocraft.tardis_refined.common.hum.HumEntry;
import whocraft.tardis_refined.common.hum.TardisHums;
import whocraft.tardis_refined.common.network.messages.hums.ChangeHumMessage;
import whocraft.tardis_refined.common.util.MiscHelper;
import whocraft.tardis_refined.registry.SoundRegistry;

import java.io.StringReader;

public class HumSelectionScreen extends SelectionScreen {
private HumEntry currentHumEntry;


protected int imageWidth = 256;
protected int imageHeight = 173;
private int leftPos, topPos;

public static ResourceLocation MONITOR_TEXTURE = new ResourceLocation(TardisRefined.MODID, "textures/gui/monitor.png");


public HumSelectionScreen() {
super(Component.translatable(""));
}

@Override
public void addSubmitButton(int x, int y) {
super.addSubmitButton(x, y);
}

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

@Override
protected void init() {
this.setEvents(() -> {
HumSelectionScreen.selectHum(currentHumEntry);
}, () -> {
Minecraft.getInstance().setScreen(null);
});
this.currentHumEntry = grabHum();

this.leftPos = (this.width - this.imageWidth) / 2;
this.topPos = (this.height - this.imageHeight) / 2;

//Super method already creates the list, we don't need to create it a second time.
super.init();

addSubmitButton(width / 2 + 85, (height) / 2 + 35);
addCancelButton(width / 2 - 105, (height) / 2 + 35);
}

@Override
public boolean mouseClicked(double d, double e, int i) {
return super.mouseClicked(d, e, i);
}

private HumEntry grabHum() {
for (HumEntry humEntry : TardisHums.getRegistry().values()) {
return humEntry;
}
return null;
}

@Override
public void render(GuiGraphics guiGraphics, int i, int j, float f) {
this.renderTransparentBackground(guiGraphics);


/*Render Back drop*/
RenderSystem.setShader(GameRenderer::getPositionTexShader);
guiGraphics.blit(MONITOR_TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);


super.render(guiGraphics, i, j, f);

}

public static void selectHum(HumEntry theme) {
new ChangeHumMessage(Minecraft.getInstance().player.level().dimension(), theme).send();
Minecraft.getInstance().setScreen(null);
}

@Override
public void renderBackground(GuiGraphics guiGraphics, int i, int j, float f) {

}

@Override
public Component getSelectedDisplayName() {
return Component.Serializer.fromJson(currentHumEntry.getName());
}

@Override
public ObjectSelectionList createSelectionList() {
int leftPos = this.width / 2 - 75;
GenericMonitorSelectionList<SelectionListEntry> selectionList = new GenericMonitorSelectionList<>(this.minecraft, 100, 80, leftPos, this.topPos + 30, this.topPos + this.imageHeight - 60, 12);
selectionList.setRenderBackground(false);

for (HumEntry humEntry : TardisHums.getRegistry().values()) {
Component name = Component.literal(MiscHelper.getCleanName(humEntry.getIdentifier().getPath()));

// Check for if the tellraw name is incomplete, or fails to pass.
try {
var json = Component.Serializer.fromJson(new StringReader(humEntry.getName()).toString());
name = json;
} catch (Exception ex) {
TardisRefined.LOGGER.error("Could not process Name for datapack desktop " + humEntry.getIdentifier().toString());
}

selectionList.children().add(new SelectionListEntry(name, (entry) -> {
// previousImage = humEntry.getPreviewTexture();
this.currentHumEntry = humEntry;

for (Object child : selectionList.children()) {
if (child instanceof SelectionListEntry current) {
current.setChecked(false);
}
}
entry.setChecked(true);
}, leftPos));
}

return selectionList;
}

}
Loading

0 comments on commit 26b2d84

Please sign in to comment.