Skip to content

Commit aeb5dc2

Browse files
authored
Merge pull request #9 from CommunalHelper/bigkahuna443/activate-trigger-rework
Improve Factory Activation Trigger
2 parents 47b2e5b + ab84548 commit aeb5dc2

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

Ahorn/lang/en_gb.lang

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ placements.entities.FactoryHelper/WindTunnel.tooltips.particleColors=Hex colors
110110

111111
# -- Triggers --
112112
# Factory Activation Trigger
113+
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.mode=The mode of this trigger.\nActivate: will activate all entities with the given activator IDs.\nDeactivate: will deactivate all entities with the given activator IDs.
113114
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.activationIds=A comma-separated list of all the activator IDs this trigger should set.
114115
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.ownActivationId=String value of the trigger's activator ID. Can only fire if the ID is active.
115-
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.resetOnLeave=Determines whether the IDs activated should be reset to their previous state upon leaving. If persistent is true this will be ignored.
116+
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.lockState=Whether the activated entities should have their states locked after using the trigger.\nIf true, other activators will not affect those entities after the trigger is used until a retry.
116117
placements.triggers.FactoryHelper/FactoryActivationTrigger.tooltips.persistent=Determines whether the IDs not already persistent that are governed by this trigger should become persistent, meaning they will be active even after leaving the room.
117118

118119
# Factory Event Trigger

Ahorn/triggers/factoryActivationTrigger.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module FactoryHelperFactoryActivationTrigger
22

33
using ..Ahorn, Maple
44

5-
@mapdef Trigger "FactoryHelper/FactoryActivationTrigger" FactoryActivationTrigger(x::Integer, y::Integer, width::Integer=16, height::Integer=16, activationIds::String="", ownActivationId::String="", resetOnLeave::Bool=false, persistent::Bool=false)
5+
@mapdef Trigger "FactoryHelper/FactoryActivationTrigger" FactoryActivationTrigger(x::Integer, y::Integer, width::Integer=16, height::Integer=16, mode::String="Activate", activationId::String="", activationIds::String="", ownActivationId::String="", lockState::Bool=true, persistent::Bool=false)
66

77
const placements = Ahorn.PlacementDict(
88
"Factory Activation Trigger (FactoryHelper)" => Ahorn.EntityPlacement(

Code/Triggers/FactoryActivationTrigger.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88
namespace FactoryHelper.Triggers {
99
[CustomEntity("FactoryHelper/FactoryActivationTrigger")]
1010
public class FactoryActivationTrigger : Trigger {
11-
private readonly bool _resetOnLeave;
11+
private readonly bool _lockState;
1212
private readonly bool _persistent;
13-
private readonly HashSet<string> _activationIds = new();
14-
private bool _hasFired;
13+
private readonly ActivationModes _mode;
14+
private readonly HashSet<string> _activationIds = [];
1515

1616
public FactoryActivationTrigger(EntityData data, Vector2 offset)
1717
: base(data, offset) {
1818
string[] activationIds = data.Attr("activationIds", "").Split(',');
1919

2020
_persistent = data.Bool("persistent", false);
21-
_resetOnLeave = !_persistent && data.Bool("resetOnLeave", false);
21+
_lockState = data.Bool("lockState", true);
22+
_mode = data.Enum("mode", ActivationModes.Activate);
2223
Add(Activator = new FactoryActivator());
2324
Activator.ActivationId = data.Attr("ownActivationId") == string.Empty ? null : data.Attr("ownActivationId");
2425
Activator.StartOn = Activator.ActivationId == null;
@@ -30,48 +31,43 @@ public FactoryActivationTrigger(EntityData data, Vector2 offset)
3031
}
3132
}
3233

34+
private enum ActivationModes {
35+
Activate,
36+
Deactivate
37+
}
38+
3339
public FactoryActivator Activator { get; }
3440

3541
public override void OnEnter(Player player) {
3642
base.OnEnter(player);
37-
if (Activator.IsOn && (!_hasFired || _resetOnLeave)) {
38-
SetSessionTags(true);
39-
SendOutSignals(true);
40-
_hasFired = true;
41-
}
42-
}
43-
44-
public override void OnLeave(Player player) {
45-
base.OnLeave(player);
46-
if (Activator.IsOn && _resetOnLeave) {
47-
SetSessionTags(false);
48-
SendOutSignals(false);
43+
if (Activator.IsOn) {
44+
SetSessionTags(_mode);
45+
SendOutSignals(_mode);
4946
}
5047
}
5148

5249
public override void Added(Scene scene) {
5350
base.Added(scene);
5451
Activator.HandleStartup(scene);
55-
5652
}
5753

58-
private void SendOutSignals(bool activating = true) {
54+
private void SendOutSignals(ActivationModes mode) {
5955
foreach (FactoryActivator activator in Scene.Tracker.GetComponents<FactoryActivator>()) {
6056
if (_activationIds.Contains(activator.ActivationId)) {
61-
if (activating) {
62-
activator.Activate();
63-
} else {
64-
activator.Deactivate();
57+
if (mode == ActivationModes.Activate) {
58+
activator.Activate(_lockState);
59+
} else if (mode == ActivationModes.Deactivate) {
60+
activator.Deactivate(_lockState);
6561
}
6662
}
6763
}
6864
}
6965

70-
private void SetSessionTags(bool activating = true) {
66+
private void SetSessionTags(ActivationModes mode) {
7167
if (_persistent) {
7268
Level level = Scene as Level;
7369
foreach (string activationId in _activationIds) {
74-
level.Session.SetFlag($"FactoryActivation:{activationId}", activating);
70+
level.Session.SetFlag($"FactoryActivation:{activationId}", mode == ActivationModes.Activate);
7571
}
7672
}
7773
}

Loenn/lang/en_gb.lang

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,10 @@ entities.FactoryHelper/WindTunnel.attributes.description.particleColors=Hex colo
168168
# -- Triggers --
169169
# Factory Activation Trigger
170170
triggers.FactoryHelper/FactoryActivationTrigger.placements.name.factory_activation=Factory Activation
171+
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.mode=The mode of this trigger.\nActivate: will activate all entities with the given activator IDs.\nDeactivate: will deactivate all entities with the given activator IDs.
171172
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.activationIds=A comma-separated list of all the activator IDs this trigger should set.
172173
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.ownActivationId=String value of the trigger's activator ID. Can only fire if the ID is active.
173-
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.resetOnLeave=Determines whether the IDs activated should be reset to their previous state upon leaving. If persistent is true this will be ignored.
174+
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.lockState=Whether the activated entities should have their states locked after using the trigger.\nIf true, other activators will not affect those entities after the trigger is used until a retry.
174175
triggers.FactoryHelper/FactoryActivationTrigger.attributes.description.persistent=Determines whether the IDs not already persistent that are governed by this trigger should become persistent, meaning they will be active even after leaving the room.
175176

176177
# Factory Event Trigger

Loenn/triggers/factory_activation.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
local factoryActivation = {}
22

3+
local modes = {
4+
"Activate",
5+
"Deactivate"
6+
}
7+
38
factoryActivation.name = "FactoryHelper/FactoryActivationTrigger"
49
factoryActivation.placements = {
510
name = "factory_activation",
611
data = {
12+
mode = "Activate",
713
activationIds = "",
814
ownActivationId = "",
9-
resetOnLeave = false,
15+
lockState = true,
1016
persistent = false
1117
}
1218
}
19+
factoryActivation.fieldInformation = {
20+
mode = {
21+
editable = false,
22+
options = modes
23+
}
24+
}
1325

1426
return factoryActivation

0 commit comments

Comments
 (0)