-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIBehaviourTreeController.cs
308 lines (262 loc) · 17.9 KB
/
AIBehaviourTreeController.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using UnityEngine;
using DD.Core.Control;
using DD.AI.BehaviourTreeSystem;
using DD.AI.States;
namespace DD.AI.Controllers
{
public class AIBehaviourTreeController : MonoBehaviour, IAIBehaviour, IBehaviourTreeInspectable
{
// BEHAVIOUR TREE
public BehaviourTree BehaviourTree { private set; get; }
// COMPONENTS - the AI's 'controller'
private AILocomotion aiLocomotion;
private AIAnimator aiAnimator;
[SerializeField] private Interactor interactor;
private void Awake()
{
aiLocomotion = GetComponent<AILocomotion>();
aiAnimator = GetComponent<AIAnimator>();
BehaviourTree = new BehaviourTree(this);
BehaviourTree.SetBehaviourTree(CreateBehaviourTree());
}
private Node CreateBehaviourTree()
{
// Create and add BB variables (to be defined in editor... but made here because custom Node tools are hard to make lol)
BehaviourTree.Blackboard.AddToBlackboard("Player", FindObjectOfType<PlayerController>());
BehaviourTree.Blackboard.AddToBlackboard("State", MrXState.SEARCHING);
BehaviourTree.Blackboard.AddToBlackboard("MoveTarget", null);
BehaviourTree.Blackboard.AddToBlackboard("TargetDoorPathIndex", 0);
BehaviourTree.Blackboard.AddToBlackboard("TargetDoorPath", null);
BehaviourTree.Blackboard.AddToBlackboard("TargetDoor", null);
BehaviourTree.Blackboard.AddToBlackboard("IdleTimerLength", 5.0f);
// BehaviourTree.Blackboard.AddToBlackboard("LastKnownLocation", Vector3.zero); // Commented out due to attach tree change
BehaviourTree.Blackboard.AddToBlackboard("TargetSearchRoom", null);
BehaviourTree.Blackboard.AddToBlackboard("SearchRoomCounter", 0);
BehaviourTree.Blackboard.AddToBlackboard("ChaseTimer", 5.0f);
// Creating actual behaviour tree
Selector root = new Selector(BehaviourTree,
new List<Node> {
// ! Stunned
new Sequence(BehaviourTree, new List<Node> {
new IsStunned(BehaviourTree),
new PlayAnimation(BehaviourTree, "stunned", true)
}),
// ! Player Dead
// new Sequence(BehaviourTree, new List<Node> {
// new IsPlayerDead(BehaviourTree),
// new IdleNode(BehaviourTree)
// }),
// ! Retreat
new Sequence(BehaviourTree, new List<Node> {
new Invertor(BehaviourTree, new IsBlackboardVariableNull(BehaviourTree, "retreat")),
new ShouldRetreat(BehaviourTree),
// Go to Retreat directly
new Selector(BehaviourTree, new List<Node> {
new Sequence(BehaviourTree, new List<Node> {
new IsInSameRoomAsVector3(BehaviourTree, "retreatPoint"),
new MoveToVector3(BehaviourTree, "retreatPoint")
}),
// Go to Retreat via Rooms
new Sequence(BehaviourTree, new List<Node> {
// Room Transition
new FindDoorPathToVector3(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "retreatPoint"),
new GetDoorFromPath(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "TargetDoor"),
new GetDoorEntryExitPoint(BehaviourTree, true, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
// Room Transition
new Sequence(BehaviourTree, new List<Node> {
new OpenDoor(BehaviourTree, "TargetDoor"),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.ENABLE),
new GetDoorEntryExitPoint(BehaviourTree, false, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.DISABLE),
new IncrementDoorPathIndex(BehaviourTree, "TargetDoorPathIndex", "TargetDoorPath"),
}, true)
})
})
}),
// ! NEW ATTACK
new ConditionalBranch(BehaviourTree, new CompareBlackboardVariable<MrXState>(BehaviourTree, MrXState.ATTACKING, "State"),
// Timer Selector
new Selector(BehaviourTree, new List<Node> {
// Reset Chase Timer if seen
new Sequence(BehaviourTree, new List<Node> {
new CanSeeObject(BehaviourTree),
new Invertor(BehaviourTree, new SetBlackboardVariable<float>(BehaviourTree, "ChaseTimer", 5.0f))
}),
// Usual Attacking Selector
new Selector(BehaviourTree, new List<Node> {
// * BECAUSE THIS IS ABOVE THE ATTACK MOVE LOGIC HE DOESN'T MOVE WHEN PLAYER IS IN VIEW WHEN ANIMATION IS PLAYING - maybe make selector between this and attacking selector?
// Decrement Chase Timer
new Sequence(BehaviourTree, new List<Node> {
new AlterBlackboardVariableByDeltaTimeFloat(BehaviourTree, "ChaseTimer", AffectValueType.DECREMENT),
new CompareBlackboardVariable<float>(BehaviourTree, 0.0f, "ChaseTimer", CompareType.LESS_THAN_EQUAL),
new PlayAnimation(BehaviourTree, "looking_around", true),
// Is Player in an inaccessible room?
new Selector(BehaviourTree, new List<Node> {
new Sequence(BehaviourTree, new List<Node> {
new CanReachObjectRoom(BehaviourTree, "Player"),
new GetRandomRoomAdjacentToTarget(BehaviourTree, true, "Player", "TargetSearchRoom")
}),
new GetRandomRoom(BehaviourTree, "TargetSearchRoom")
}),
new SetBlackboardVariable<MrXState>(BehaviourTree, "State", MrXState.SEARCHING)
}, false, true),
// Atttacking Player
new Sequence(BehaviourTree, new List<Node> {
new IsAtTarget<Component>(BehaviourTree, "Player", 1.0f),
new PlayAnimation(BehaviourTree, "right_hook", true)
}),
// Move To Player
new Sequence(BehaviourTree, new List<Node> {
new IsInSameRoomAs<Component>(BehaviourTree, "Player"),
new MoveTo<Component>(BehaviourTree, "Player")
}),
// Room Navigation
new Sequence(BehaviourTree, new List<Node> {
// Room Transition
new FindDoorPathTo<Component>(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "Player"),
new GetDoorFromPath(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "TargetDoor"),
new GetDoorEntryExitPoint(BehaviourTree, true, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
// Room Transition
new Selector(BehaviourTree, new List<Node> {
// Locked Door?
new Sequence(BehaviourTree, new List<Node> {
new Invertor(BehaviourTree, new CanUseDoor(BehaviourTree, "TargetDoor")),
new CloseDoor(BehaviourTree, "TargetDoor"),
new BangDoor(BehaviourTree, "TargetDoor"),
new IdleNode(BehaviourTree, "IdleTimerLength"),
new GetRandomRoom(BehaviourTree, "TargetSearchRoom"),
new SetBlackboardVariable<MrXState>(BehaviourTree, "State", MrXState.SEARCHING)
}),
// Use Door
new Sequence(BehaviourTree, new List<Node> {
new OpenDoor(BehaviourTree, "TargetDoor"),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.ENABLE),
new GetDoorEntryExitPoint(BehaviourTree, false, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.DISABLE),
new IncrementDoorPathIndex(BehaviourTree, "TargetDoorPathIndex", "TargetDoorPath"),
}, true)
})
})
})
})
),
// ! Search
new ConditionalBranch(BehaviourTree, new CompareBlackboardVariable<MrXState>(BehaviourTree, MrXState.SEARCHING, "State"),
new Selector(BehaviourTree, new List<Node> {
// ! Init Search
new Sequence(BehaviourTree, new List<Node> {
new IsBlackboardVariableNull(BehaviourTree, "TargetSearchRoom"),
new GetRandomRoomAdjacentToTarget(BehaviourTree, true, "Player", "TargetSearchRoom")
}),
// ! Can See Player?
new Sequence(BehaviourTree, new List<Node> {
new CanSeeObject(BehaviourTree),
// new SetLastKnownLocation(BehaviourTree, "LastKnownLocation", "Player"),
new SetBlackboardVariable<MrXState>(BehaviourTree, "State", MrXState.ATTACKING)
}),
// ! Can Hear Player?
new Sequence(BehaviourTree, new List<Node> {
new HasHeardSound(BehaviourTree),
new CanReachObjectRoom(BehaviourTree, "Player"),
// new SetLastKnownLocation(BehaviourTree, "LastKnownLocation", "Player"),
new SetBlackboardVariable<MrXState>(BehaviourTree, "State", MrXState.ATTACKING)
}),
// ! Search Room + Go to Room
new Selector(BehaviourTree, new List<Node> {
// Search Room
new Sequence(BehaviourTree, new List<Node> {
new IsInRoom(BehaviourTree, "TargetSearchRoom"),
// Search Spots
new Repeater(BehaviourTree,
new Sequence(BehaviourTree, new List<Node> {
new GetRandomRoomSearchSpot(BehaviourTree, "TargetSearchRoom", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
new PlayAnimation(BehaviourTree, "looking_around", true),
new IncrementBlackboardVariable(BehaviourTree, "SearchRoomCounter", 1)
}),
new CompareBlackboardVariable<int>(BehaviourTree, 1, "SearchRoomCounter"),
NodeState.SUCCESSFUL
),
new SetBlackboardVariable<int>(BehaviourTree, "SearchRoomCounter", 0),
// Is Player in an inaccessible room?
new Selector(BehaviourTree, new List<Node> {
new Sequence(BehaviourTree, new List<Node> {
new CanReachObjectRoom(BehaviourTree, "Player"),
new GetRandomRoomAdjacentToTarget(BehaviourTree, true, "Player", "TargetSearchRoom")
}),
new GetRandomRoom(BehaviourTree, "TargetSearchRoom")
}),
}),
// Room Transition
new Sequence(BehaviourTree, new List<Node> {
new FindDoorPathTo<Component>(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "TargetSearchRoom"),
new GetDoorFromPath(BehaviourTree, "TargetDoorPath", "TargetDoorPathIndex", "TargetDoor"),
new GetDoorEntryExitPoint(BehaviourTree, true, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
new Selector(BehaviourTree, new List<Node> {
// Locked Door?
new Sequence(BehaviourTree, new List<Node> {
new Invertor(BehaviourTree, new CanUseDoor(BehaviourTree, "TargetDoor")),
new CloseDoor(BehaviourTree, "TargetDoor"),
new BangDoor(BehaviourTree, "TargetDoor"),
new IdleNode(BehaviourTree, "IdleTimerLength"),
new GetRandomRoom(BehaviourTree, "TargetSearchRoom")
}),
// Use Door
new Sequence(BehaviourTree, new List<Node> {
new OpenDoor(BehaviourTree, "TargetDoor"),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.ENABLE),
new GetDoorEntryExitPoint(BehaviourTree, false, "TargetDoor", "MoveTarget"),
new Repeater(BehaviourTree, new MoveTo<Component>(BehaviourTree, "MoveTarget"), new IsAtTarget<Component>(BehaviourTree, "MoveTarget", 0.2f), NodeState.SUCCESSFUL),
new SendAnimationRigSignal(BehaviourTree, "door", Animation.RigEvents.AnimRigEventType.DISABLE),
new IncrementDoorPathIndex(BehaviourTree, "TargetDoorPathIndex", "TargetDoorPath"),
}, true)
})
})
})
})
),
// ! Idle
new IdleNode(BehaviourTree)
}
);
return root;
}
private void Update()
{
BehaviourTree.EvaluateTree();
}
public Transform GetAITransform()
{
return transform;
}
public AIAnimator GetAnimator()
{
return aiAnimator;
}
public Interactor GetInteractor()
{
return interactor ?? null;
}
public T GetAIComponent<T>()
{
T component = GetComponent<T>();
if (component == null)
{
component = GetComponentInChildren<T>(true);
}
return component;
}
public T PeekAtBlackBoardVariable<T>(string blackboardKey)
{
if (BehaviourTree == null) return default;
return BehaviourTree.Blackboard.GetFromBlackboard<T>(blackboardKey);
}
}
}