// volume is 0-100 public void SetMusic(float volume) { foreach (var status in SurvivorsToTrack) { if (volume <= 0 && status.MusicPlaying) { // If it's NOT moving and the music IS playing, then PAUSE status.MusicPlaying = false; AkSoundEngine.PostEvent(BRIEF_RESPITE, status.ControllerGameObject); } else if (volume > 0 && !status.MusicPlaying) { // If it IS moving and the music is NOT playing, then RESUME (or start, if it hasn't been started yet) if (status.MusicStarted) { AkSoundEngine.PostEvent(KEEP_GOING, status.ControllerGameObject); } else { AkSoundEngine.PostEvent(START_RUNNING, status.ControllerGameObject); } status.MusicPlaying = true; } RtpcSetter gameParamSetter = new RtpcSetter("Speeds", status.ControllerGameObject) { value = volume }; gameParamSetter.FlushIfChanged(); } }
public void FixedUpdate() { // To avoid trying to remove while iterating through our list List <SurvivorStatus> toRemove = new List <SurvivorStatus>(); // I don't know 100% if this is needed, but I started getting // fewer errors with it in. int preRemoved = SurvivorsToTrack.RemoveAll((status) => status.Controller == null || !status.Controller.isConnected); foreach (var status in SurvivorsToTrack) { if (status.ControllerGameObject != null) { float speed = status.Speed; // Set the volume of the music to change depending on current speed // Formula is this: // speedItems * .4 + speed/12 + sqrt(speedItems)*speed/22 - 1 // // Explanation: // I wanted the following things to be true: // 1. Nothing would be heard under normal circumstances with 0 speed items. // 2. Volume would scale off of a combination of speedItems and speed // 3. Volume would not fluctuate very noticeably as the turret slowed down/sped up. // // After some tweaking, I ended up preferring a mix of additive and multiplicitave scaling. // The -1 at the end helps increase the number of speedItems you need before you hear anything. float newVolumeModifier = status.NumSpeedItems * 0.4f + speed / 12 + ((float)Math.Sqrt(status.NumSpeedItems) * speed / 16) - 1; RtpcSetter gameParamSetter = new RtpcSetter("Speeds", status.ControllerGameObject) { value = newVolumeModifier }; gameParamSetter.FlushIfChanged(); // Getting item counts from the inventory every update is probably less than optimal, but I'm too lazy to do better float moveSpeedThreshold = MoveSpeedThreshold(status.Inventory.GetItemCount(ItemIndex.Hoof), status.Inventory.GetItemCount(ItemIndex.SprintOutOfCombat), status.Controller.networkUser.GetCurrentBody().outOfCombat); if (speed <= moveSpeedThreshold && status.MusicPlaying) { // If it's NOT moving and the music IS playing, then PAUSE status.MusicPlaying = false; AkSoundEngine.PostEvent(BRIEF_RESPITE, status.ControllerGameObject); } else if (speed > moveSpeedThreshold && !status.MusicPlaying) { // If it IS moving and the music is NOT playing, then RESUME (or start, if it hasn't been started yet) if (status.MusicStarted) { AkSoundEngine.PostEvent(KEEP_GOING, status.ControllerGameObject); } else { AkSoundEngine.PostEvent(START_RUNNING, status.ControllerGameObject); } status.MusicPlaying = true; } // The last thing we want to do, prep for next update status.RecordLastPosition(); } } }