Skip to content

Commit c8864c1

Browse files
committed
Refactor configure state triggers into abstract class handling all logic
1 parent 2c096f3 commit c8864c1

File tree

4 files changed

+161
-174
lines changed

4 files changed

+161
-174
lines changed

everest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- Name: CommunalHelper
2-
Version: 1.22.0
2+
Version: 1.23.0
33
DLL: src/bin/Debug/net7.0/CommunalHelper.dll
44
Dependencies:
55
- Name: EverestCore
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Celeste.Mod.CommunalHelper.Components;
2+
using System.Linq;
3+
4+
namespace Celeste.Mod.CommunalHelper.Triggers;
5+
6+
// todo: tracking is borked
7+
[Tracked(true)]
8+
public abstract class AbstractConfigureStateTrigger<TOptions, TChanges> : Trigger
9+
where TOptions : struct where TChanges : struct
10+
{
11+
private readonly TOptions options;
12+
private TChanges? changesNeededToRevert;
13+
14+
private readonly bool revertOnLeave;
15+
private readonly bool revertOnDeath;
16+
private readonly bool onlyOnce;
17+
18+
public AbstractConfigureStateTrigger(EntityData data, Vector2 offset)
19+
: base(data, offset)
20+
{
21+
revertOnLeave = data.Bool("revertOnLeave", false);
22+
revertOnDeath = data.Bool("revertOnDeath", true);
23+
onlyOnce = data.Bool("onlyOnce", false);
24+
25+
options = GetConfiguredOptions(data);
26+
27+
string flag = data.Attr("flag");
28+
if (!string.IsNullOrEmpty(flag)) {
29+
Add(new FlagToggleComponent(flag, data.Bool("flagInverted")));
30+
}
31+
}
32+
33+
protected abstract TOptions GetConfiguredOptions(EntityData data);
34+
protected abstract TOptions GetCurrentOptions(Player player);
35+
protected abstract void SaveOptions(Player player, TOptions options);
36+
37+
protected abstract TChanges CalculateChangesNeededToRevert(TOptions from, TOptions to);
38+
protected abstract TOptions RevertChanges(TOptions current, TChanges? changesNeededToRevert);
39+
40+
public override void OnEnter(Player player)
41+
{
42+
changesNeededToRevert = CalculateChangesNeededToRevert(GetCurrentOptions(player), options);
43+
SaveOptions(player, options);
44+
45+
if (onlyOnce)
46+
{
47+
RemoveSelf();
48+
}
49+
}
50+
51+
public override void OnLeave(Player player)
52+
{
53+
if (revertOnLeave && !player.Dead)
54+
{
55+
SaveOptions(player, RevertChanges(options, changesNeededToRevert));
56+
}
57+
}
58+
59+
#region Hooks
60+
61+
internal static void Load()
62+
{
63+
Everest.Events.Player.OnDie += OnDie;
64+
}
65+
66+
internal static void Unload()
67+
{
68+
Everest.Events.Player.OnDie -= OnDie;
69+
}
70+
71+
private static void OnDie(Player player)
72+
{
73+
foreach (AbstractConfigureStateTrigger<TOptions, TChanges> trigger in player.SceneAs<Level>().Tracker.GetEntities<AbstractConfigureStateTrigger<TOptions, TChanges>>()
74+
.Cast<AbstractConfigureStateTrigger<TOptions, TChanges>>()
75+
.Where(trigger => trigger.revertOnDeath && trigger.changesNeededToRevert is not null))
76+
{
77+
trigger.SaveOptions(player, trigger.RevertChanges(trigger.options, trigger.changesNeededToRevert));
78+
}
79+
}
80+
81+
#endregion
82+
}
Lines changed: 25 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,17 @@
1-
using Celeste.Mod.CommunalHelper.Components;
2-
using System.Linq;
31
using static Celeste.Mod.CommunalHelper.DashStates.DreamTunnelDash;
42

53
namespace Celeste.Mod.CommunalHelper.Triggers;
64

75
[CustomEntity("CommunalHelper/ConfigureDreamTunnelDashTrigger")]
8-
[Tracked]
9-
public class ConfigureDreamTunnelDashTrigger : Trigger
6+
[TrackedAs(typeof(AbstractConfigureStateTrigger<DreamTunnelDashConfiguration, DreamTunnelDashConfigurationChanges>))]
7+
public class ConfigureDreamTunnelDashTrigger : AbstractConfigureStateTrigger<DreamTunnelDashConfiguration, DreamTunnelDashConfigurationChanges>
108
{
11-
private readonly DreamTunnelDashConfiguration options;
12-
13-
private readonly bool revertOnLeave;
14-
private readonly bool revertOnDeath;
15-
private readonly bool onlyOnce;
16-
17-
private struct DreamTunnelDashConfigurationChanges
18-
{
19-
public bool? AllowRedirect;
20-
public bool? AllowSameDirectionRedirect;
21-
public float? SameDirectionSpeedMultiplier;
22-
public bool? UseEntryDirection;
23-
public SpeedConfiguration? SpeedConfiguration;
24-
public float? CustomSpeed;
25-
public bool? AllowDashCancels;
26-
public bool? RedirectConsumesNormalDash;
27-
public bool? AllowTransitions;
28-
public bool? BounceOnCollision;
29-
}
30-
31-
private DreamTunnelDashConfigurationChanges? changesNeededToRevert;
32-
339
public ConfigureDreamTunnelDashTrigger(EntityData data, Vector2 offset)
3410
: base(data, offset)
35-
{
36-
revertOnLeave = data.Bool("revertOnLeave", false);
37-
revertOnDeath = data.Bool("revertOnDeath", true);
38-
onlyOnce = data.Bool("onlyOnce", false);
39-
40-
options = new DreamTunnelDashConfiguration()
11+
{ }
12+
13+
protected override DreamTunnelDashConfiguration GetConfiguredOptions(EntityData data)
14+
=> new()
4115
{
4216
AllowRedirect = data.Bool("allowRedirect", false),
4317
AllowSameDirectionRedirect = data.Bool("allowSameDirectionRedirect", false),
@@ -50,14 +24,12 @@ public ConfigureDreamTunnelDashTrigger(EntityData data, Vector2 offset)
5024
AllowTransitions = data.Bool("allowTransitions", false),
5125
BounceOnCollision = data.Bool("bounceOnCollision", false)
5226
};
53-
54-
string flag = data.Attr("flag");
55-
if (!string.IsNullOrEmpty(flag)) {
56-
Add(new FlagToggleComponent(flag, data.Bool("flagInverted")));
57-
}
58-
}
27+
protected override DreamTunnelDashConfiguration GetCurrentOptions(Player player)
28+
=> CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration;
29+
protected override void SaveOptions(Player player, DreamTunnelDashConfiguration options)
30+
=> CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration = options;
5931

60-
private static DreamTunnelDashConfigurationChanges CalculateChangesNeededToRevert(DreamTunnelDashConfiguration from, DreamTunnelDashConfiguration to)
32+
protected override DreamTunnelDashConfigurationChanges CalculateChangesNeededToRevert(DreamTunnelDashConfiguration from, DreamTunnelDashConfiguration to)
6133
=> new()
6234
{
6335
AllowRedirect = to.AllowRedirect == from.AllowRedirect ? null : from.AllowRedirect,
@@ -71,8 +43,7 @@ private static DreamTunnelDashConfigurationChanges CalculateChangesNeededToRever
7143
AllowTransitions = to.AllowTransitions == from.AllowTransitions ? null : from.AllowTransitions,
7244
BounceOnCollision = to.BounceOnCollision == from.BounceOnCollision ? null : from.BounceOnCollision
7345
};
74-
75-
private static DreamTunnelDashConfiguration RevertChanges(DreamTunnelDashConfiguration current, DreamTunnelDashConfigurationChanges? changesNeededToRevert)
46+
protected override DreamTunnelDashConfiguration RevertChanges(DreamTunnelDashConfiguration current, DreamTunnelDashConfigurationChanges? changesNeededToRevert)
7647
=> new()
7748
{
7849
AllowRedirect = changesNeededToRevert?.AllowRedirect ?? current.AllowRedirect,
@@ -86,46 +57,18 @@ private static DreamTunnelDashConfiguration RevertChanges(DreamTunnelDashConfigu
8657
AllowTransitions = changesNeededToRevert?.AllowTransitions ?? current.AllowTransitions,
8758
BounceOnCollision = changesNeededToRevert?.BounceOnCollision ?? current.BounceOnCollision
8859
};
60+
}
8961

90-
public override void OnEnter(Player player)
91-
{
92-
changesNeededToRevert = CalculateChangesNeededToRevert(CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration, options);
93-
CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration = options;
94-
95-
if (onlyOnce) {
96-
RemoveSelf();
97-
}
98-
}
99-
100-
public override void OnLeave(Player player)
101-
{
102-
if (revertOnLeave && !player.Dead)
103-
{
104-
CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration = RevertChanges(CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration, changesNeededToRevert);
105-
}
106-
}
107-
108-
#region Hooks
109-
110-
internal static void Load()
111-
{
112-
Everest.Events.Player.OnDie += OnDie;
113-
}
114-
115-
internal static void Unload()
116-
{
117-
Everest.Events.Player.OnDie -= OnDie;
118-
}
119-
120-
private static void OnDie(Player player)
121-
{
122-
foreach (ConfigureDreamTunnelDashTrigger trigger in player.SceneAs<Level>().Tracker.GetEntities<ConfigureDreamTunnelDashTrigger>()
123-
.Cast<ConfigureDreamTunnelDashTrigger>()
124-
.Where(trigger => trigger.revertOnDeath && trigger.changesNeededToRevert is not null))
125-
{
126-
CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration = RevertChanges(CommunalHelperModule.Session.CurrentDreamTunnelDashConfiguration, trigger.changesNeededToRevert);
127-
}
128-
}
129-
130-
#endregion
62+
public struct DreamTunnelDashConfigurationChanges
63+
{
64+
public bool? AllowRedirect;
65+
public bool? AllowSameDirectionRedirect;
66+
public float? SameDirectionSpeedMultiplier;
67+
public bool? UseEntryDirection;
68+
public SpeedConfiguration? SpeedConfiguration;
69+
public float? CustomSpeed;
70+
public bool? AllowDashCancels;
71+
public bool? RedirectConsumesNormalDash;
72+
public bool? AllowTransitions;
73+
public bool? BounceOnCollision;
13174
}
Lines changed: 53 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,77 @@
1-
using Celeste.Mod.CommunalHelper.Components;
2-
using Celeste.Mod.CommunalHelper.States;
3-
using System.Linq;
1+
using Celeste.Mod.CommunalHelper.States;
42
using static Celeste.Mod.CommunalHelper.States.Elytra;
53

64
namespace Celeste.Mod.CommunalHelper.Triggers;
75

86
[CustomEntity("CommunalHelper/ConfigureElytraTrigger")]
9-
[Tracked]
10-
public class ConfigureElytraTrigger : Trigger
7+
[TrackedAs(typeof(AbstractConfigureStateTrigger<ElytraOptions, ElytraOptionsChanges>))]
8+
public class ConfigureElytraTrigger : AbstractConfigureStateTrigger<ElytraOptions, ElytraOptionsChanges>
119
{
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-
2810
public ConfigureElytraTrigger(EntityData data, Vector2 offset)
2911
: 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+
{ }
3713

38-
options = new ElytraConfiguration()
14+
protected override ElytraOptions GetConfiguredOptions(EntityData data)
15+
=> new()
3916
{
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+
},
4123
};
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)
5025
=> new()
5126
{
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,
5530
};
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)
6432
{
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;
8036
}
8137

82-
public override void OnLeave(Player player)
83-
{
84-
if (revertOnLeave && !player.Dead)
38+
protected override ElytraOptionsChanges CalculateChangesNeededToRevert(ElytraOptions from, ElytraOptions to)
39+
=> new()
8540
{
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+
}
9759

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+
}
10266

103-
private static void OnDie(Player player)
67+
public struct ElytraOptionsChanges
68+
{
69+
public struct ElytraConfigurationChanges
10470
{
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;
11272
}
11373

114-
#endregion
74+
public bool? Allow;
75+
public bool? Infinite;
76+
public ElytraConfigurationChanges Configuration;
11577
}

0 commit comments

Comments
 (0)