A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2
3namespace Unity.VisualScripting
4{
5 [Analyser(typeof(IState))]
6 public class StateAnalyser<TState> : Analyser<TState, StateAnalysis>
7 where TState : class, IState
8 {
9 public StateAnalyser(GraphReference reference, TState target) : base(reference, target) { }
10
11 public TState state => target;
12
13 [Assigns]
14 protected virtual bool IsEntered()
15 {
16 using (var recursion = Recursion.New(1))
17 {
18 return IsEntered(state, recursion);
19 }
20 }
21
22 [Assigns]
23 protected virtual IEnumerable<Warning> Warnings()
24 {
25 if (!IsEntered())
26 {
27 yield return Warning.Info("State is never entered.");
28 }
29 }
30
31 private bool IsEntered(IState state, Recursion recursion)
32 {
33 if (state.isStart)
34 {
35 return true;
36 }
37
38 if (!recursion?.TryEnter(state) ?? false)
39 {
40 return false;
41 }
42
43 foreach (var incomingTransition in state.incomingTransitions)
44 {
45 if (IsEntered(incomingTransition.source, recursion) && incomingTransition.Analysis<StateTransitionAnalysis>(context).isTraversed)
46 {
47 recursion?.Exit(state);
48 return true;
49 }
50 }
51
52 recursion?.Exit(state);
53
54 return false;
55 }
56 }
57}