-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNode.cs
More file actions
143 lines (118 loc) · 4.89 KB
/
Copy pathNode.cs
File metadata and controls
143 lines (118 loc) · 4.89 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections.Generic;
using UnityEngine;
namespace ActionGraph
{
public partial class Node
{
// -------------------------------------------------------------------------------
public string Name = "Node";
public Rect GraphWindowRect = new Rect(10, 10, 300, 75);
public bool MustFinishAllActions = true;
public List<Action> Actions = new List<Action>();
public List<Connection> OutgoingConnections = new List<Connection>();
public List<Connection> IncomingConnections = new List<Connection>();
private int mCurrentActionIndex = 0;
#if UNITY_EDITOR
public class ConnectionHandle
{
public Vector2 Position;
public Rect DisplayRect;
}
[System.NonSerialized]
public List<ConnectionHandle> ConnectionHandles = new List<ConnectionHandle>();
#endif
// -------------------------------------------------------------------------------
public bool FinishedAllActions { get; set; }
public Action CurrentAction { get { return (mCurrentActionIndex < Actions.Count) ? Actions[mCurrentActionIndex] : null; } }
// -------------------------------------------------------------------------------
public virtual void OnEnter()
{
mCurrentActionIndex = 0;
if (Actions.Count > 0)
{
FinishedAllActions = false;
Actions[mCurrentActionIndex].Start();
}
else
{
FinishedAllActions = true;
}
}
// -------------------------------------------------------------------------------
public virtual void OnUpdate()
{
if (FinishedAllActions || mCurrentActionIndex >= Actions.Count)
{
return;
}
// Update the current action until it says it's done
var currentAction = Actions[mCurrentActionIndex];
currentAction.Update();
var actionComplete = (currentAction.Status == ActionStatus.Finished); // KB TODO: SUPPORT THIS ON START TOO
if (actionComplete)
{
currentAction.Finish();
mCurrentActionIndex++;
// Have we finished running all of our actions?
if (mCurrentActionIndex >= Actions.Count)
{
FinishedAllActions = true;
}
else
{
Actions[mCurrentActionIndex].Start();
}
}
}
// -------------------------------------------------------------------------------
public virtual void OnExit()
{
// If we're leaving this node before we've finished every action, make sure we
// give our current action the chance to clean up after itself!
if (!FinishedAllActions && mCurrentActionIndex < Actions.Count)
{
Actions[mCurrentActionIndex].Finish();
}
}
// -------------------------------------------------------------------------------
public virtual void OnDelete()
{
// Make sure we clean up any connection we have to other nodes!
foreach (var incomingConnection in IncomingConnections)
{
incomingConnection.StartNode.OutgoingConnections.Remove(incomingConnection);
}
foreach (var outgoingConnection in OutgoingConnections)
{
outgoingConnection.EndNode.IncomingConnections.Remove(outgoingConnection);
}
}
// -------------------------------------------------------------------------------
public Connection GetOutgoingConnectionToNode(Node node)
{
foreach (var connection in OutgoingConnections)
{
if (connection.EndNode == node)
{
return connection;
}
}
return null;
}
// -------------------------------------------------------------------------------
public virtual Node CheckConnections()
{
// Check all of our outgoing connections
foreach (var outgoingConnection in OutgoingConnections)
{
// If we pass all the necessary conditions, return the EndNode for a transition!
if (outgoingConnection.CheckConditions())
{
return outgoingConnection.EndNode;
}
}
return this;
}
// -------------------------------------------------------------------------------
}
}