|
1 | | -using Celeste.Mod.CommunalHelper.Components; |
2 | | -using Celeste.Mod.CommunalHelper.States; |
3 | | -using System.Linq; |
| 1 | +using Celeste.Mod.CommunalHelper.States; |
4 | 2 | using static Celeste.Mod.CommunalHelper.States.Elytra; |
5 | 3 |
|
6 | 4 | namespace Celeste.Mod.CommunalHelper.Triggers; |
7 | 5 |
|
8 | 6 | [CustomEntity("CommunalHelper/ConfigureElytraTrigger")] |
9 | | -[Tracked] |
10 | | -public class ConfigureElytraTrigger : Trigger |
| 7 | +[TrackedAs(typeof(AbstractConfigureStateTrigger<ElytraOptions, ElytraOptionsChanges>))] |
| 8 | +public class ConfigureElytraTrigger : AbstractConfigureStateTrigger<ElytraOptions, ElytraOptionsChanges> |
11 | 9 | { |
12 | | - private readonly bool allow, infinite; |
13 | | - private readonly ElytraConfiguration options; |
14 | | - |
15 | | - private readonly bool revertOnLeave; |
16 | | - private readonly bool revertOnDeath; |
17 | | - private readonly bool onlyOnce; |
18 | | - |
19 | | - private struct ElytraConfigurationChanges |
20 | | - { |
21 | | - public bool? Allow; |
22 | | - public bool? Infinite; |
23 | | - public bool? DisableReverseVerticalMomentum; |
24 | | - } |
25 | | - |
26 | | - private ElytraConfigurationChanges? changesNeededToRevert; |
27 | | - |
28 | 10 | public ConfigureElytraTrigger(EntityData data, Vector2 offset) |
29 | 11 | : base(data, offset) |
30 | | - { |
31 | | - revertOnLeave = data.Bool("revertOnLeave", false); |
32 | | - revertOnDeath = data.Bool("revertOnDeath", true); |
33 | | - onlyOnce = data.Bool("onlyOnce", false); |
34 | | - |
35 | | - allow = data.Bool("allow", false); |
36 | | - infinite = data.Bool("infinite", false); |
| 12 | + { } |
37 | 13 |
|
38 | | - options = new ElytraConfiguration() |
| 14 | + protected override ElytraOptions GetConfiguredOptions(EntityData data) |
| 15 | + => new() |
39 | 16 | { |
40 | | - DisableReverseVerticalMomentum = data.Bool("disableReverseVerticalMomentum"), |
| 17 | + Allow = data.Bool("allow", false), |
| 18 | + Infinite = data.Bool("infinite", false), |
| 19 | + Configuration = new ElytraConfiguration() |
| 20 | + { |
| 21 | + DisableReverseVerticalMomentum = data.Bool("disableReverseVerticalMomentum"), |
| 22 | + }, |
41 | 23 | }; |
42 | | - |
43 | | - string flag = data.Attr("flag"); |
44 | | - if (!string.IsNullOrEmpty(flag)) { |
45 | | - Add(new FlagToggleComponent(flag, data.Bool("flagInverted"))); |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - private static ElytraConfigurationChanges CalculateChangesNeededToRevert((bool allow, bool infinite, ElytraConfiguration options) from, (bool allow, bool infinite, ElytraConfiguration options) to) |
| 24 | + protected override ElytraOptions GetCurrentOptions(Player player) |
50 | 25 | => new() |
51 | 26 | { |
52 | | - Allow = to.allow == from.allow ? null : from.allow, |
53 | | - Infinite = to.infinite == from.infinite ? null : from.infinite, |
54 | | - DisableReverseVerticalMomentum = to.options.DisableReverseVerticalMomentum == from.options.DisableReverseVerticalMomentum ? null : from.options.DisableReverseVerticalMomentum |
| 27 | + Allow = CommunalHelperModule.Session.CanDeployElytra, |
| 28 | + Infinite = player.HasInfiniteElytra(), |
| 29 | + Configuration = CommunalHelperModule.Session.CurrentElytraConfiguration, |
55 | 30 | }; |
56 | | - |
57 | | - private static (bool, bool, ElytraConfiguration) RevertChanges((bool allow, bool infinite, ElytraConfiguration options) current, ElytraConfigurationChanges? changesNeededToRevert) |
58 | | - => (changesNeededToRevert?.Allow ?? current.allow, changesNeededToRevert?.Infinite ?? current.infinite, new ElytraConfiguration() |
59 | | - { |
60 | | - DisableReverseVerticalMomentum = changesNeededToRevert?.DisableReverseVerticalMomentum ?? current.options.DisableReverseVerticalMomentum |
61 | | - }); |
62 | | - |
63 | | - private static void SaveChanges(Player player, bool allow, bool infinite, ElytraConfiguration options) |
| 31 | + protected override void SaveOptions(Player player, ElytraOptions options) |
64 | 32 | { |
65 | | - CommunalHelperModule.Session.CanDeployElytra = allow; |
66 | | - CommunalHelperModule.Session.CurrentElytraConfiguration = options; |
67 | | - player.SetInfiniteElytra(infinite); |
68 | | - } |
69 | | - |
70 | | - public override void OnEnter(Player player) |
71 | | - { |
72 | | - changesNeededToRevert = CalculateChangesNeededToRevert( |
73 | | - (CommunalHelperModule.Session.CanDeployElytra, player.HasInfiniteElytra(), CommunalHelperModule.Session.CurrentElytraConfiguration), |
74 | | - (allow, infinite, options)); |
75 | | - SaveChanges(player, allow, infinite, options); |
76 | | - |
77 | | - if (onlyOnce) { |
78 | | - RemoveSelf(); |
79 | | - } |
| 33 | + CommunalHelperModule.Session.CanDeployElytra = options.Allow; |
| 34 | + player.SetInfiniteElytra(options.Infinite); |
| 35 | + CommunalHelperModule.Session.CurrentElytraConfiguration = options.Configuration; |
80 | 36 | } |
81 | 37 |
|
82 | | - public override void OnLeave(Player player) |
83 | | - { |
84 | | - if (revertOnLeave && !player.Dead) |
| 38 | + protected override ElytraOptionsChanges CalculateChangesNeededToRevert(ElytraOptions from, ElytraOptions to) |
| 39 | + => new() |
85 | 40 | { |
86 | | - (bool newAllow, bool newInfinite, ElytraConfiguration newOptions) = RevertChanges((allow, infinite, options), changesNeededToRevert); |
87 | | - SaveChanges(player, newAllow, newInfinite, newOptions); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - #region Hooks |
92 | | - |
93 | | - internal static void Load() |
94 | | - { |
95 | | - Everest.Events.Player.OnDie += OnDie; |
96 | | - } |
| 41 | + Allow = to.Allow == from.Allow ? null : from.Allow, |
| 42 | + Infinite = to.Infinite == from.Infinite ? null : from.Infinite, |
| 43 | + Configuration = new ElytraOptionsChanges.ElytraConfigurationChanges() |
| 44 | + { |
| 45 | + DisableReverseVerticalMomentum = to.Configuration.DisableReverseVerticalMomentum == from.Configuration.DisableReverseVerticalMomentum ? null : from.Configuration.DisableReverseVerticalMomentum |
| 46 | + } |
| 47 | + }; |
| 48 | + protected override ElytraOptions RevertChanges(ElytraOptions current, ElytraOptionsChanges? changesNeededToRevert) |
| 49 | + => new() |
| 50 | + { |
| 51 | + Allow = changesNeededToRevert?.Allow ?? current.Allow, |
| 52 | + Infinite = changesNeededToRevert?.Infinite ?? current.Infinite, |
| 53 | + Configuration = new ElytraConfiguration() |
| 54 | + { |
| 55 | + DisableReverseVerticalMomentum = changesNeededToRevert?.Configuration.DisableReverseVerticalMomentum ?? current.Configuration.DisableReverseVerticalMomentum |
| 56 | + } |
| 57 | + }; |
| 58 | +} |
97 | 59 |
|
98 | | - internal static void Unload() |
99 | | - { |
100 | | - Everest.Events.Player.OnDie -= OnDie; |
101 | | - } |
| 60 | +public struct ElytraOptions |
| 61 | +{ |
| 62 | + public bool Allow; |
| 63 | + public bool Infinite; |
| 64 | + public ElytraConfiguration Configuration; |
| 65 | +} |
102 | 66 |
|
103 | | - private static void OnDie(Player player) |
| 67 | +public struct ElytraOptionsChanges |
| 68 | +{ |
| 69 | + public struct ElytraConfigurationChanges |
104 | 70 | { |
105 | | - foreach (ConfigureElytraTrigger trigger in player.SceneAs<Level>().Tracker.GetEntities<ConfigureElytraTrigger>() |
106 | | - .Cast<ConfigureElytraTrigger>() |
107 | | - .Where(trigger => trigger.revertOnDeath && trigger.changesNeededToRevert is not null)) |
108 | | - { |
109 | | - (bool newAllow, bool newInfinite, ElytraConfiguration newOptions) = RevertChanges((trigger.allow, trigger.infinite, trigger.options), trigger.changesNeededToRevert); |
110 | | - SaveChanges(player, newAllow, newInfinite, newOptions); |
111 | | - } |
| 71 | + public bool? DisableReverseVerticalMomentum; |
112 | 72 | } |
113 | 73 |
|
114 | | - #endregion |
| 74 | + public bool? Allow; |
| 75 | + public bool? Infinite; |
| 76 | + public ElytraConfigurationChanges Configuration; |
115 | 77 | } |
0 commit comments