Skip to content

Commit

Permalink
better prefab registering for jukebox tracks
Browse files Browse the repository at this point in the history
add console command to manually unlock tracks
make the tracks sound like they're coming from the jukebox instead of just magically going full stereo once you're close enough to it
  • Loading branch information
Govorunb committed Dec 21, 2023
1 parent 1edf61b commit 3be337b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
</relationship>
</object>
<object class="MixerBusPanner" id="{8b846630-8bcf-433a-88bf-263e2c979c62}">
<property name="surroundPanExtent">
<value>180</value>
</property>
<property name="surroundLFELevel">
<value>-2</value>
</property>
Expand Down
20 changes: 19 additions & 1 deletion SCHIZO/ConsoleCommands/ConsoleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using FMODUnity;
using JetBrains.Annotations;
using Nautilus.Commands;
using Nautilus.Handlers;
using SCHIZO.Helpers;
using UnityEngine;
using Object = UnityEngine.Object;
using TrackId = global::Jukebox.UnlockableTrack;

Check failure on line 13 in SCHIZO/ConsoleCommands/ConsoleCommands.cs

View workflow job for this annotation

GitHub Actions / build (Subnautica)

The type or namespace name 'Jukebox' could not be found in the global namespace (are you missing an assembly reference?)

Check failure on line 13 in SCHIZO/ConsoleCommands/ConsoleCommands.cs

View workflow job for this annotation

GitHub Actions / build (Subnautica)

The type or namespace name 'Jukebox' could not be found in the global namespace (are you missing an assembly reference?)

namespace SCHIZO.ConsoleCommands;

Expand Down Expand Up @@ -46,7 +48,7 @@ public static void OnConsoleCommand_say(params string[] args)
// so we don't have to needlessly split and join
ErrorMessage.AddMessage(string.Join(" ", args));
}

#region FMOD
public static string OnConsoleCommand_banks()
{
RuntimeManager.StudioSystem.getBankList(out Bank[] banks);
Expand Down Expand Up @@ -161,4 +163,20 @@ public static string OnConsoleCommand_fmod(params string[] args)
_ => null,
};
}
#endregion FMOD

[ConsoleCommand("unlocktrack"), UsedImplicitly]
public static string OnConsoleCommand_unlocktrack(params string[] trackArgs)
{
string track = string.Join(" ", trackArgs);
if (string.IsNullOrEmpty(track))
return "Usage: unlocktrack <track id>\nTrack ID can be specified by enum name or value (e.g. `Track1` or just `1`)";
// Nautilus only patches Enum.Parse...
if (!Enum.TryParse(track, true, out TrackId trackId) && !EnumHandler.TryGetValue(track, out trackId))
return $"No such track '{track}'";

if (!Player.main) return "Jukebox tracks can only be unlocked in-game";
global::Jukebox.Unlock(trackId, true);
return null;
}
}
64 changes: 57 additions & 7 deletions SCHIZO/Jukebox/CustomJukeboxDisk.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
using System.Collections.Generic;
using Nautilus.Assets;
using Nautilus.Assets.Gadgets;
using Nautilus.Assets.PrefabTemplates;
using Nautilus.Utility;
using SCHIZO.Creatures.Components;
using SCHIZO.Helpers;
using UnityEngine;

namespace SCHIZO.Jukebox;

public sealed class CustomJukeboxDisk : JukeboxDisk
public sealed class JukeboxDiskPrefab
{
public new void Start()
// prefab path "Misc/JukeboxDisk8.prefab"
private const string DISK_CLASSID = "5108080f-242b-49e8-9b91-d01d6bbe138c";

internal static Dictionary<string, JukeboxDiskPrefab> Prefabs = [];

public CustomPrefab NautilusPrefab { get; }
private JukeboxDiskPrefab(CustomJukeboxTrack track)
{
if (track == default)
string prefabName = $"{nameof(CustomJukeboxTrack)}_{track.identifier}";

NautilusPrefab = new CustomPrefab(prefabName, null, null);

NautilusPrefab.SetSpawns(new SpawnLocation(track.diskSpawnLocation.position, track.diskSpawnLocation.rotation));

NautilusPrefab.SetGameObject(new CloneTemplate(NautilusPrefab.Info, DISK_CLASSID)
{
LOGGER.LogError($"Jukebox disk {name} at {transform.position} was not assigned a track, self-destructing");
Destroy(this);
}
ModifyPrefab = prefab =>
{
prefab.SetActive(false);
if (track.diskPrefab)
{
Renderer[] renderers = prefab.GetComponentsInChildren<Renderer>();
renderers.ForEach(r => r.gameObject.SetActive(false));

GameObject customModel = GameObject.Instantiate(track.diskPrefab, prefab.transform, false);
customModel.transform.localPosition = Vector3.zero;

base.Start();
MaterialUtils.ApplySNShaders(customModel, 1);
}

JukeboxDisk diskComp = prefab.EnsureComponent<JukeboxDisk>();
diskComp.track = track;
if (!string.IsNullOrEmpty(track.unlockFmodEvent))
diskComp.acquireSound = AudioUtils.GetFmodAsset(track.unlockFmodEvent, FMODHelpers.GetId(track.unlockFmodEvent));

DisableUntilStoryGoal storyGate = prefab.EnsureComponent<DisableUntilStoryGoal>();
storyGate.storyGoalBZ = "SanctuaryCompleted";
}
});
NautilusPrefab.Register();
}

public static void Register(CustomJukeboxTrack track)
{
if (Prefabs.ContainsKey(track.identifier))
{
LOGGER.LogWarning($"Track {track.identifier} already registered, skipping prefab registration");
return;
}
Prefabs[track.identifier] = new JukeboxDiskPrefab(track);
}
}
39 changes: 3 additions & 36 deletions SCHIZO/Jukebox/CustomJukeboxTrack.BelowZero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected override void Register()
}
CustomJukeboxTrackPatches.customTracks[trackId] = this;
RegisterInJukebox(BZJukebox._main);
JukeboxDiskPrefab.Register(this);

if (!Player.main) return;

Expand Down Expand Up @@ -106,50 +107,16 @@ public static bool IsTrackCustom(BZJukebox.UnlockableTrack trackId)

internal void SetupUnlock()
{
BZJukebox.UnlockableTrack trackId = this;

if (!Player.main || !GameModeManager.HaveGameOptionsSet)
{
LOGGER.LogError($"Can't set up unlock for {trackId} with no {(!Player.main ? "player" : "game options")}!");
LOGGER.LogError($"Can't set up unlock for {identifier} with no {(!Player.main ? "player" : "game options")}!");
return;
}

if (unlockedOnStart || !GameModeManager.GetOption<bool>(GameOption.Story))
{
BZJukebox.Unlock(trackId, false);
BZJukebox.Unlock(this, false);
BZJukebox.main.SetInfo(JukeboxIdentifier, ToTrackInfo(false));
}
else
{
SpawnDisk(trackId, diskSpawnLocation.position, diskSpawnLocation.rotation);
}
}

private void SpawnDisk(BZJukebox.UnlockableTrack trackId, Vector3 position, Vector3 rotation)
{
GameObject disk = Instantiate(CustomJukeboxTrackPatches.defaultDiskPrefab);
disk.transform.position = position;
disk.transform.eulerAngles = rotation;

if (diskPrefab)
{
Renderer[] renderers = disk.GetComponentsInChildren<Renderer>();
renderers.ForEach(r => r.gameObject.SetActive(false));

GameObject customModel = Instantiate(diskPrefab, disk.transform, false);
customModel.transform.localPosition = Vector3.zero;

MaterialUtils.ApplySNShaders(disk, 1);
}

Destroy(disk.GetComponent<JukeboxDisk>());

CustomJukeboxDisk diskComp = disk.EnsureComponent<CustomJukeboxDisk>();
diskComp.track = trackId;
diskComp.acquireSound = AudioUtils.GetFmodAsset(unlockFmodEvent, FMODHelpers.GetId(unlockFmodEvent));

disk.GetComponent<LargeWorldEntity>().enabled = false; // don't save

LOGGER.LogDebug($"Spawned disk for {trackId} at {position}");
}
}
16 changes: 0 additions & 16 deletions SCHIZO/Jukebox/CustomJukeboxTrackPatches.BelowZero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,11 @@ public static class CustomJukeboxTrackPatches
{
internal static readonly SelfCheckingDictionary<BZJukebox.UnlockableTrack, CustomJukeboxTrack> customTracks = new("customTracks");

internal static GameObject defaultDiskPrefab;

static CustomJukeboxTrackPatches()
{
CoroutineHost.StartCoroutine(GetJukeboxDiskPrefab());
CoroutineHost.StartCoroutine(InitJukebox());
SaveUtils.RegisterOnQuitEvent(() => CoroutineHost.StartCoroutine(InitJukebox()));
}
private static IEnumerator GetJukeboxDiskPrefab()
{
if (defaultDiskPrefab) yield break;
// const string diskClassId = "5108080f-242b-49e8-9b91-d01d6bbe138c";
const string diskPrefabPath = "Misc/JukeboxDisk8.prefab";
IPrefabRequest request = PrefabDatabase.GetPrefabForFilenameAsync(diskPrefabPath);
yield return request;

if (!request.TryGetPrefab(out defaultDiskPrefab))
LOGGER.LogError("Could not get prefab for jukebox disk!");
LOGGER.LogDebug("Loaded default prefab for custom tracks");
}

private static IEnumerator InitJukebox()
{
Expand Down Expand Up @@ -87,7 +72,6 @@ public static void EnableRichText(object __instance)
[HarmonyPostfix]
public static void SetupUnlocksForCustomTracks()
{
// duplicate disks are not a problem - they self-destruct on Start if already unlocked
customTracks.ForEach(pair => pair.Value.SetupUnlock());
}

Expand Down

0 comments on commit 3be337b

Please sign in to comment.