{"TotalCount":5,"Files":[{"Ident":"facepunch.dspcomponents","Path":"Code/VolumeSystem/VolumeSystem.cs","FileName":"VolumeSystem.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":58614,"IsPrivate":false,"Code":"using System.Collections.Generic;\r\nusing System.Linq;\r\n\r\nnamespace Sandbox.Volumes;\r\n\r\n/// \u003Csummary\u003E\r\n/// A base GameObjectSystem for handling of VolumeComponents. The idea is that you\u0027re going to have\r\n/// a custom VolumeComponent, and register your volumes in a VolumeGameObjectSystem derived GameObjectSystem.\r\n/// This system\u0027s responsibility is primarily to store volumes and make them searchable.\r\n/// \u003C/summary\u003E\r\npublic abstract class VolumeSystem\u003CT\u003E : GameObjectSystem where T : VolumeComponent\r\n{\r\n\tpublic VolumeSystem( Scene scene ) : base( scene )\r\n\t{\r\n\r\n\t}\r\n\r\n\tHashSet\u003CT\u003E volumes = new HashSet\u003CT\u003E();\r\n\r\n\tpublic void Add( T volume )\r\n\t{\r\n\t\tvolumes.Add( volume );\r\n\t}\r\n\r\n\tpublic void Remove( T volume )\r\n\t{\r\n\t\tvolumes.Remove( volume );\r\n\t}\r\n\r\n\tpublic T FindVolume( Vector3 position )\r\n\t{\r\n\t\treturn FindAll( position ).FirstOrDefault();\r\n\t}\r\n\r\n\tpublic IEnumerable\u003CT\u003E FindAll( Vector3 position )\r\n\t{\r\n\t\tforeach ( var volume in volumes )\r\n\t\t{\r\n\t\t\tif ( !volume.SceneVolume.Test( volume.Transform.World, position ) )\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\tyield return volume;\r\n\t\t}\r\n\r\n\t}\r\n}\r\n"},{"Ident":"facepunch.dspcomponents","Path":"Code/VolumeSystem/VolumeComponent.cs","FileName":"VolumeComponent.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":58614,"IsPrivate":false,"Code":"namespace Sandbox.Volumes;\r\n\r\npublic class VolumeComponent : Component\r\n{\r\n\t[InlineEditor]\r\n\t[Property] public SceneVolume SceneVolume { get; set; } = new SceneVolume();\r\n\r\n\tprotected override void DrawGizmos()\r\n\t{\r\n\t\tbase.DrawGizmos();\r\n\r\n\t\tif ( !Gizmo.IsSelected )\r\n\t\t\treturn;\r\n\r\n\t\tvar vol = SceneVolume;\r\n\t\tvol.DrawGizmos( true );\r\n\t\tSceneVolume = vol;\r\n\t}\r\n\r\n\tpublic virtual float GetPriority()\r\n\t{\r\n\t\t// higher number is better, smaller volume is better\r\n\t\treturn 1.0f - (SceneVolume.GetVolume() / 16000000000f);\r\n\t}\r\n}\r\n"},{"Ident":"facepunch.dspcomponents","Path":"Code/DspVolumeGameSystem.cs","FileName":"DspVolumeGameSystem.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":58614,"IsPrivate":false,"Code":"using Sandbox;\r\nusing Sandbox.Audio;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\npublic class DspVolumeGameSystem : Sandbox.Volumes.VolumeSystem\u003CDspVolume\u003E\r\n{\r\n\tpublic DspVolumeGameSystem( Scene scene ) : base( scene )\r\n\t{\r\n\t\tListen( Stage.FinishUpdate, 0, Update, \u0022Dsp Update\u0022 );\r\n\t}\r\n\r\n\tpublic override void Dispose()\r\n\t{\r\n\t\tbase.Dispose();\r\n\r\n\t\tvar gameMixer = Mixer.FindMixerByName( \u0022Game\u0022 );\r\n\t\tif ( gameMixer is null ) return;\r\n\r\n\t\tforeach ( var processor in _entries.Values )\r\n\t\t{\r\n\t\t\tgameMixer.RemoveProcessor( processor.processor );\r\n\t\t}\r\n\t}\r\n\r\n\tHashSet\u003Cstring\u003E _active { get; set; }\r\n\r\n\trecord class Entry( DspProcessor processor, bool active );\r\n\tDictionary\u003Cstring, Entry\u003E _entries = new();\r\n\r\n\tvoid Update()\r\n\t{\r\n\t\tvar gameMixer = Mixer.FindMixerByName( \u0022Game\u0022 );\r\n\t\tif ( gameMixer is null ) return;\r\n\r\n\r\n\t\tfloat lastPriority = 0;\r\n\t\tstring found = default;\r\n\r\n\t\tforeach ( var volume in FindAll( Sound.Listener.Position ) )\r\n\t\t{\r\n\t\t\tfloat priority = volume.GetPriority();\r\n\r\n\t\t\tif ( priority \u003C lastPriority )\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\tlastPriority = priority;\r\n\t\t\tfound = volume.Dsp.Name;\r\n\t\t}\r\n\r\n\t\tif ( !string.IsNullOrWhiteSpace( found ) \u0026\u0026 !_entries.ContainsKey( found ) )\r\n\t\t{\r\n\t\t\tvar processor = new DspProcessor();\r\n\r\n\t\t\tprocessor.Effect = found;\r\n\t\t\tprocessor.Mix = 0;\r\n\r\n\t\t\tgameMixer.AddProcessor( processor );\r\n\t\t\t_entries[found] = new Entry( processor, true );\r\n\t\t}\r\n\r\n\t\tforeach ( var entry in _entries )\r\n\t\t{\r\n\t\t\tif ( found == entry.Key )\r\n\t\t\t{\r\n\t\t\t\tentry.Value.processor.Mix = entry.Value.processor.Mix.Approach( 1.0f, Time.Delta * 2.0f );\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tentry.Value.processor.Mix = entry.Value.processor.Mix.Approach( 0.0f, Time.Delta * 2.0f );\r\n\t\t\t}\r\n\t\t}\r\n\r\n\r\n\t\tforeach ( var entry in _entries.Where( x =\u003E x.Value.processor.Mix \u003C= 0 ).ToArray() )\r\n\t\t{\r\n\t\t\tgameMixer.RemoveProcessor( entry.Value.processor );\r\n\t\t\t_entries.Remove( entry.Key );\r\n\t\t}\r\n\r\n\r\n\r\n\t}\r\n\r\n\tprivate void TryAdd( string name )\r\n\t{\r\n\t\tif ( _active.Contains( name ) ) return;\r\n\t\t_active.Add( name );\r\n\t}\r\n\r\n\tprivate void TryRemove( string name )\r\n\t{\r\n\t\tif ( !_active.Contains( name ) ) return;\r\n\t\t_active.Remove( name );\r\n\r\n\r\n\t}\r\n}\r\n"},{"Ident":"facepunch.dspcomponents","Path":"Code/DspVolume.cs","FileName":"DspVolume.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":58614,"IsPrivate":false,"Code":"using Sandbox;\r\nusing Sandbox.Audio;\r\n\r\n[Title( \u0022Dsp Volume\u0022 )]\r\n[Group( \u0022Audio\u0022 )]\r\npublic class DspVolume : Sandbox.Volumes.VolumeComponent\r\n{\r\n\t[Property]\r\n\tpublic DspPresetHandle Dsp { get; set; }\r\n\r\n\t[Property]\r\n\tpublic MixerHandle TargetMixer { get; set; } = new MixerHandle { Name = \u0022Game\u0022 };\r\n\r\n\t[Property]\r\n\tpublic int Priority { get; set; }\r\n\r\n\r\n\tprotected override void OnEnabled()\r\n\t{\r\n\t\tbase.OnEnabled();\r\n\r\n\t\tScene.GetSystem\u003CDspVolumeGameSystem\u003E().Add( this );\r\n\t}\r\n\r\n\tprotected override void OnDisabled()\r\n\t{\r\n\t\tbase.OnDisabled();\r\n\r\n\t\tScene.GetSystem\u003CDspVolumeGameSystem\u003E().Remove( this );\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Prefer higher priority volumes, and if priorities are the same, we prefer smaller volumes.\r\n\t/// \u003C/summary\u003E\r\n\tpublic override float GetPriority()\r\n\t{\r\n\t\treturn Priority \u002B base.GetPriority();\r\n\t}\r\n}\r\n\r\n"},{"Ident":"facepunch.dspcomponents","Path":"Code/VolumeSystem/SceneVolume.cs","FileName":"SceneVolume.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":58614,"IsPrivate":false,"Code":"using System.Text.Json.Serialization;\r\n\r\nnamespace Sandbox.Volumes;\r\n\r\n/// \u003Csummary\u003E\r\n/// A generic way to represent volumes in a scene. If we all end up using this instead of defining our own version\r\n/// in everything, we can improve this and improve everything at the same time.\r\n/// \u003C/summary\u003E\r\npublic struct SceneVolume\r\n{\r\n\tpublic SceneVolume()\r\n\t{\r\n\t}\r\n\r\n\tpublic enum VolumeTypes\r\n\t{\r\n\t\t/// \u003Csummary\u003E\r\n\t\t/// A sphere. It\u0027s like the earth. Or an eyeball.\r\n\t\t/// \u003C/summary\u003E\r\n\t\tSphere,\r\n\r\n\t\t/// \u003Csummary\u003E\r\n\t\t/// A box, like a cube.\r\n\t\t/// \u003C/summary\u003E\r\n\t\tBox\r\n\t}\r\n\r\n\t[JsonInclude]\r\n\tpublic VolumeTypes Type = VolumeTypes.Box;\r\n\r\n\t[JsonInclude]\r\n\t[ShowIf( \u0022Type\u0022, VolumeTypes.Sphere )]\r\n\tpublic Sphere Sphere = new Sphere( 0, 10 );\r\n\r\n\t[JsonInclude]\r\n\t[ShowIf( \u0022Type\u0022, VolumeTypes.Box )]\r\n\tpublic BBox Box = BBox.FromPositionAndSize( 0, 100 );\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Draws an editable sphere/box gizmo, for adjusting the volume\r\n\t/// \u003C/summary\u003E\r\n\tpublic void DrawGizmos( bool withControls )\r\n\t{\r\n\t\tif ( Type == VolumeTypes.Sphere )\r\n\t\t{\r\n\t\t\tif ( withControls )\r\n\t\t\t{\r\n\t\t\t\tGizmo.Control.Sphere( \u0022Volume\u0022, Sphere.Radius, out Sphere.Radius, Color.Yellow );\r\n\t\t\t}\r\n\r\n\t\t\tGizmo.Draw.IgnoreDepth = false;\r\n\t\t\tGizmo.Draw.Color = Gizmo.Colors.Blue.WithAlpha( 0.8f );\r\n\t\t\tGizmo.Draw.LineSphere( Sphere );\r\n\r\n\t\t\tGizmo.Draw.IgnoreDepth = true;\r\n\t\t\tGizmo.Draw.Color = Color.White.WithAlpha( 0.05f );\r\n\t\t\tGizmo.Draw.LineSphere( Sphere );\r\n\t\t}\r\n\r\n\t\tif ( Type == VolumeTypes.Box )\r\n\t\t{\r\n\t\t\tGizmo.Draw.IgnoreDepth = false;\r\n\r\n\t\t\tif ( withControls )\r\n\t\t\t{\r\n\t\t\t\tGizmo.Control.BoundingBox( \u0022Volume\u0022, Box, out Box );\r\n\t\t\t}\r\n\r\n\t\t\tGizmo.Draw.Color = Gizmo.Colors.Blue.WithAlpha( 0.8f );\r\n\t\t\tGizmo.Draw.LineBBox( Box );\r\n\r\n\t\t\tGizmo.Draw.IgnoreDepth = true;\r\n\t\t\tGizmo.Draw.Color = Color.White.WithAlpha( 0.05f );\r\n\t\t\tGizmo.Draw.LineBBox( Box );\r\n\t\t}\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Is this point within the volume\r\n\t/// \u003C/summary\u003E\r\n\tpublic bool Test( in Transform volumeTransform, in Vector3 position )\r\n\t{\r\n\t\treturn Test( volumeTransform.PointToLocal( position ) );\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Is this point within the (local space) volume\r\n\t/// \u003C/summary\u003E\r\n\tpublic bool Test( in Vector3 position )\r\n\t{\r\n\t\tif ( Type == VolumeTypes.Sphere )\r\n\t\t{\r\n\t\t\treturn Sphere.Contains( position );\r\n\t\t}\r\n\r\n\t\tif ( Type == VolumeTypes.Box )\r\n\t\t{\r\n\t\t\treturn Box.Contains( position );\r\n\t\t}\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Get the actual amount of volume in this shape. This is useful if you want to make\r\n\t/// a system where you prioritize by volume size. Don\u0027t forget to multiply by scale!\r\n\t/// \u003C/summary\u003E\r\n\tpublic float GetVolume()\r\n\t{\r\n\t\tif ( Type == VolumeTypes.Sphere )\r\n\t\t{\r\n\t\t\treturn Sphere.GetVolume();\r\n\t\t}\r\n\r\n\t\tif ( Type == VolumeTypes.Box )\r\n\t\t{\r\n\t\t\treturn Box.GetVolume();\r\n\t\t}\r\n\r\n\t\treturn 0.0f;\r\n\t}\r\n}\r\n"}]}