Skip to content

Commit

Permalink
Add item sounds (untested 😃)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexejhero committed Nov 1, 2023
1 parent 3351c6e commit 45785bc
Show file tree
Hide file tree
Showing 187 changed files with 113 additions and 47 deletions.
2 changes: 1 addition & 1 deletion SCHIZO/Creatures/Ermshark/ErmsharkAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ partial class ErmsharkAttack
{
private void Awake()
{
attackSounds = attackSounds.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
attackSounds = attackSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
}

public override void OnTouch(Collider collider)
Expand Down
2 changes: 1 addition & 1 deletion SCHIZO/Events/Ermcon/ErmconAttendee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void Awake()
// "meta"-priority - this number determines the order in which actions get *evaluated*
// and the priority obtained from Evaluate() actually determines which action gets *performed*
evaluatePriority = 99f;
pickupDeniedSounds = pickupDeniedSounds.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
pickupDeniedSounds = pickupDeniedSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);
}
public void OnHandHover(GUIHand hand)
{
Expand Down
5 changes: 5 additions & 0 deletions SCHIZO/Items/ModItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public void LoadStep2()
unlockTechTypes = new List<TechType> {this},
});
}

if (ItemData.itemSounds)
{
ItemData.itemSounds.Register(this);
}
}

public static implicit operator PrefabInfo(ModItem self) => self.PrefabInfo;
Expand Down
1 change: 1 addition & 0 deletions SCHIZO/SCHIZO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<Folder Include="Attributes/Validation" />
<Folder Include="Attributes/Visual" />
<Folder Include="Utilities" />
<Folder Include="_old\Sounds\Patches\" />
</ItemGroup>

<ItemGroup>
Expand Down
36 changes: 36 additions & 0 deletions SCHIZO/Sounds/ItemSounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using Nautilus.Utility;

namespace SCHIZO.Sounds;

partial class ItemSounds
{
private static readonly Dictionary<TechType, ItemSounds> _registeredItemSounds = new();

public static bool TryGet(TechType techType, out ItemSounds itemSounds)
{
return _registeredItemSounds.TryGetValue(techType, out itemSounds);
}

public void Register(TechType techType)
{
if (_registeredItemSounds.TryGetValue(techType, out ItemSounds existingItemSounds) && existingItemSounds != this)
{
throw new Exception($"ItemSounds for {techType} already registered by {existingItemSounds}");
}

_registeredItemSounds[techType] = this;

// If multiple items share the same sounds, Initialize will return the same SoundCollectionInstance as long as the bus is the same, so we don't need to worry about not calling it.

pickupSounds = pickupSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
dropSounds = dropSounds!?.Initialize(AudioUtils.BusPaths.UnderwaterCreatures);

drawSounds = drawSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
holsterSounds = holsterSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);

cookSounds = cookSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
eatSounds = eatSounds!?.Initialize(AudioUtils.BusPaths.PDAVoice);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@
namespace SCHIZO.Sounds.Patches;

[HarmonyPatch]
public static class CreatureSoundsPatches
public static class ItemSoundsPatches
{
[HarmonyPatch(typeof(Pickupable), nameof(Pickupable.PlayPickupSound))]
[HarmonyPostfix]
public static void PlayCustomPickupSound(Pickupable __instance)
{
if (!CreatureSoundsHandler.TryGetCreatureSounds(__instance.GetTechType(), out CreatureSounds sounds)) return;

sounds.PickupSounds?.Play2D();
if (!ItemSounds.TryGet(__instance.GetTechType(), out ItemSounds itemSounds)) return;
itemSounds.pickupSounds!?.Play2D();
}

[HarmonyPatch(typeof(Pickupable), nameof(Pickupable.PlayDropSound))]
[HarmonyPostfix]
public static void PlayCustomDropSound(Pickupable __instance)
{
if (!CreatureSoundsHandler.TryGetCreatureSounds(__instance.GetTechType(), out CreatureSounds sounds)) return;
if (sounds.DropSounds == null) return;
if (!ItemSounds.TryGet(__instance.GetTechType(), out ItemSounds sounds)) return;
if (sounds.dropSounds == null) return;

sounds.HolsterSounds?.CancelAllDelayed();
sounds.DropSounds.Play(__instance.GetComponent<FMOD_CustomEmitter>());
sounds.holsterSounds!?.CancelAllDelayed();
sounds.dropSounds.Play(__instance.GetComponent<FMOD_CustomEmitter>());
}

[HarmonyPatch(typeof(PlayerTool), nameof(PlayerTool.OnDraw))]
Expand All @@ -36,12 +35,12 @@ public static void PlayCustomDrawSound(PlayerTool __instance)
{
try
{
if (!__instance.pickupable || !CreatureSoundsHandler.TryGetCreatureSounds(__instance.pickupable.GetTechType(), out CreatureSounds sounds)) return;
if (sounds.DrawSounds == null) return;
if (!__instance.pickupable || !ItemSounds.TryGet(__instance.pickupable.GetTechType(), out ItemSounds sounds)) return;
if (sounds.drawSounds == null) return;

if (Time.time < sounds.PickupSounds?.LastPlay + 0.5f) return;
if (Time.time < sounds.pickupSounds!?.LastPlay + 0.5f) return;

sounds.DrawSounds.Play2D();
sounds.drawSounds.Play2D();
}
catch
{
Expand All @@ -55,14 +54,14 @@ public static void PlayCustomHolsterSound(PlayerTool __instance)
{
try
{
if (!__instance.pickupable || !CreatureSoundsHandler.TryGetCreatureSounds(__instance.pickupable.GetTechType(), out CreatureSounds sounds)) return;
if (sounds.HolsterSounds == null) return;
if (!__instance.pickupable || !ItemSounds.TryGet(__instance.pickupable.GetTechType(), out ItemSounds sounds)) return;
if (sounds.holsterSounds == null) return;

if (Time.time < sounds.DropSounds?.LastPlay + 0.5f) return;
if (Time.time < sounds.EatSounds?.LastPlay + 0.5f) return;
if (Time.time < sounds.CookSounds?.LastPlay + 0.5f) return;
if (Time.time < sounds.dropSounds!?.LastPlay + 0.5f) return;
if (Time.time < sounds.eatSounds!?.LastPlay + 0.5f) return;
if (Time.time < sounds.cookSounds!?.LastPlay + 0.5f) return;

sounds.HolsterSounds.Play2D(0.15f);
sounds.holsterSounds.Play2D(0.15f);
}
catch
{
Expand Down Expand Up @@ -97,23 +96,24 @@ public static IEnumerable<CodeInstruction> Injector(IEnumerable<CodeInstruction>

private static void Patch(TechType techType)
{
if (!CreatureSoundsHandler.TryGetCreatureSounds(techType, out CreatureSounds sounds)) return;
if (!ItemSounds.TryGet(techType, out ItemSounds sounds)) return;
if (sounds.eatSounds == null) return;

if (Time.time < sounds.EatSounds?.LastPlay + 0.1f) return;
if (Time.time < sounds.eatSounds.LastPlay + 0.1f) return;

sounds.HolsterSounds?.CancelAllDelayed();
sounds.EatSounds?.Play2D();
sounds.holsterSounds!?.CancelAllDelayed();
sounds.eatSounds.Play2D();
}
}

[HarmonyPatch(typeof(Crafter), nameof(Crafter.OnCraftingBegin))]
[HarmonyPostfix]
public static void PlayCustomCookSound(TechType techType)
{
if (!CreatureSoundsHandler.TryGetCreatureSounds(techType, out CreatureSounds sounds)) return;
if (sounds.CookSounds == null) return;
if (!ItemSounds.TryGet(techType, out ItemSounds sounds)) return;
if (sounds.cookSounds == null) return;

sounds.HolsterSounds?.CancelAllDelayed();
sounds.CookSounds.Play2D();
sounds.holsterSounds!?.CancelAllDelayed();
sounds.cookSounds.Play2D();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SCHIZO.Creatures.Components;
namespace SCHIZO.Sounds.Players;

partial class HurtSoundPlayer : IOnTakeDamage
{
Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Ermfish/Cooked ermfish.asset
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ MonoBehaviour:
isCraftable: 1
isBuildable: 0
craftingTime: 2.5
itemSounds: {fileID: 11400000, guid: bfb77f100ed7e6e4fb42c9b05bcb00af, type: 2}
registerInSN: 1
recipeSN: {fileID: 11400000, guid: 902e1464a3d91ad4a9dfb020ab4770a8, type: 2}
craftTreeTypeSN: 1
Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Ermfish/Cured ermfish.asset
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ MonoBehaviour:
isCraftable: 1
isBuildable: 0
craftingTime: 2.5
itemSounds: {fileID: 11400000, guid: bfb77f100ed7e6e4fb42c9b05bcb00af, type: 2}
registerInSN: 1
recipeSN: {fileID: 11400000, guid: 9636d4aefaf6b714fa2a2035b9ea0e0d, type: 2}
craftTreeTypeSN: 1
Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Ermfish/Erm.asset
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ MonoBehaviour:
isCraftable: 0
isBuildable: 1
craftingTime: 2.5
itemSounds: {fileID: 0}
registerInSN: 1
recipeSN: {fileID: 11400000, guid: 9ef8730f2922d8047bfb88e7e6edfebd, type: 2}
craftTreeTypeSN: 0
Expand Down
1 change: 1 addition & 0 deletions Unity/Assets/Ermfish/Ermfish regular.asset
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ MonoBehaviour:
isCraftable: 0
isBuildable: 0
craftingTime: 2.5
itemSounds: {fileID: 11400000, guid: bfb77f100ed7e6e4fb42c9b05bcb00af, type: 2}
registerInSN: 1
recipeSN: {fileID: 0}
craftTreeTypeSN: 0
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ MonoBehaviour:
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c0985a00ee6f8fe428ff0d0e09eba1b9, type: 3}
m_Name: Ermfish sounds
m_Script: {fileID: 11500000, guid: efd98f6d65dc4615a0a85770a69d8cef, type: 3}
m_Name: Ermfish Sounds
m_EditorClassIdentifier:
ambientItemSounds: {fileID: 11400000, guid: 3de2ffb6cd5e6404fb0bcdccfc44fa46, type: 2}
ambientWorldSounds: {fileID: 11400000, guid: 3de2ffb6cd5e6404fb0bcdccfc44fa46, type: 2}
pickupSounds: {fileID: 11400000, guid: f247414bf2966cf47aaafd9366ae6ca4, type: 2}
dropSounds: {fileID: 11400000, guid: 6eded4b83ed55cc4aac8329f5aaef703, type: 2}
drawSounds: {fileID: 11400000, guid: 80a13049ce8e7d04cabc4fd9c887c1d7, type: 2}
holsterSounds: {fileID: 11400000, guid: d105741eaefb1404bad1edcd3eb49376, type: 2}
cookSounds: {fileID: 11400000, guid: 5f9bffa48791b084487973927c5a3901, type: 2}
eatSounds: {fileID: 11400000, guid: 411f4ebbddbe2ea48b4129b7e8c80fab, type: 2}
hurtSounds: {fileID: 11400000, guid: 4f47d8b7127a0234ea057da7472d7808, type: 2}
attackSounds: {fileID: 0}
scanSounds: {fileID: 11400000, guid: 1138c2d796e81ac43a4f257daf9651b7, type: 2}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions Unity/Assets/Scripts/SCHIZO/Creatures/Ermshark/Ermshark.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using JetBrains.Annotations;
using NaughtyAttributes;
using SCHIZO.Creatures.Components;
using SCHIZO.Interop.Subnautica;
using SCHIZO.Attributes.Typing;
using SCHIZO.Sounds.Players;
using UnityEngine;
using WorldAmbientSoundPlayer = SCHIZO.Sounds.Players.WorldAmbientSoundPlayer;

namespace SCHIZO.Creatures.Ermshark
{
Expand Down
16 changes: 11 additions & 5 deletions Unity/Assets/Scripts/SCHIZO/Items/Data/ItemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SCHIZO.Items.Data.Crafting;
using SCHIZO.Registering;
using SCHIZO.Attributes.Visual;
using SCHIZO.Sounds;
using UnityEngine;
using UnityEngine.Serialization;
using static NaughtyAttributes.EConditionOperator;
Expand All @@ -26,21 +27,24 @@ public partial class ItemData : ModRegistryItem
[BoxGroup("TechType"), ResizableTextArea, ShowIf(nameof(ShowPickupableProps))]
public string tooltip;

[BoxGroup("Common Properties"), ShowIf(nameof(ShowPickupableProps)), Required]
[CommonData, ShowIf(nameof(ShowPickupableProps)), Required]
public Sprite icon;

[BoxGroup("Common Properties"), HideIf(Or, nameof(HidePickupableProps), nameof(isBuildable))]
[CommonData, HideIf(Or, nameof(HidePickupableProps), nameof(isBuildable))]
public Vector2Int itemSize = new Vector2Int(1, 1);

[BoxGroup("Common Properties"), HideIf(Or, nameof(HidePickupableProps), nameof(isBuildable))]
[CommonData, HideIf(Or, nameof(HidePickupableProps), nameof(isBuildable))]
public bool isCraftable;

[BoxGroup("Common Properties"), HideIf(Or, nameof(HidePickupableProps), nameof(isCraftable))]
[CommonData, HideIf(Or, nameof(HidePickupableProps), nameof(isCraftable))]
public bool isBuildable;

[BoxGroup("Common Properties"), ShowIf(nameof(IsActuallyCraftable))]
[CommonData, ShowIf(nameof(IsActuallyCraftable))]
public float craftingTime = 2.5f;

[CommonData, ShowIf(nameof(itemSounds_ShowIf))]
public ItemSounds itemSounds;

#region Subnautica Data

[SNData, Label("Register"), SerializeField]
Expand Down Expand Up @@ -126,6 +130,8 @@ public partial class ItemData : ModRegistryItem
private bool requiredForUnlockSN_ShowIf() => !unlockAtStartSN && IsBuildableOrCraftable();
private bool requiredForUnlockBZ_ShowIf() => !unlockAtStartBZ && IsBuildableOrCraftable();

private bool itemSounds_ShowIf() => ShowPickupableProps() && !isBuildable;

protected virtual bool ShowPickupableProps() => true;
private bool HidePickupableProps() => !ShowPickupableProps();

Expand Down
19 changes: 19 additions & 0 deletions Unity/Assets/Scripts/SCHIZO/Sounds/ItemSounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SCHIZO.Interop.NaughtyAttributes;
using SCHIZO.Sounds.Collections;
using UnityEngine;

namespace SCHIZO.Sounds
{
[CreateAssetMenu(menuName = "SCHIZO/Sounds/Item Sounds")]
public sealed partial class ItemSounds : NaughtyScriptableObject
{
public SoundCollection pickupSounds;
public SoundCollection dropSounds;

public SoundCollection drawSounds;
public SoundCollection holsterSounds;

public SoundCollection cookSounds;
public SoundCollection eatSounds;
}
}
3 changes: 3 additions & 0 deletions Unity/Assets/Scripts/SCHIZO/Sounds/ItemSounds.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using SoundPlayer = SCHIZO.Sounds.Players.SoundPlayer;

namespace SCHIZO.Creatures.Components
namespace SCHIZO.Sounds.Players
{
public sealed partial class HurtSoundPlayer : SoundPlayer
{
Expand Down

0 comments on commit 45785bc

Please sign in to comment.