Skip to content

Commit

Permalink
Fix #382: add sounds for food/drink items
Browse files Browse the repository at this point in the history
-also have squeakers not activate on grab
-also add ouch sound
  • Loading branch information
JonnyOThan committed Nov 11, 2024
1 parent 0d4afd1 commit 26e7d3f
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 20 deletions.
14 changes: 14 additions & 0 deletions FreeIva/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace FreeIva
{
Expand Down Expand Up @@ -36,5 +37,18 @@ public static T GetModule<T>(this Part part) where T : PartModule
{
return (T)GetModule(part, typeof(T));
}

public static AudioSource PlayUnspatializedClip(this AudioClip clip, float volume = 1.0f)
{
GameObject gameObject = new GameObject("One shot audio");
AudioSource audioSource = (AudioSource)gameObject.AddComponent(typeof(AudioSource));
audioSource.clip = clip;
audioSource.spatialBlend = 0f;
audioSource.spatialize = false;
audioSource.volume = volume;
audioSource.Play();
GameObject.Destroy(gameObject, clip.length * ((Time.timeScale < 0.01f) ? 0.01f : Time.timeScale));
return audioSource;
}
}
}
59 changes: 39 additions & 20 deletions FreeIva/InternalModules/PhysicalProp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class PhysicalProp : InternalModule

internal static PhysicalProp HeldProp { get; private set; }

private string DebugName => internalProp.hasModel ? internalProp.propName : (internalModel.internalName + "." + transformName);

public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
Expand All @@ -70,8 +72,6 @@ public override void OnLoad(ConfigNode node)
var colliderNode = node.GetNode("Collider");
if (colliderNode != null)
{
string dbgName = internalProp.hasModel ? internalProp.propName : internalModel.internalName;

Transform colliderTransform;
var transformName = colliderNode.GetValue("parentTransformName");
if (transformName != null)
Expand All @@ -85,7 +85,7 @@ public override void OnLoad(ConfigNode node)

if (colliderTransform != null)
{
m_collider = ColliderUtil.CreateCollider(colliderTransform, colliderNode, dbgName);
m_collider = ColliderUtil.CreateCollider(colliderTransform, colliderNode, DebugName);
}
}
else if (transformName != string.Empty)
Expand Down Expand Up @@ -114,7 +114,7 @@ public override void OnLoad(ConfigNode node)
}
else
{
Log.Error($"PhysicalProp: prop {internalProp.propName} does not have a collider");
Log.Error($"PhysicalProp: prop {DebugName} does not have a collider");
}

if (m_collider != null)
Expand All @@ -124,16 +124,6 @@ public override void OnLoad(ConfigNode node)
m_stickAudioClip = LoadAudioClip(node, "stickSound");
m_impactAudioClip = LoadAudioClip(node, "impactSound");

if (m_grabAudioClip != null || m_stickAudioClip != null || m_impactAudioClip != null)
{
m_audioSource = m_collider.gameObject.AddComponent<AudioSource>();
m_audioSource.volume = GameSettings.SHIP_VOLUME;
m_audioSource.minDistance = 2;
m_audioSource.maxDistance = 10;
m_audioSource.playOnAwake = false;
m_audioSource.spatialize = true;
}

// setup interactions (requires a collider)
var interactionNode = node.GetNode("Interaction");
if (interactionNode != null)
Expand Down Expand Up @@ -251,7 +241,23 @@ public AudioClip LoadAudioClip(ConfigNode node, string key)

if (result == null)
{
Log.Error($"Failed to find audio clip {clipUrl} for prop {internalProp.propName}");
Log.Error($"Failed to find audio clip {clipUrl} for prop {DebugName}");
}
else if (m_audioSource == null)
{
if (m_collider != null)
{
m_audioSource = m_collider.gameObject.AddComponent<AudioSource>();
m_audioSource.volume = GameSettings.SHIP_VOLUME;
m_audioSource.minDistance = 2;
m_audioSource.maxDistance = 10;
m_audioSource.playOnAwake = false;
m_audioSource.spatialize = true;
}
else
{
Log.Error($"Loaded audio clip {clipUrl} for prop {DebugName} but it does not have a collider!");
}
}

return result;
Expand Down Expand Up @@ -542,22 +548,35 @@ public override void OnImpact(float magnitude)

public class InteractionSqueak : Interaction
{
[SerializeReference] AudioClip m_squeakSound;
[SerializeReference] AudioClip squeakSound;

public override void OnLoad(ConfigNode interactionNode)
{
base.OnLoad(interactionNode);
m_squeakSound = PhysicalProp.LoadAudioClip(interactionNode, "squeakSound");
squeakSound = PhysicalProp.LoadAudioClip(interactionNode, nameof(squeakSound));
}

public override void OnGrab()
public override void StartInteraction()
{
PhysicalProp.PlayAudioClip(squeakSound);
}
}

public class InteractionConsume : Interaction
{
[SerializeReference] AudioClip consumeSound;

public override void OnLoad(ConfigNode interactionNode)
{
PhysicalProp.PlayAudioClip(m_squeakSound);
base.OnLoad(interactionNode);
consumeSound = PhysicalProp.LoadAudioClip(interactionNode, nameof(consumeSound));
}

public override void StartInteraction()
{
PhysicalProp.PlayAudioClip(m_squeakSound);
consumeSound.PlayUnspatializedClip();
GameObject.Destroy(PhysicalProp.rigidBodyObject);
HeldProp = null;
}
}

Expand Down
12 changes: 12 additions & 0 deletions GameData/FreeIva/ASET/ASET_PhysicalProps.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@
}
}

@PROP[BeveragePackage_*|MAS_Beverage_*]:HAS[@MODULE[PhysicalProp]:HAS[!Interaction]]:FOR[FreeIva]
{
@MODULE[PhysicalProp]
{
Interaction
{
name = InteractionConsume
consumeSound = FreeIva/Sounds/drink01
}
}
}

@PROP[ASET_RecoveryBeacon|MAS_RecoveryBeacon]:HAS[!MODULE[PhysicalProp]]:FOR[FreeIva]
{
MODULE
Expand Down
16 changes: 16 additions & 0 deletions GameData/FreeIva/Knes/Knes_PhysicalProps.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
dimensions = 0.1, 0.04, 0.1
center = 0, 0, 0
}

Interaction
{
name = InteractionConsume
consumeSound = FreeIva/Sounds/food01
}
}
}

Expand Down Expand Up @@ -100,6 +106,11 @@
dimensions = 0.062, 0.163, 0.012
center = 0, 0, 0
}
Interaction
{
name = InteractionConsume
consumeSound = FreeIva/Sounds/food01
}
}
}

Expand All @@ -117,5 +128,10 @@
dimensions = 0.071, 0.167, 0.064
center = 0, 0, 0
}
Interaction
{
name = InteractionConsume
consumeSound = FreeIva/Sounds/food01
}
}
}
5 changes: 5 additions & 0 deletions GameData/FreeIva/MiningExpansion/SMXPotatoHabInternal.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,10 @@
center = -0.921, 0.218, 0.903
dimensions = 0.082, 0.081, 0.106
}
Interaction
{
name = InteractionSqueak
squeakSound = FreeIva/Sounds/ouch01
}
}
}
5 changes: 5 additions & 0 deletions GameData/FreeIva/NearFutureProps/NF_PhysicalProps.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@
center = 0, 0, 0
dimensions = 0.105, 0.066, 0.078
}
Interaction
{
name = InteractionSqueak
squeakSound = FreeIva/Sounds/drink01
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions GameData/FreeIva/ProbeControlRoom/MissionControl.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
dimensions = 0.2, 0.227, 0.2
center = 0, 0.014, -0.087
}
Interaction
{
name = InteractionSqueak
squeakSound = FreeIva/Sounds/drink01
}
}
}

3 changes: 3 additions & 0 deletions GameData/FreeIva/Sounds/LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ https://freesound.org/people/mushroomjesus/sounds/141709/
http://creativecommons.org/publicdomain/zero/1.0/

synth.wav
drink01.wav
food01.wav
ouch01.wav
original recording by JonnyOThan
http://creativecommons.org/publicdomain/zero/1.0/

Expand Down
Binary file added GameData/FreeIva/Sounds/drink01.wav
Binary file not shown.
Binary file added GameData/FreeIva/Sounds/food01.wav
Binary file not shown.
Binary file added GameData/FreeIva/Sounds/ouch01.wav
Binary file not shown.
15 changes: 15 additions & 0 deletions GameData/FreeIva/WildBlueIndustries/WildBlueProps.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
dimensions = 0.2, 0.55, 0.2
center = 0.075, -0.665, 0.075
}
Interaction
{
name = InteractionSqueak
squeakSound = FreeIva/Sounds/ouch01
}
}
}

Expand Down Expand Up @@ -167,6 +172,11 @@
dimensions = 0.2, 0.155, 0.2
center = 0.074, -0.863, 0.078
}
Interaction
{
name = InteractionConsume
squeakSound = FreeIva/Sounds/food01
}
}
}

Expand Down Expand Up @@ -245,6 +255,11 @@
dimensions = 0.039, 0.1, 0.1
center = 0, 0, 0
}
Interaction
{
name = InteractionConsume
squeakSound = FreeIva/Sounds/food01
}
}
}

Expand Down

0 comments on commit 26e7d3f

Please sign in to comment.