Advertisement
xMissCorielx

Tractor Sound

Jul 15th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using StardewModdingAPI;
  7. using StardewValley;
  8. using StardewModdingAPI.Enums;
  9. using StardewModdingAPI.Utilities;
  10. using System.Threading;
  11. using Microsoft.Xna.Framework.Audio;
  12. using Microsoft.Xna.Framework;
  13. using StardewValley.Characters;
  14. using System.IO;
  15. using StardewModdingAPI.Events;
  16.  
  17. namespace Tractor_Sound_Complete
  18. {
  19.     public class ModEntry : Mod
  20.     {
  21.         public static SoundEffect tractorEffect;
  22.         public static ActiveMusic tractorIdle;
  23.         public override void Entry(IModHelper helper)
  24.         {
  25.             using (FileStream stream = new FileStream(Path.Combine(Helper.DirectoryPath, "tractorEngine.wav"), FileMode.Open))
  26.                 tractorEffect = SoundEffect.FromStream(stream);
  27.             helper.Events.Player.Warped += Player_Warped;
  28.             helper.Events.GameLoop.UpdateTicked += UpdateTicked;
  29.         }
  30.         private void UpdateTicked(object sender, UpdateTickedEventArgs e)
  31.         {
  32.         }
  33.         private void Player_Warped(object sender, StardewModdingAPI.Events.WarpedEventArgs e)
  34.         {
  35.             if (!e.Player.isRidingHorse() || !e.Player.horseName.Value.ToLower().Contains("tractor"))
  36.                 tractorIdle?.Dispose();
  37.             if (e.NewLocation?.characters.FirstOrDefault(character => character is Horse && character.Name.ToLower().Contains("tractor")) is Horse tractor)
  38.             {
  39.                 Vector2 tractorPosition = tractor.getTileLocation();
  40.                 tractorIdle = new ActiveMusic("tractorEngine.wav", tractorEffect.CreateInstance(), true, true, tractor, 0.01f, 1f, 50);
  41.                 tractorIdle.Play();
  42.                 Monitor.Log("Found Tractor at " + tractor.position.X + " - " + tractor.position.Y, LogLevel.Warn);
  43.             }
  44.             else
  45.             {
  46.                 Monitor.Log("Tractor Not Found", LogLevel.Warn);
  47.             }
  48.         }
  49.         public class ActiveMusic : IDisposable
  50.         {
  51.             public string Id { get; set; }
  52.             public SoundEffectInstance Sound { get; set; } = null;
  53.             public bool Ambient { get; set; } = false;
  54.             public bool IsPlaying { get; set; } = false;
  55.             public bool Loop { get; set; } = false;
  56.             public Thread UpdateThread { get; set; }
  57.  
  58.             private Horse Tractor { get; }
  59.             public AudioEmitter Emitter { get; set; } = null;
  60.             public AudioListener Listener { get; set; } = null;
  61.  
  62.             public bool IsEmitter { get; set; } = false;
  63.  
  64.             public float Distance { get; set; } = 1;
  65.             public float Volume { get; set; } = 1;
  66.  
  67.             public int MaxDistance { get; set; } = 100;
  68.  
  69.             public Vector2 EmitterTile { get; set; } = Vector2.Zero;
  70.  
  71.  
  72.             public ActiveMusic(string id, SoundEffectInstance sound, bool ambient, bool loop)
  73.             {
  74.                 this.Id = id;
  75.                 this.Sound = sound;
  76.                 this.Ambient = ambient;
  77.                 this.Loop = loop;
  78.                 Sound.IsLooped = loop;
  79.                 Play();
  80.             }
  81.  
  82.             public ActiveMusic(string id, SoundEffectInstance sound, bool ambient, bool loop, Horse tractor, float distance, float volume, int maxDistance)
  83.             {
  84.                 //This kills the crab
  85.                 this.Tractor = tractor;
  86.                 this.Id = id;
  87.                 this.Sound = sound;
  88.                 this.Ambient = ambient;
  89.                 this.Loop = loop;
  90.                 Sound.IsLooped = loop;
  91.                 Emitter = new AudioEmitter();
  92.                 Distance = distance;
  93.                 EmitterTile = Tractor.getTileLocation();
  94.                 var ePos = (EmitterTile * new Vector2(Game1.tileSize, Game1.tileSize)) + new Vector2(Game1.tileSize / 2f, Game1.tileSize / 2f);
  95.                 Emitter.Position = new Vector3(0, ePos.X * distance, ePos.Y * distance); ;
  96.                 Listener = new AudioListener();
  97.                 IsEmitter = true;
  98.                 Volume = volume;
  99.                 MaxDistance = maxDistance * maxDistance;
  100.                 Play();
  101.             }
  102.             public void SetVolume(float volume)
  103.             {
  104.                 try
  105.                 {
  106.  
  107.  
  108.                     if (Sound is SoundEffectInstance instance && !instance.IsDisposed)
  109.                     {
  110.                         instance.Volume = Math.Max(0, Math.Min(volume, 1));
  111.                         if (IsEmitter)
  112.                         {
  113.                             bool changedPosition = false;
  114.  
  115.                             if (EmitterTile != Tractor.getTileLocation())
  116.                             {
  117.                                 changedPosition = true;
  118.                                 EmitterTile = Tractor.getTileLocation();
  119.                                 var ePos = (EmitterTile * new Vector2(Game1.tileSize, Game1.tileSize)) + new Vector2(Game1.tileSize / 2f, Game1.tileSize / 2f);
  120.                                 Emitter.Position = new Vector3(0, ePos.X * Distance, ePos.Y * Distance); ;
  121.                             }
  122.  
  123.                             var position = Game1.player.Position;
  124.                             var t = new Vector3(0, position.X * Distance, position.Y * Distance);
  125.                             if (changedPosition || t != Listener.Position)
  126.                             {
  127.                                 Listener.Position = t;
  128.                                 Sound?.Apply3D(Listener, Emitter);
  129.                             }
  130.                         }
  131.  
  132.                     }
  133.                 }
  134.                 catch
  135.                 {
  136.  
  137.                 }
  138.             }
  139.  
  140.             public void Stop()
  141.             {
  142.                 try
  143.                 {
  144.                     IsPlaying = false;
  145.                     Sound?.Stop();
  146.                 }
  147.                 catch
  148.                 {
  149.                 }
  150.             }
  151.  
  152.             public void Play()
  153.             {
  154.                 if (IsEmitter)
  155.                 {
  156.                     var position = Game1.player.Position;
  157.                     Listener.Position = new Vector3(0, position.X * Distance, position.Y * Distance);
  158.                     Sound?.Apply3D(new AudioListener[] { Listener }, Emitter);
  159.                 }
  160.  
  161.                 Sound?.Play();
  162.                 IsPlaying = true;
  163.  
  164.                 UpdateThread = new Thread(Update);
  165.                 UpdateThread.Start();
  166.             }
  167.  
  168.             public void Dispose()
  169.             {
  170.                 Stop();
  171.             }
  172.  
  173.             public void Update()
  174.             {
  175.  
  176.                 try
  177.                 {
  178.                     while (!Sound.IsDisposed && IsPlaying && Sound.State == SoundState.Playing)
  179.                     {
  180.                         if (IsEmitter)
  181.                         {
  182.                             var position = Game1.player.Position;
  183.                             var t = new Vector3(0, position.X * Distance, position.Y * Distance);
  184.                             if (t != Listener.Position)
  185.                             {
  186.                                 Listener.Position = t;
  187.                                 Sound?.Apply3D(Listener, Emitter);
  188.                             }
  189.                         }
  190.  
  191.                         float mainvol = (Ambient ? Game1.ambientPlayerVolume : Game1.musicPlayerVolume);
  192.                         float optionsvol = (Ambient ? Game1.options.ambientVolumeLevel : Game1.options.musicVolumeLevel);
  193.  
  194.                         if (IsEmitter && MaxDistance < GetSquaredDistance(Game1.player.getTileLocation(), EmitterTile))
  195.                             optionsvol = 0f;
  196.  
  197.                         SetVolume(Math.Min(optionsvol, mainvol));
  198.  
  199.                         Thread.Sleep(1);
  200.                     }
  201.                 }
  202.                 catch (Exception e)
  203.                 {
  204.                    
  205.                 }
  206.  
  207.                 IsPlaying = false;
  208.             }
  209.  
  210.             public static float GetSquaredDistance(Vector2 point1, Vector2 point2)
  211.             {
  212.                 float a = (point1.X - point2.X);
  213.                 float b = (point1.Y - point2.Y);
  214.                 return (a * a) + (b * b);
  215.             }
  216.            
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement