Skip to content

Commit 8be48f1

Browse files
committed
Action renamed into Desire, namespace added to prevent name conflicts
1 parent 24b20b9 commit 8be48f1

File tree

6 files changed

+153
-139
lines changed

6 files changed

+153
-139
lines changed

Runtime/Action.cs

Lines changed: 0 additions & 80 deletions
This file was deleted.

Runtime/Blackboard.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public class Blackboard : MonoBehaviour
5+
namespace UtilitySystem
66
{
7-
[SerializeField]
8-
public List<Stat> stats = new List<Stat>();
9-
}
7+
public class Blackboard : MonoBehaviour
8+
{
9+
[SerializeField]
10+
public List<Stat> stats = new List<Stat>();
11+
}
12+
}

Runtime/Desire.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Unity.Collections;
4+
using UnityEngine;
5+
using UnityEngine.Events;
6+
7+
namespace UtilitySystem
8+
{
9+
10+
public class Desire : MonoBehaviour
11+
{
12+
[System.Serializable]
13+
struct StatImportance
14+
{
15+
public string statName;
16+
17+
[SerializeField]
18+
public AnimationCurve curve; // must return a value between 0 and 1.
19+
}
20+
21+
[SerializeField]
22+
float defaultImportance = 0f;
23+
24+
[SerializeField]
25+
public int priority = 0;
26+
27+
[SerializeField]
28+
List<StatImportance> statsImportance = new List<StatImportance>();
29+
30+
[SerializeField]
31+
UnityEvent<Desire> onStart;
32+
[SerializeField]
33+
UnityEvent<Desire> onEnd;
34+
35+
[SerializeField]
36+
float lastEvaluation = 0f;
37+
38+
39+
// STATS
40+
public float TotalDurationDoingDesire
41+
{
42+
get;
43+
private set;
44+
}
45+
46+
47+
48+
public float EvaluateUtility(Blackboard bb)
49+
{
50+
float totalImportance = defaultImportance;
51+
52+
foreach (StatImportance statImportance in statsImportance)
53+
{
54+
Stat stat = bb.stats.Find((Stat s) => { return s.name == statImportance.statName; });
55+
if (stat != null)
56+
totalImportance += statImportance.curve.Evaluate(stat.Ratio);
57+
else
58+
{
59+
Debug.LogWarning("Stat not found : " + statImportance.statName);
60+
}
61+
}
62+
63+
lastEvaluation = Mathf.Clamp(totalImportance, 0, 1);
64+
65+
return lastEvaluation;
66+
}
67+
68+
public virtual void StartDesire()
69+
{
70+
onStart?.Invoke(this);
71+
}
72+
73+
public virtual void UpdateDesire()
74+
{
75+
TotalDurationDoingDesire += Time.deltaTime;
76+
}
77+
78+
public virtual void EndDesire()
79+
{
80+
onEnd?.Invoke(this);
81+
}
82+
}
83+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Stat.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
using UnityEngine;
44
using UnityEngine.Events;
55

6-
[System.Serializable]
7-
public class Stat : MonoBehaviour
6+
namespace UtilitySystem
87
{
9-
[SerializeField]
10-
private float ratio;
11-
public float Ratio
8+
9+
[System.Serializable]
10+
public class Stat : MonoBehaviour
1211
{
13-
get { return ratio; }
14-
set
12+
[SerializeField]
13+
private float ratio;
14+
public float Ratio
1515
{
16-
ratio = Mathf.Clamp(value, 0, 1);
16+
get { return ratio; }
17+
set
18+
{
19+
ratio = Mathf.Clamp(value, 0, 1);
20+
}
1721
}
18-
}
19-
};
22+
};
23+
24+
}

Runtime/UtilitySystem.cs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,70 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
6-
7-
public class UtilitySystem : MonoBehaviour
5+
namespace UtilitySystem
86
{
9-
[SerializeField]
10-
List<Action> actions = new List<Action>();
11-
public List<Action> Actions
7+
8+
public class UtilitySystem : MonoBehaviour
129
{
13-
get { return actions; }
14-
}
10+
[SerializeField]
11+
List<Desire> actions = new List<Desire>();
12+
public List<Desire> Desires
13+
{
14+
get { return actions; }
15+
}
1516

1617

17-
[SerializeField]
18-
Blackboard bb;
18+
[SerializeField]
19+
Blackboard bb;
1920

20-
Action currentAction = null;
21+
Desire currentDesire = null;
2122

22-
public Action CurrentAction
23-
{
24-
get { return currentAction; }
25-
}
23+
public Desire CurrentDesire
24+
{
25+
get { return currentDesire; }
26+
}
2627

2728

2829

29-
private void Start()
30-
{
31-
if (currentAction != null)
30+
private void Start()
3231
{
33-
currentAction.StartAction();
32+
if (currentDesire != null)
33+
{
34+
currentDesire.StartDesire();
35+
}
3436
}
35-
}
3637

37-
private void Update()
38-
{
39-
float highestUtility = 0f;
40-
Action newAction = null;
41-
foreach (Action action in actions)
38+
private void Update()
4239
{
43-
float utility = action.EvaluateUtility(bb);
44-
if (utility > highestUtility || (newAction != null && utility == highestUtility && action.priority > newAction.priority))
40+
float highestUtility = 0f;
41+
Desire newDesire = null;
42+
foreach (Desire action in actions)
4543
{
46-
newAction = action;
47-
highestUtility = utility;
44+
float utility = action.EvaluateUtility(bb);
45+
if (utility > highestUtility || (newDesire != null && utility == highestUtility && action.priority > newDesire.priority))
46+
{
47+
newDesire = action;
48+
highestUtility = utility;
49+
}
4850
}
49-
}
5051

51-
if (newAction == null)
52-
{
53-
newAction = actions[Random.Range(0, actions.Count)];
54-
}
52+
if (newDesire == null)
53+
{
54+
newDesire = actions[Random.Range(0, actions.Count)];
55+
}
5556

56-
if (newAction != currentAction)
57-
{
58-
if (currentAction != null)
59-
currentAction.EndAction();
57+
if (newDesire != currentDesire)
58+
{
59+
if (currentDesire != null)
60+
currentDesire.EndDesire();
6061

61-
currentAction = newAction;
62-
currentAction.StartAction();
63-
}
62+
currentDesire = newDesire;
63+
currentDesire.StartDesire();
64+
}
6465

65-
// Call update atleast 1 time, even if StartAction() has been called during the same frame
66-
currentAction.UpdateAction();
66+
// Call update atleast 1 time, even if StartDesire() has been called during the same frame
67+
currentDesire.UpdateDesire();
68+
}
6769
}
68-
}
70+
71+
}

0 commit comments

Comments
 (0)