Skip to content

Commit f4bbfca

Browse files
author
Baptiste SUZON
committed
feat: Rework utilitySystem to use Dictionaries
1 parent 304a152 commit f4bbfca

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

Runtime/Stat.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public float Value
2020
set { this.value = Mathf.Clamp(value, 0, 1); }
2121
}
2222

23+
public float OneNegValue => 1 - value;
24+
2325
public Stat(string name, float value)
2426
{
2527
Name = name;

Runtime/Utility.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public float Value
1818
set { this.value = Mathf.Clamp(value, 0, 1); }
1919
}
2020

21+
public float OneNegValue => 1 - value;
22+
2123
[SerializeField]
2224
public List<StatImportance> statImportances;
2325

Runtime/UtilitySystem.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
@@ -8,24 +9,28 @@ namespace UtilitySystemPackage
89
[System.Serializable]
910
public class UtilitySystem
1011
{
11-
[SerializeField] private List<Stat> _inputs = new List<Stat>();
12+
[SerializeField] private Dictionary<string, Stat> _inputs = new Dictionary<string, Stat>();
1213

13-
[SerializeField] private List<Utility> _outputs = new List<Utility>();
14+
[SerializeField] private Dictionary<string, Utility> _outputs = new Dictionary<string, Utility>();
1415

1516
public void Init(UtilitySystemData systemData)
1617
{
17-
_inputs = new List<Stat>();
18+
_inputs = new Dictionary<string, Stat>();
1819
for (int i = 0; i < systemData.inputs.Count; i++)
1920
{
20-
_inputs.Add(new Stat(systemData.inputs[i], 0f));
21+
_inputs.Add(systemData.inputs[i],new Stat(systemData.inputs[i], 0f));
2122
}
2223

23-
_outputs = systemData.utilities;
24+
_outputs = new Dictionary<string, Utility>();
25+
for (int i = 0; i < systemData.utilities.Count; i++)
26+
{
27+
_outputs.Add(systemData.utilities[i].Name, systemData.utilities[i]);
28+
}
2429
}
2530

2631
public void UpdateStats(Dictionary<string, float> inputs, bool allowPartialModifications = false)
2732
{
28-
foreach (Stat input in _inputs)
33+
foreach (Stat input in _inputs.Values)
2934
{
3035
if (inputs.ContainsKey(input.Name))
3136
{
@@ -40,40 +45,53 @@ public void UpdateStats(Dictionary<string, float> inputs, bool allowPartialModif
4045

4146
public void Update()
4247
{
43-
foreach (var output in _outputs)
48+
foreach (var output in _outputs.Values)
4449
{
45-
output.EvaluateUtility(_inputs);
50+
output.EvaluateUtility(new List<Stat>(_inputs.Values));
4651
}
4752
}
4853

4954
public Utility GetHighestUtility()
5055
{
5156
if (_outputs.Count == 0) return null;
5257

53-
Utility max = _outputs[0];
54-
for (int i = 1; i < _outputs.Count; i++)
58+
Utility max = null;
59+
60+
foreach (var utility in _outputs.Values)
5561
{
56-
if (_outputs[i].Value > max.Value)
57-
max = _outputs[i];
62+
if (max == null)
63+
max = utility;
64+
else if (utility.Value > max.Value)
65+
max = utility;
5866
}
5967

6068
return max;
6169
}
6270

63-
public List<Utility> GetUtilities()
71+
public Dictionary<string, Utility> GetUtilities()
6472
{
65-
List<Utility> retVal = new List<Utility>(_outputs);
66-
return retVal;
73+
return _outputs;
74+
}
75+
76+
public Dictionary<string, float> GetUtilitiesAsFloat()
77+
{
78+
Dictionary<string, float> utilities = new Dictionary<string, float>();
79+
foreach (KeyValuePair<string,Utility> kvp in _outputs)
80+
{
81+
utilities.Add(kvp.Key, kvp.Value.Value);
82+
}
83+
84+
return utilities;
6785
}
6886

6987
public List<Utility> GetUtilitiesSorted()
7088
{
71-
List<Utility> retVal = new List<Utility>(_outputs);
89+
List<Utility> retVal = new List<Utility>(_outputs.Values);
7290
retVal.Sort((lhs, rhs) => rhs.Value.CompareTo(lhs.Value));
7391
return retVal;
7492
}
7593

76-
public List<Stat> GetInputs()
94+
public Dictionary<string, Stat> GetInputs()
7795
{
7896
return _inputs;
7997
}

Samples~/Demo/UtilitySystems.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using UnityEngine;
6-
using UtilitySystem.Runtime;
6+
using UtilitySystemPackage;
77

88
public class UtilitySystems : MonoBehaviour
99
{
10-
[SerializeField] public UtilitySystem.Runtime.UtilitySystem logicUtilitySystem;
10+
[SerializeField] public UtilitySystem logicUtilitySystem;
1111
public UtilitySystemData logicData;
12-
[SerializeField] public UtilitySystem.Runtime.UtilitySystem personalityUtilitySystem;
12+
[SerializeField] public UtilitySystem personalityUtilitySystem;
1313
public UtilitySystemData personalityData;
1414

1515
// Start is called before the first frame update
@@ -23,15 +23,9 @@ void Start()
2323
void Update()
2424
{
2525
logicUtilitySystem.Update();
26-
var utilities = logicUtilitySystem.GetUtilities();
27-
28-
var dict = new Dictionary<string, float>();
29-
foreach (var utility in utilities)
30-
{
31-
dict[utility.Name] = utility.Value;
32-
}
33-
34-
personalityUtilitySystem.UpdateStats(dict, true);
26+
var utilities = logicUtilitySystem.GetUtilitiesAsFloat();
27+
28+
personalityUtilitySystem.UpdateStats(utilities, true);
3529
personalityUtilitySystem.Update();
3630
}
3731
#if UNITY_EDITOR
@@ -43,12 +37,12 @@ private void OnGUI()
4337
GUILayout.EndVertical();
4438
}
4539

46-
private void DisplayUtilitySystem(UtilitySystem.Runtime.UtilitySystem system, string name)
40+
private void DisplayUtilitySystem(UtilitySystem system, string name)
4741
{
4842
GUILayout.Label(name);
4943
GUILayout.BeginHorizontal();
5044
GUILayout.BeginVertical("box");
51-
foreach (var stat in system.GetInputs())
45+
foreach (var stat in system.GetInputs().Values)
5246
{
5347
GUILayout.BeginHorizontal();
5448
GUILayout.Label(stat.Name);
@@ -58,7 +52,7 @@ private void DisplayUtilitySystem(UtilitySystem.Runtime.UtilitySystem system, st
5852
GUILayout.EndVertical();
5953

6054
GUILayout.BeginVertical("box");
61-
foreach (var utility in system.GetUtilities())
55+
foreach (var utility in system.GetUtilities().Values)
6256
{
6357
GUILayout.BeginHorizontal();
6458
GUILayout.Label(utility.Name);

0 commit comments

Comments
 (0)