A game about forced loneliness, made by TACStudios
1using UnityEngine; 2 3namespace Unity.VisualScripting 4{ 5 /// <summary> 6 /// Compares two inputs. 7 /// </summary> 8 [UnitCategory("Logic")] 9 [UnitTitle("Comparison")] 10 [UnitShortTitle("Comparison")] 11 [UnitOrder(99)] 12 public sealed class Comparison : Unit 13 { 14 /// <summary> 15 /// The first input. 16 /// </summary> 17 [DoNotSerialize] 18 public ValueInput a { get; private set; } 19 20 /// <summary> 21 /// The second input. 22 /// </summary> 23 [DoNotSerialize] 24 public ValueInput b { get; private set; } 25 26 /// <summary> 27 /// Whether the compared inputs are numbers. 28 /// </summary> 29 [Serialize] 30 [Inspectable] 31 public bool numeric { get; set; } = true; 32 33 /// <summary> 34 /// Whether A is less than B. 35 /// </summary> 36 [DoNotSerialize] 37 [PortLabel("A < B")] 38 public ValueOutput aLessThanB { get; private set; } 39 40 /// <summary> 41 /// Whether A is less than or equal to B. 42 /// </summary> 43 [DoNotSerialize] 44 [PortLabel("A \u2264 B")] 45 public ValueOutput aLessThanOrEqualToB { get; private set; } 46 47 /// <summary> 48 /// Whether A is equal to B. 49 /// </summary> 50 [DoNotSerialize] 51 [PortLabel("A = B")] 52 public ValueOutput aEqualToB { get; private set; } 53 54 /// <summary> 55 /// Whether A is not equal to B. 56 /// </summary> 57 [DoNotSerialize] 58 [PortLabel("A \u2260 B")] 59 public ValueOutput aNotEqualToB { get; private set; } 60 61 /// <summary> 62 /// Whether A is greater than or equal to B. 63 /// </summary> 64 [DoNotSerialize] 65 [PortLabel("A \u2265 B")] 66 public ValueOutput aGreaterThanOrEqualToB { get; private set; } 67 68 /// <summary> 69 /// Whether A is greater than B. 70 /// </summary> 71 [DoNotSerialize] 72 [PortLabel("A > B")] 73 public ValueOutput aGreatherThanB { get; private set; } 74 75 protected override void Definition() 76 { 77 if (numeric) 78 { 79 a = ValueInput<float>(nameof(a)); 80 b = ValueInput<float>(nameof(b), 0); 81 82 aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => NumericLess(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 83 aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => NumericLessOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 84 aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => NumericEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 85 aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => NumericNotEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 86 aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => NumericGreaterOrEqual(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 87 aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => NumericGreater(flow.GetValue<float>(a), flow.GetValue<float>(b))).Predictable(); 88 } 89 else 90 { 91 a = ValueInput<object>(nameof(a)).AllowsNull(); 92 b = ValueInput<object>(nameof(b)).AllowsNull(); 93 94 aLessThanB = ValueOutput(nameof(aLessThanB), (flow) => GenericLess(flow.GetValue(a), flow.GetValue(b))); 95 aLessThanOrEqualToB = ValueOutput(nameof(aLessThanOrEqualToB), (flow) => GenericLessOrEqual(flow.GetValue(a), flow.GetValue(b))); 96 aEqualToB = ValueOutput(nameof(aEqualToB), (flow) => GenericEqual(flow.GetValue(a), flow.GetValue(b))); 97 aNotEqualToB = ValueOutput(nameof(aNotEqualToB), (flow) => GenericNotEqual(flow.GetValue(a), flow.GetValue(b))); 98 aGreaterThanOrEqualToB = ValueOutput(nameof(aGreaterThanOrEqualToB), (flow) => GenericGreaterOrEqual(flow.GetValue(a), flow.GetValue(b))); 99 aGreatherThanB = ValueOutput(nameof(aGreatherThanB), (flow) => GenericGreater(flow.GetValue(a), flow.GetValue(b))); 100 } 101 102 Requirement(a, aLessThanB); 103 Requirement(b, aLessThanB); 104 105 Requirement(a, aLessThanOrEqualToB); 106 Requirement(b, aLessThanOrEqualToB); 107 108 Requirement(a, aEqualToB); 109 Requirement(b, aEqualToB); 110 111 Requirement(a, aNotEqualToB); 112 Requirement(b, aNotEqualToB); 113 114 Requirement(a, aGreaterThanOrEqualToB); 115 Requirement(b, aGreaterThanOrEqualToB); 116 117 Requirement(a, aGreatherThanB); 118 Requirement(b, aGreatherThanB); 119 } 120 121 private bool NumericLess(float a, float b) 122 { 123 return a < b; 124 } 125 126 private bool NumericLessOrEqual(float a, float b) 127 { 128 return a < b || Mathf.Approximately(a, b); 129 } 130 131 private bool NumericEqual(float a, float b) 132 { 133 return Mathf.Approximately(a, b); 134 } 135 136 private bool NumericNotEqual(float a, float b) 137 { 138 return !Mathf.Approximately(a, b); 139 } 140 141 private bool NumericGreaterOrEqual(float a, float b) 142 { 143 return a > b || Mathf.Approximately(a, b); 144 } 145 146 private bool NumericGreater(float a, float b) 147 { 148 return a > b; 149 } 150 151 private bool GenericLess(object a, object b) 152 { 153 return OperatorUtility.LessThan(a, b); 154 } 155 156 private bool GenericLessOrEqual(object a, object b) 157 { 158 return OperatorUtility.LessThanOrEqual(a, b); 159 } 160 161 private bool GenericEqual(object a, object b) 162 { 163 return OperatorUtility.Equal(a, b); 164 } 165 166 private bool GenericNotEqual(object a, object b) 167 { 168 return OperatorUtility.NotEqual(a, b); 169 } 170 171 private bool GenericGreaterOrEqual(object a, object b) 172 { 173 return OperatorUtility.GreaterThanOrEqual(a, b); 174 } 175 176 private bool GenericGreater(object a, object b) 177 { 178 return OperatorUtility.GreaterThan(a, b); 179 } 180 } 181}