static public BtAction GetBehaviorTreeDemo1() { if (bev_tree_demo1 != null) { return(bev_tree_demo1); } // root bev_tree_demo1 = new BtActionPrioritizedSelector(); { // 序列节点 BtActionSequence sequence_01 = new BtActionSequence(); bev_tree_demo1.AddChild(sequence_01); { // TbPreconditionNot not = new TbPreconditionNot(new BtHasReachedTargetCondition()); sequence_01.SetPrecondition(not); { sequence_01.AddChild(new BtTurnToLeaf()); sequence_01.AddChild(new BtMoveToLeaf()); } } bev_tree_demo1.AddChild(new BtAttackLeaf()); } return(bev_tree_demo1); }
protected override bool OnEvaluate(BtWorkingData workData) { BtActionParallelContext thisContext = GetContext <BtActionParallelContext>(workData); _init_list_to(thisContext._evaluationStatus, false); bool finalResult = false; for (int i = 0; i < GetChildCount(); ++i) { BtAction node = GetChild <BtAction>(i); bool ret = node.Evaluate(workData); //early break if (_evaluationRelationship == E_ChildrenRelationShip.and && ret == false) { finalResult = false; break; } if (ret) { finalResult = true; } thisContext._evaluationStatus[i] = ret; } return(finalResult); }
public BtEntityAi Init() { _behavior_tree = BtEntityFactory.GetBehaviorTreeDemo1(); _behavior_working_data = new AIEntityWorkingData(); _behavior_working_data.EntityAi = this; _blackboard = new BtBlackBoard(); _nexttime_to_gen_moving_target = 0; return(this); }
protected override void OnTransition(BtWorkingData workData) { BtActionParallelContext thisContext = GetContext <BtActionParallelContext>(workData); for (int i = 0; i < GetChildCount(); ++i) { BtAction node = GetChild <BtAction>(i); node.Transition(workData); } //clear running status _init_list_to(thisContext._runningStatus, BtRunningStatus.EXECUTING); }
protected override int OnUpdate(BtWorkingData workData) { BtActionParallelContext thisContext = GetContext <BtActionParallelContext>(workData); //first time initialization if (thisContext._runningStatus.Count != GetChildCount()) { _init_list_to(thisContext._runningStatus, BtRunningStatus.EXECUTING); } bool hasFinished = false; bool hasExecuting = false; for (int i = 0; i < GetChildCount(); ++i) { if (thisContext._evaluationStatus[i] == false) { continue; } if (BtRunningStatus.IsFinished(thisContext._runningStatus[i])) { hasFinished = true; continue; } BtAction node = GetChild <BtAction>(i); int runningStatus = node.Update(workData); if (BtRunningStatus.IsFinished(runningStatus)) { hasFinished = true; } else { hasExecuting = true; } thisContext._runningStatus[i] = runningStatus; } if (_runningstatusRelationship == E_ChildrenRelationShip.or && hasFinished || _runningstatusRelationship == E_ChildrenRelationShip.and && hasExecuting == false) { _init_list_to(thisContext._runningStatus, BtRunningStatus.EXECUTING); return(BtRunningStatus.FINISHED); } return(BtRunningStatus.EXECUTING); }
public static BtAction GetBtDemo2() { if (bev_tree_demo2 != null) { return(bev_tree_demo2); } // root bev_tree_demo2 = new BtActionPrioritizedSelector(); { BtActionPrioritizedSelector patrol = new BtActionPrioritizedSelector(); patrol.TbName = "巡逻"; bev_tree_demo2.AddChild(patrol); { // 视野内没有目标 // 随机走路 } BtActionSequence attack_sequence = new BtActionSequence(); attack_sequence.TbName = "攻击"; bev_tree_demo2.AddChild(attack_sequence); { BtActionSequence violent_attack = new BtActionSequence(); violent_attack.TbName = "狂暴攻击"; { // 自身处于狂暴状态 // 移动 // 普攻 } BtActionSequence normal_attack = new BtActionSequence(); normal_attack.TbName = "常规攻击"; // 视野内发现目标 // 目标是玩家 { // 移动 // 普攻 } } BtActionPrioritizedSelector self_defense = new BtActionPrioritizedSelector(); self_defense.TbName = "反击"; bev_tree_demo2.AddChild(self_defense); { // 反击 { // 玩家被攻击 // 生命高于500 // 移动 攻击玩家 } // 逃跑 { // 玩家被攻击 // hp 低于500 // 在警戒线外 } } } return(bev_tree_demo2); }