This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cs
112 lines (96 loc) · 2.94 KB
/
Node.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using UnityEngine;
using System;
using System.Collections.Generic;
namespace BehaviorTree
{
/// <summary>
/// Base node for behavior tree nodes
/// </summary>
public abstract class Node : ScriptableObject
{
/// <summary>
/// Possible node states
/// </summary>
public enum State
{
Running,
Failure,
Success
}
public State state { get; protected set; }
public bool running { get; private set; } = false;
public string guid;
public abstract string Description { get; }
public Action updateHeat;
public Dictionary<string, dynamic> TreeBlackboard;
/// <summary>
/// Tree private blackboard
/// </summary>
public Dictionary<string, dynamic> Blackboard;
public dynamic BlackboardGet(string key)
{
if (Blackboard.ContainsKey(key))
{
return Blackboard[key];
}
return TreeBlackboard[key];
}
public void BlackboardSet(string key, dynamic value)
{
if (Debug.isDebugBuild && TreeBlackboard.ContainsKey(key))
{
Debug.LogWarning($"Shadowing Tree Blackboard value at {key}");
}
Blackboard[key] = value;
}
public void TreeBlackboardSet(string key, dynamic value)
{
if (Debug.isDebugBuild && Blackboard.ContainsKey(key))
{
Debug.LogWarning($"Tree Blackboard value at {key} shadowed by private blackboard value");
}
TreeBlackboard[key] = value;
}
/// <summary>
/// Editor position
/// </summary>
public Vector2 position;
/// <summary>
/// Execute this nodes logic callbacks
/// </summary>
/// <returns>The state of the node after execution</returns>
public State Update()
{
if (!running)
{
OnStart();
running = true;
}
state = OnUpdate();
updateHeat?.Invoke();
if (state == State.Failure || state == State.Success)
{
OnStop();
running = false;
}
return state;
}
public virtual Node Clone()
{
return Instantiate(this);
}
/// <summary>
/// Called when the node begins executing
/// </summary>
protected virtual void OnStart() { }
/// <summary>
/// Called when the node finishes execution (state becomes Success or Failure)
/// </summary>
protected virtual void OnStop() { }
/// <summary>
/// Called once per node execution
/// </summary>
/// <returns>The state of the node after execution</returns>
protected virtual State OnUpdate() { return State.Success; }
}
}