Skip to content

Commit

Permalink
Implement CancelNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumble009 committed Aug 23, 2020
1 parent fdcbfa4 commit 85cfcf5
Show file tree
Hide file tree
Showing 17 changed files with 1,948 additions and 155 deletions.
1,706 changes: 1,653 additions & 53 deletions BaseTreeGraph/BaseTree.asset

Large diffs are not rendered by default.

31 changes: 22 additions & 9 deletions BaseTreeGraph/BaseTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BaseTree : BehaviourTreeComponent
public bool case11 = false;[UnityEngine.SerializeField]
public bool setParameterBool1 = false;[UnityEngine.SerializeField]
public bool setParameterBool2 = true;[UnityEngine.SerializeField]
public bool while_bool2 = false;[UnityEngine.SerializeField]
public UnityEngine.Events.UnityEvent first_ev = new UnityEngine.Events.UnityEvent();
[UnityEngine.SerializeField]
public UnityEngine.Events.UnityEvent itr_ev = new UnityEngine.Events.UnityEvent();
Expand Down Expand Up @@ -78,6 +79,8 @@ public class BaseTree : BehaviourTreeComponent
[UnityEngine.SerializeField]
public UnityEngine.Events.UnityEvent setParameterEv7 = new UnityEngine.Events.UnityEvent();
[UnityEngine.SerializeField]
public UnityEngine.Events.UnityEvent while_ev3 = new UnityEngine.Events.UnityEvent();
[UnityEngine.SerializeField]
public float if_float1 = 100.5f;[UnityEngine.SerializeField]
public float setParameterFloat1 = 0f;[UnityEngine.SerializeField]
public float setParameterFloat2 = 0f;[UnityEngine.SerializeField]
Expand Down Expand Up @@ -116,28 +119,28 @@ override public void MakeTree()
BT_Execute sequenceEx3 = new BT_Execute();
BT_Interrupt frameCounterTest = new BT_Interrupt();
BT_Execute frameCounterEx1 = new BT_Execute();
BT_Timing frameCounter1 = new BT_Timing(behaviourTree, false, false);
BT_Timing frameCounter1 = new BT_Timing(behaviourTree, false, false, "frameCounter1");
BT_Interrupt frameCounterTest2 = new BT_Interrupt();
BT_Execute frameCounterEx2 = new BT_Execute();
BT_Interrupt frameCounterTest3 = new BT_Interrupt();
BT_Execute frameCounterEx3 = new BT_Execute();
BT_Timing frameCounter2 = new BT_Timing(behaviourTree, false, true);
BT_Timing frameCounter2 = new BT_Timing(behaviourTree, false, true, "frameCounter2");
BT_Interrupt frameCounterTest4 = new BT_Interrupt();
BT_Execute frameCounterEx4 = new BT_Execute();
BT_Timing frameCounter3 = new BT_Timing(behaviourTree, true, false);
BT_Timing frameCounter3 = new BT_Timing(behaviourTree, true, false, "frameCounter3");
BT_Interrupt frameCounterTest5 = new BT_Interrupt();
BT_Execute frameCounterEx5 = new BT_Execute();
BT_Interrupt timerTest = new BT_Interrupt();
BT_Execute timerTestEx1 = new BT_Execute();
BT_Timing timer1 = new BT_Timing(behaviourTree, false, false);
BT_Timing timer1 = new BT_Timing(behaviourTree, false, false, "timer1");
BT_Interrupt timerTest2 = new BT_Interrupt();
BT_Execute timerTestEx2 = new BT_Execute();
BT_Interrupt timerTest3 = new BT_Interrupt();
BT_Execute timerTestEx3 = new BT_Execute();
BT_Timing timer2 = new BT_Timing(behaviourTree, false, true);
BT_Timing timer2 = new BT_Timing(behaviourTree, false, true, "timer2");
BT_Interrupt timerTest4 = new BT_Interrupt();
BT_Execute timerTestEx4 = new BT_Execute();
BT_Timing timer3 = new BT_Timing(behaviourTree, true, false);
BT_Timing timer3 = new BT_Timing(behaviourTree, true, false, "timer3");
BT_Interrupt timerTest5 = new BT_Interrupt();
BT_Execute timerTestEx5 = new BT_Execute();
BT_Success NextSequence = new BT_Success();
Expand All @@ -164,6 +167,8 @@ override public void MakeTree()
BT_Execute SetFloat2 = new BT_Execute();
BT_If setParameterIf7 = new BT_If();
BT_Execute setParameterEx7 = new BT_Execute();
BT_While while2 = new BT_While();
BT_Execute whileEx3 = new BT_Execute();
firstEx.AddEvent(()=>{
first_ev.Invoke();
});
Expand Down Expand Up @@ -204,6 +209,13 @@ override public void MakeTree()
ifEx3.AddEvent(()=>{
if_ev3.Invoke();
});
while2.SetCondition(()=>{
return while_bool2;
});
while2.AddChild(whileEx3);
whileEx3.AddEvent(()=>{
while_ev3.Invoke();
});
while1.SetCondition(()=>{
return while_bool1;
});
Expand All @@ -220,6 +232,7 @@ override public void MakeTree()
whileEx2.AddEvent(()=>{
while_ev2.Invoke();
});
whileEx2.AddChild(while2);
selectorIf1.SetCondition(()=>{
return selector_bool1;
});
Expand All @@ -232,13 +245,13 @@ override public void MakeTree()
});
behaviourTree.AddInterrupt(SelectorTest);
SelectorTest.AddChild(selector1);
selectorEx2.AddEvent(()=>{
selector_ev2.Invoke();
});

selector1.AddChild(selectorIf1);
selector1.AddChild(selectorEx2);
selector1.AddChild(selectorEx3);
selectorEx2.AddEvent(()=>{
selector_ev2.Invoke();
});
selectorEx3.AddEvent(()=>{
selector_ev3.Invoke();
});
Expand Down
2 changes: 1 addition & 1 deletion ExampleScene/Scripts/EnemyBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ override public void MakeTree()
BT_Interrupt DamageInterrupt = new BT_Interrupt();
BT_Execute ResetDamageFlag = new BT_Execute();
BT_Execute ActivateEscape = new BT_Execute();
BT_Timing EscapeTimer = new BT_Timing(behaviourTree, true, false);
BT_Timing EscapeTimer = new BT_Timing(behaviourTree, true, false, "EscapeTimer");
BT_Interrupt EscapeResetInterrupt = new BT_Interrupt();
BT_Execute EscapeResetter = new BT_Execute();
BT_Execute SetFound = new BT_Execute();
Expand Down
17 changes: 16 additions & 1 deletion Script/BehaviourTree/BT_Timing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ namespace BT
public delegate Timing TimingCreate();
public class BT_Timing : BT_Node
{
public BT_Timing(BehaviourTree _tree, bool _isOverwrite, bool _isMultiple) : base()
public BT_Timing(BehaviourTree _tree, bool _isOverwrite, bool _isMultiple, string _name) : base()
{
tree = _tree;
isOverwrite = _isOverwrite;
isMultiple = _isMultiple;
name = _name;
}

protected BehaviourTree tree;
protected bool isOverwrite;
protected bool isMultiple;
protected string name;
public Timing lastInstance;
protected TimingCreate timingCreator;

Expand All @@ -24,6 +26,7 @@ public override ResultContainer Next()
if (lastInstance == null || lastInstance.IsActivated || isMultiple)
{
lastInstance = CreateNewTiming();
lastInstance.Name = name;
lastInstance.Init();
tree.AddTiming(lastInstance);
}
Expand Down Expand Up @@ -65,6 +68,18 @@ public class Timing
{
protected BT_Node target;
bool isActivated;
protected string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public bool IsActivated {
get {
return isActivated;
Expand Down
22 changes: 22 additions & 0 deletions Script/BehaviourTree/BehaviourTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,27 @@ public void AddTiming(Timing timing)
timings.Add(timing);
}
}

public void CancelTiming(bool isAllCancel, params string[] targets)
{
if (isAllCancel)
{
timings = new List<Timing>();
}
else
{
for(int i = timings.Count - 1; i >= 0; i--)
{
foreach(var target in targets)
{
if(timings[i].Name == target)
{
timings.RemoveAt(i);
break;
}
}
}
}
}
}
}
6 changes: 4 additions & 2 deletions Script/BehaviourTree/DefaultTemplates/CodeTemplateTable.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{"Key":"Success", "Value":"Declare/Success.txt"},
{"Key":"Timer", "Value":"Declare/Timer.txt"},
{"Key":"While", "Value":"Declare/While.txt"},
{"Key":"NextSequence", "Value":"Declare/NextSequence.txt"}
{"Key":"NextSequence", "Value":"Declare/NextSequence.txt"},
{"Key":"Cancel", "Value":"Declare/Cancel.txt"}
]},

{"Key":"Init","Value":[
Expand All @@ -39,7 +40,8 @@
{"Key":"Success", "Value":"Init/Success.txt"},
{"Key":"Timer", "Value":"Init/Timer.txt"},
{"Key":"While", "Value":"Init/While.txt"},
{"Key":"NextSequence", "Value":"Init/NextSequence.txt"}
{"Key":"NextSequence", "Value":"Init/NextSequence.txt"},
{"Key":"Cancel", "Value":"Init/Cancel.txt"}
]},

{"Key":"Base", "Value":[
Expand Down
1 change: 1 addition & 0 deletions Script/BehaviourTree/DefaultTemplates/Declare/Cancel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BT_Execute {{name}} = new BT_Execute();
7 changes: 7 additions & 0 deletions Script/BehaviourTree/DefaultTemplates/Declare/Cancel.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
BT_Timing {{name}} = new BT_Timing(behaviourTree, {{isOverwrite}}, {{isMultiple}});
BT_Timing {{name}} = new BT_Timing(behaviourTree, {{isOverwrite}}, {{isMultiple}}, "{{name}}");
2 changes: 1 addition & 1 deletion Script/BehaviourTree/DefaultTemplates/Declare/Timer.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BT_Timing {{name}} = new BT_Timing(behaviourTree, {{isOverwrite}}, {{isMultiple}});
BT_Timing {{name}} = new BT_Timing(behaviourTree, {{isOverwrite}}, {{isMultiple}}, "{{name}}");
3 changes: 3 additions & 0 deletions Script/BehaviourTree/DefaultTemplates/Init/Cancel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{name}}.AddEvent(()=>{
behaviourTree.CancelTiming({{isAllCancel}}, {{cancelTarget}});
});
7 changes: 7 additions & 0 deletions Script/BehaviourTree/DefaultTemplates/Init/Cancel.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
{"Key":"Success", "Value":"../Declare/Success.txt"},
{"Key":"Timer", "Value":"../Declare/Timer.txt"},
{"Key":"While", "Value":"../Declare/While.txt"},
{"Key": "NextSequence", "Value":"../Declare/NextSequence.txt"}
{"Key": "NextSequence", "Value":"../Declare/NextSequence.txt"},
{"Key":"Cancel", "Value":"../Declare/Cancel.txt"}
]},

{"Key":"Init","Value":[
Expand All @@ -40,7 +41,8 @@
{"Key":"Timer", "Value":"../Init/Timer.txt"},
{"Key":"While", "Value":"../Init/While.txt"},
{"Key":"InitParameter", "Value":"InitParameter.txt"},
{"Key":"NextSequence", "Value":"../Init/NextSequence.txt"}
{"Key":"NextSequence", "Value":"../Init/NextSequence.txt"},
{"Key":"Cancel", "Value":"../Init/Cancel.txt"}
]},

{"Key":"Base", "Value":[
Expand Down
14 changes: 7 additions & 7 deletions Test/Editor/BT_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public void TimingTest()

BT_Root root = new BT_Root();
BehaviourTree tree = new BehaviourTree(root);
BT_Timing timing = new BT_Timing(tree, false, false);
BT_Timing timing = new BT_Timing(tree, false, false, "timing");
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();

Expand Down Expand Up @@ -483,7 +483,7 @@ public void TimerTest()

BT_Root root = new BT_Root();
BehaviourTree tree = new BehaviourTree(root);
BT_Timing timing = new BT_Timing(tree, false, false);
BT_Timing timing = new BT_Timing(tree, false, false, "timing");
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();

Expand Down Expand Up @@ -541,7 +541,7 @@ public void TimerOverwriteTest()

BT_Root root = new BT_Root();
BehaviourTree tree = new BehaviourTree(root);
BT_Timing timing = new BT_Timing(tree, true, false);
BT_Timing timing = new BT_Timing(tree, true, false, "timing");
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();

Expand Down Expand Up @@ -606,7 +606,7 @@ public void TimerMultipleTest()

BT_Root root = new BT_Root();
BehaviourTree tree = new BehaviourTree(root);
BT_Timing timing = new BT_Timing(tree, false, true);
BT_Timing timing = new BT_Timing(tree, false, true, "timing");
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();

Expand Down Expand Up @@ -671,7 +671,7 @@ public void FrameCounterTest()

BT_Root root = new BT_Root();
BehaviourTree tree = new BehaviourTree(root);
BT_Timing timing = new BT_Timing(tree, false, false);
BT_Timing timing = new BT_Timing(tree, false, false, "timing");
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();

Expand Down Expand Up @@ -729,7 +729,7 @@ public void FrameCounterOverwriteTest()
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();
BT_If if1 = new BT_If();
BT_Timing timing = new BT_Timing(tree, true, false);
BT_Timing timing = new BT_Timing(tree, true, false, "timing");

root.AddChild(ex1);
ex1.AddChild(ex_target);
Expand Down Expand Up @@ -798,7 +798,7 @@ public void FrameCounterMultipleTest()
BT_Execute ex1 = new BT_Execute();
BT_Execute ex_target = new BT_Execute();
BT_If if1 = new BT_If();
BT_Timing timing = new BT_Timing(tree, false, true);
BT_Timing timing = new BT_Timing(tree, false, true, "timing");

root.AddChild(ex1);
ex1.AddChild(ex_target);
Expand Down
Loading

0 comments on commit 85cfcf5

Please sign in to comment.