|
| 1 | +using Celeste.Mod.Entities; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +namespace Celeste.Mod.SpringCollab2020.Triggers { |
| 6 | + [CustomEntity("SpringCollab2020/SpeedBasedMusicParamTrigger")] |
| 7 | + class SpeedBasedMusicParamTrigger : Trigger { |
| 8 | + public static void Load() { |
| 9 | + On.Celeste.Player.Update += onPlayerUpdate; |
| 10 | + } |
| 11 | + |
| 12 | + public static void Unload() { |
| 13 | + On.Celeste.Player.Update -= onPlayerUpdate; |
| 14 | + } |
| 15 | + |
| 16 | + private static void onPlayerUpdate(On.Celeste.Player.orig_Update orig, Player self) { |
| 17 | + orig(self); |
| 18 | + |
| 19 | + AudioState audio = self.SceneAs<Level>().Session.Audio; |
| 20 | + float playerSpeed = self.Speed.Length(); |
| 21 | + |
| 22 | + // set all the speed-based music params to their corresponding values. |
| 23 | + foreach (KeyValuePair<string, SpringCollab2020Session.SpeedBasedMusicParamInfo> musicParam |
| 24 | + in SpringCollab2020Module.Instance.Session.ActiveSpeedBasedMusicParams) { |
| 25 | + |
| 26 | + audio.Music.Param(musicParam.Key, MathHelper.Clamp(playerSpeed, musicParam.Value.MinimumSpeed, musicParam.Value.MaximumSpeed)); |
| 27 | + } |
| 28 | + |
| 29 | + audio.Apply(); |
| 30 | + } |
| 31 | + |
| 32 | + private string paramName; |
| 33 | + private float minSpeed; |
| 34 | + private float maxSpeed; |
| 35 | + private bool activate; |
| 36 | + |
| 37 | + public SpeedBasedMusicParamTrigger(EntityData data, Vector2 offset) : base(data, offset) { |
| 38 | + paramName = data.Attr("paramName"); |
| 39 | + minSpeed = data.Float("minSpeed"); |
| 40 | + maxSpeed = data.Float("maxSpeed"); |
| 41 | + activate = data.Bool("activate"); |
| 42 | + } |
| 43 | + |
| 44 | + public override void OnEnter(Player player) { |
| 45 | + base.OnEnter(player); |
| 46 | + |
| 47 | + if (activate) { |
| 48 | + // register the speed-based music param as active to keep it updated. |
| 49 | + SpringCollab2020Module.Instance.Session.ActiveSpeedBasedMusicParams[paramName] = new SpringCollab2020Session.SpeedBasedMusicParamInfo() { |
| 50 | + MinimumSpeed = minSpeed, |
| 51 | + MaximumSpeed = maxSpeed |
| 52 | + }; |
| 53 | + } else { |
| 54 | + // unregister the param, and set the music param to the minimum value. |
| 55 | + SpringCollab2020Module.Instance.Session.ActiveSpeedBasedMusicParams.Remove(paramName); |
| 56 | + |
| 57 | + AudioState audio = SceneAs<Level>().Session.Audio; |
| 58 | + audio.Music.Param(paramName, minSpeed); |
| 59 | + audio.Apply(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments