Advertisement
jarppaaja

SoundHelper

Jun 12th, 2019
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. // SpeakSentry.cs "$Revision: 2301 $" "$Date: 2019-06-13 15:49:20 +0300 (to, 13 kesä 2019) $"
  2. using System.Linq;
  3.  
  4. namespace Turbo.Plugins.User
  5. {
  6.     public class SpeakEntry
  7.     {
  8.         public readonly string Text;
  9.         public readonly bool BeSilentInTown;
  10.         public readonly bool BeSilentWhenDead;
  11.         public readonly bool SpeakOnlyWithEnemies;
  12.         public readonly double VolumeMultiplier;
  13.         public readonly int Priority;
  14.         public bool CanSpeak => !string.IsNullOrEmpty(Text);
  15.         public bool IsPriorityOverride(SpeakEntry other) => Priority < other.Priority;
  16.  
  17.         public SpeakEntry(string text, bool beSilentInTown = true, bool beSilentWhenDead = true, bool speakOnlyWithEnemies = true, double volumeMultiplier = 1.0, int priority = 100)
  18.         {
  19.             Text = text;
  20.             BeSilentInTown = beSilentInTown;
  21.             BeSilentWhenDead = beSilentWhenDead;
  22.             SpeakOnlyWithEnemies = speakOnlyWithEnemies;
  23.             VolumeMultiplier = volumeMultiplier;
  24.             Priority = priority;
  25.         }
  26.     }
  27.  
  28.     public class SoundHelper
  29.     {
  30.         private readonly IController Hud;
  31.         private SpeakEntry lastSpeak = new SpeakEntry("");
  32.  
  33.         public SoundHelper(IController hud)
  34.         {
  35.             Hud = hud;
  36.         }
  37.  
  38.         public void Speak(SpeakEntry entry)
  39.         {
  40.             if (string.IsNullOrEmpty(entry.Text))
  41.                 return;
  42.             if (entry.BeSilentInTown && Hud.Game.IsInTown)
  43.                 return;
  44.             if (entry.BeSilentWhenDead && Hud.Game.Me.IsDead)
  45.                 return;
  46.             if (entry.SpeakOnlyWithEnemies && !Hud.Game.AliveMonsters.Any())
  47.                 return;
  48.             // Save volume.
  49.             var restoreVolume = false;
  50.             var VolumeMultiplier = Hud.Sound.VolumeMultiplier;
  51.             var VolumeMode = Hud.Sound.VolumeMode;
  52.             if (entry.VolumeMultiplier > 0 && (VolumeMultiplier != entry.VolumeMultiplier || VolumeMode != VolumeMode.AutoMaster))
  53.             {
  54.                 restoreVolume = true;
  55.                 Hud.Sound.VolumeMultiplier = entry.VolumeMultiplier;
  56.                 if (Hud.Sound.VolumeMode != VolumeMode.AutoMaster)
  57.                 {
  58.                     Hud.Sound.VolumeMode = VolumeMode.AutoMaster;
  59.                 }
  60.             }
  61.             lastSpeak = entry;
  62.             if (entry.IsPriorityOverride(lastSpeak))
  63.             {
  64.                 // If priority is less, override pervious speak if it is still playing.
  65.                 Hud.Sound.StopSpeak();
  66.             }
  67.             Hud.Sound.Speak(entry.Text);
  68.             // Restore volume.
  69.             if (restoreVolume)
  70.             {
  71.                 Hud.Sound.VolumeMultiplier = VolumeMultiplier;
  72.                 Hud.Sound.VolumeMode = VolumeMode;
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement