-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoratorNode.cs
51 lines (40 loc) · 1.43 KB
/
DecoratorNode.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
A decorator node, like a composite node, can have a child node. Unlike a composite node, they can specifically only have a single child.
Their function is either to transform the result they receive from their child node's status, to terminate the child, or repeat processing
of the child, depending on the type of decorator node.
A commonly used example of a decorator is the Inverter, which will simply invert the result of the child. A child fails and it will return
success to its parent, or a child succeeds and it will return failure to the parent.
*/
namespace BeeTree {
public abstract class DecoratorNode : Node
{
public override string Id => "DecoratorNode";
public override int MaxChildren => 1;
public override bool CanAddChild(Node child)
{
return childrenGuids.Count == 0;
}
public override bool AddChild(Node child)
{
if (child == null)
{
throw new System.Exception("Decorator.AddChild: cannot add null child.");
}
if (childrenGuids.Contains(child.guid))
{
throw new System.Exception("Decorator.AddChild: already a child.");
}
if (childrenGuids.Count >= 1)
{
Debug.LogError("Decorator.AddChild: decorators can only have on child. Skipping...");
return false;
}
child.Parent = this;
childrenGuids.Add(child.guid);
return true;
}
}
}