Skip to content

Commit

Permalink
<feature> - fix: BHTreeNode.Container_SelectorSequence_Execute() - ad…
Browse files Browse the repository at this point in the history
…d: SelectorRandom_Execute()
  • Loading branch information
MiaoElin committed Jul 23, 2024
1 parent 8bdded7 commit bf1f79f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 22 deletions.
7 changes: 7 additions & 0 deletions Assets/Doc/行为树.txt.meta

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

76 changes: 71 additions & 5 deletions Assets/Src_Runtime/BHTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public void Reset() {
activeChild = null;
}

private bool AllowEnter() {
if (status == BHTreeNodeStatus.Done) {
return false;
} else if (status == BHTreeNodeStatus.NotEntered) {
if (PreconditionHandle == null || PreconditionHandle.Invoke()) {
return true;
} else {
return false;
}
} else {
return true;
}
}

// ==== 容器 ====
#region Container
public void InitAsContainer(BHTreeNodeType type) {
Expand All @@ -61,8 +75,10 @@ public BHTreeNodeStatus Execute(float dt) {
// 容器: 顺序节点 / 选择节点 / 并行与节点 / 并行或节点
if (type == BHTreeNodeType.Sequence) {
status = Container_Sequence_Execute(dt);
} else if (type == BHTreeNodeType.Selector) {
status = Container_Selector_Execute(dt);
} else if (type == BHTreeNodeType.SelectorSequence) {
status = Container_SelectorSequence_Execute(dt);
} else if (type == BHTreeNodeType.SelectorRandom) {
status = Container_SelectorRandom_Execute(dt);
} else if (type == BHTreeNodeType.ParallelAnd) {
status = Container_Parallel_And_Execute(dt);
} else if (type == BHTreeNodeType.ParallelOr) {
Expand Down Expand Up @@ -113,7 +129,7 @@ BHTreeNodeStatus Container_Sequence_Execute(float dt) {
return status;
}

BHTreeNodeStatus Container_Selector_Execute(float dt) {
BHTreeNodeStatus Container_SelectorSequence_Execute(float dt) {
if (status == BHTreeNodeStatus.NotEntered) {
if (PreconditionHandle == null || PreconditionHandle.Invoke()) {
status = BHTreeNodeStatus.Running;
Expand All @@ -135,9 +151,14 @@ BHTreeNodeStatus Container_Selector_Execute(float dt) {
// 同时执行一个
for (int i = 0; i < children.Count; i += 1) {
BHTreeNode child = children[i];
BHTreeNodeStatus childStatus = child.Execute(dt);
if (childStatus != BHTreeNodeStatus.Done) {
bool allow = child.AllowEnter();
if (allow) {
activeChild = child;
BHTreeNodeStatus childStatus = activeChild.Execute(dt);
if (childStatus == BHTreeNodeStatus.Done) {
status = BHTreeNodeStatus.Done;
activeChild = null;
}
break;
}
}
Expand All @@ -151,6 +172,51 @@ BHTreeNodeStatus Container_Selector_Execute(float dt) {
return status;
}

private BHTreeNodeStatus Container_SelectorRandom_Execute(float dt) {
if (status == BHTreeNodeStatus.NotEntered) {
if (PreconditionHandle == null || PreconditionHandle.Invoke()) {
status = BHTreeNodeStatus.Running;
for (int i = 0; i < children.Count; i++) {
int j = UnityEngine.Random.Range(i, children.Count);
BHTreeNode temp = children[i];
children[i] = children[j];
children[j] = temp;
}

} else {
status = BHTreeNodeStatus.Done;
}
} else if (status == BHTreeNodeStatus.Running) {
if (activeChild != null) {
BHTreeNodeStatus childStatus = activeChild.Execute(dt);
if (childStatus == BHTreeNodeStatus.Done) {
status = BHTreeNodeStatus.Done;
}
} else {
for (int i = 0; i < children.Count; i++) {
var child = children[i];
bool allow = child.AllowEnter();
if (allow) {
BHTreeNodeStatus childstatus = activeChild.Execute(dt);
if (childstatus == BHTreeNodeStatus.Done) {
status = BHTreeNodeStatus.Done;
} else {
activeChild = child;
}
break;
}
}
if (activeChild == null) {
status = BHTreeNodeStatus.Done;
}
}
} else {

}
return status;
}


BHTreeNodeStatus Container_Parallel_And_Execute(float dt) {
if (status == BHTreeNodeStatus.NotEntered) {
if (PreconditionHandle == null || PreconditionHandle.Invoke()) {
Expand Down
3 changes: 2 additions & 1 deletion Assets/Src_Runtime/BHTreeNodeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace BHTreeTutorial {
public enum BHTreeNodeType {
Action, // 行为节点
Sequence, // 顺序节点
Selector, // 选择节点
SelectorSequence, // 选择节点
SelectorRandom,
ParallelAnd, // 并行与节点
ParallelOr, // 并行或节点
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Src_Runtime/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void Start() {
attackContainer.PreconditionHandle = () => {
return hasTarget;
};
attackContainer.InitAsContainer(BHTreeNodeType.Selector);
attackContainer.InitAsContainer(BHTreeNodeType.SelectorSequence);
attackContainer.Container_AddChild(skill1Action);
attackContainer.Container_AddChild(skill2Action);

Expand Down
25 changes: 12 additions & 13 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"dependencies": {
"com.unity.2d.animation": {
"version": "9.1.0",
"version": "9.0.4",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.common": "8.0.2",
"com.unity.2d.common": "8.0.1",
"com.unity.2d.sprite": "1.0.0",
"com.unity.collections": "1.1.0",
"com.unity.modules.animation": "1.0.0",
Expand All @@ -14,7 +14,7 @@
"url": "https://packages.unity.cn"
},
"com.unity.2d.aseprite": {
"version": "1.1.2",
"version": "1.0.1",
"depth": 1,
"source": "registry",
"dependencies": {
Expand All @@ -26,7 +26,7 @@
"url": "https://packages.unity.cn"
},
"com.unity.2d.common": {
"version": "8.0.2",
"version": "8.0.1",
"depth": 2,
"source": "registry",
"dependencies": {
Expand All @@ -46,12 +46,12 @@
"url": "https://packages.unity.cn"
},
"com.unity.2d.psdimporter": {
"version": "8.0.4",
"version": "8.0.2",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.animation": "9.1.0",
"com.unity.2d.common": "8.0.2",
"com.unity.2d.animation": "9.0.1",
"com.unity.2d.common": "8.0.1",
"com.unity.2d.sprite": "1.0.0"
},
"url": "https://packages.unity.cn"
Expand Down Expand Up @@ -95,12 +95,11 @@
"url": "https://packages.unity.cn"
},
"com.unity.burst": {
"version": "1.8.13",
"version": "1.8.9",
"depth": 3,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1",
"com.unity.modules.jsonserialize": "1.0.0"
"com.unity.mathematics": "1.2.1"
},
"url": "https://packages.unity.cn"
},
Expand Down Expand Up @@ -133,14 +132,14 @@
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.2d.animation": "9.1.0",
"com.unity.2d.animation": "9.0.4",
"com.unity.2d.pixel-perfect": "5.0.3",
"com.unity.2d.psdimporter": "8.0.4",
"com.unity.2d.psdimporter": "8.0.2",
"com.unity.2d.sprite": "1.0.0",
"com.unity.2d.spriteshape": "9.0.2",
"com.unity.2d.tilemap": "1.0.0",
"com.unity.2d.tilemap.extras": "3.1.2",
"com.unity.2d.aseprite": "1.1.2"
"com.unity.2d.aseprite": "1.0.1"
}
},
"com.unity.ide.rider": {
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.23f1c1
m_EditorVersionWithRevision: 2022.3.23f1c1 (4a0772a0ebd0)
m_EditorVersion: 2022.3.13f1c1
m_EditorVersionWithRevision: 2022.3.13f1c1 (d214d547af8b)

0 comments on commit bf1f79f

Please sign in to comment.