-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreconditionCollector.cs
More file actions
58 lines (48 loc) · 1.81 KB
/
Copy pathPreconditionCollector.cs
File metadata and controls
58 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
namespace Screenplay
{
public class PreconditionCollector : IPreconditionCollector
{
private readonly List<GlobalId> _lastAppliedLocals = new();
private readonly Action<PreconditionCollector> _onUnlocked;
private readonly Action<PreconditionCollector> _onLocked;
public bool IsUnlocked { get; private set; }
public ScreenplayGraph.Introspection Introspection { get; }
public Locals SharedLocals { get; } = new();
public PreconditionCollector(Action<PreconditionCollector> onUnlocked, Action<PreconditionCollector> onLocked, Precondition target, ScreenplayGraph.Introspection introspection)
{
_onUnlocked = onUnlocked;
_onLocked = onLocked;
Introspection = introspection;
if (introspection.Preconditions.TryGetValue(target, out var list) == false)
introspection.Preconditions[target] = list = new();
list.Add(this);
}
public void SetUnlockedState(bool state, params GlobalId[] locals)
{
lock (SharedLocals)
{
if (IsUnlocked == state)
return;
IsUnlocked = !IsUnlocked;
if (IsUnlocked)
{
foreach (var v in locals)
{
if (SharedLocals.TryAdd(v))
_lastAppliedLocals.Add(v);
}
_onUnlocked(this);
}
else
{
foreach (var v in _lastAppliedLocals)
SharedLocals.Remove(v);
_lastAppliedLocals.Clear();
_onLocked(this);
}
}
}
}
}