// Use this for initialization void Start() { // -- ROOT AND ITS CHILDREN -- // Root node selector m_AI = new SelectorNode(); //m_AI = gameObject.AddComponent<SelectorNode>(); // 1st node of Root Condition whistleCondition = new Condition(Conditions.hasWhistled); ConditionNode whistle = new ConditionNode(whistleCondition, this); //ConditionNode whistle = gameObject.AddComponent<ConditionNode>(); // 2nd node of Root Condition inSphereCondition = new Condition(Conditions.inSphere); ConditionNode inSphere = new ConditionNode(inSphereCondition, this); // 3rd node of Root StochasticNode runFaster = new StochasticNode(); // Adding children to Root m_AI.AddNode(whistle); m_AI.AddNode(inSphere); m_AI.AddNode(runFaster); // -- CHILDREN OF WHISTLE CONDITION (1ST CHILD OF ROOT) -- Action approachAction = new Action(Actions.ApproachPlayer); ActionNode approach = new ActionNode(approachAction, this); whistle.AddNode(approach); // -- CHILDREN OF IN-SPHERE CONDITION (2ND CHILD OF ROOT) -- Condition interactingCondition = new Condition(Conditions.interacting); ConditionNode interacting = new ConditionNode(interactingCondition, this); inSphere.AddNode(interacting); // -- CHILDREN OF RUN-FASTER CONDITION (3RD CHILD OF ROOT) -- SequenceNode runAhead = new SequenceNode(); SequenceNode runWith = new SequenceNode(); runFaster.AddNode(runAhead); runFaster.AddNode(runWith); // -- CHILDREN OF INTERACTING CONDITION NODE (CHILD OF IN-SPHERE CONDITION) -- SequenceNode sitThenHunt = new SequenceNode(); SequenceNode idle = new SequenceNode(); interacting.AddNode(sitThenHunt); interacting.AddNode(idle); // -- CHILDREN OF SIT-THEN-HUNT SEQUENCE (CHILD OF SIT-THEN-HUNT SEQUENCE) -- Action sitAction = new Action(Actions.Sit); ActionNode sit = new ActionNode(sitAction, this); SequenceNode huntSequence = new SequenceNode(); sitThenHunt.AddNode(sit); sitThenHunt.AddNode(huntSequence); // -- CHILDREN OF IDLE SEQUENCE (CHILD OF IDLE SEQUENCE) -- Action sniffAction = new Action(Actions.Sniff); ActionNode randomSpot = new ActionNode(sniffAction, this); Condition tenMinutesCondition = new Condition(Conditions.tenMinutes); ConditionNode tenMinutes = new ConditionNode(tenMinutesCondition, this); idle.AddNode(randomSpot); idle.AddNode(tenMinutes); // joint child // -- CHILDREN OF RUN-AHEAD SEQUENCE (CHILD OF RUN-FASTER CONDITION) -- Action runFastAction = new Action(Actions.RunFast); ActionNode runFast = new ActionNode(runFastAction, this); Action waitAction = new Action(Actions.Wait); ActionNode turnAndWait = new ActionNode(waitAction, this); runAhead.AddNode(runFast); runAhead.AddNode(tenMinutes); // joint child tenMinutes.AddNode(huntSequence); runAhead.AddNode(turnAndWait); // -- CHILDREN OF RUN-WITH SEQUENCE (CHILD OF RUN-FASTER CONDITION) -- Action runWithAction = new Action(Actions.RunWith); ActionNode runNear = new ActionNode(runWithAction, this); runWith.AddNode(runNear); // -- CHILDREN OF HUNT SEQUENCE (CHILD OF SIT-THEN-HUNT SEQUENCE AND TEN-MINUTES CONDITION) -- Action dissapearAction = new Action(Actions.Dissapear); ActionNode dissapear = new ActionNode(dissapearAction, this); Action huntedAction = new Action(Actions.Hunted); ActionNode hunted = new ActionNode(huntedAction, this); huntSequence.AddNode(dissapear); huntSequence.AddNode(hunted); // START m_AI.StartCoroutine(m_AI.Execute()); }
// Use this for initialization public void Start() { inputData.attribute = GetComponent <CharacterAttribute>(); rootNode.nodeName += "根"; //条件 var hasNoTarget = new PreconditionNOT(() => { return(inputData.attribute.hasTarget); }); hasNoTarget.nodeName = "无目标"; var hasTarget = new Precondition(hasNoTarget); hasTarget.nodeName = "发现目标"; var isAnger = new Precondition(() => { return(inputData.attribute.isAnger); }); isAnger.nodeName = "愤怒状态"; var isNotAnger = new PreconditionNOT(isAnger); isNotAnger.nodeName = "非愤怒状态"; var HPLessThan500 = new Precondition(() => { return(inputData.attribute.health < 500); }); HPLessThan500.nodeName = "血少于500"; var HPMoreThan500 = new PreconditionNOT(HPLessThan500); HPMoreThan500.nodeName = "血大于500"; var isAlert = new Precondition(() => { return(inputData.attribute.isAlert); }); isAlert.nodeName = "警戒"; var isNotAlert = new PreconditionNOT(isAlert); isNotAlert.nodeName = "非警戒"; var patrolNode = new SequenceNode(); patrolNode.nodeName += "巡逻"; patrolNode.AddCondition(hasNoTarget); patrolNode.AddCondition(isNotAlert); patrolNode.AddNode(new PatrolAction()); var alert = new SequenceNode(); alert.nodeName += "警戒"; alert.AddCondition(hasNoTarget); alert.AddCondition(isAlert); alert.AddNode(new AlertAction()); var runaway = new SequenceNode(); runaway.nodeName += "逃跑"; runaway.AddCondition(hasTarget); runaway.AddCondition(HPLessThan500); runaway.AddNode(new RunAwayAction()); var attack = new SelectorNode(); attack.nodeName += "攻击"; attack.AddCondition(hasTarget); attack.AddCondition(HPMoreThan500); var attackCrazy = new SequenceNode(); attackCrazy.nodeName += "疯狂攻击"; attackCrazy.AddCondition(isAnger); attackCrazy.AddNode(new CrazyAttackAction()); attack.AddNode(attackCrazy); var attackNormal = new SequenceNode(); attackNormal.nodeName += "普通攻击"; attackNormal.AddCondition(isNotAnger); attackNormal.AddNode(new AttackAction()); attack.AddNode(attackNormal); rootNode.AddNode(patrolNode); rootNode.AddNode(alert); rootNode.AddNode(runaway); rootNode.AddNode(attack); var ret = rootNode.Enter(inputData); if (!ret) { Debug.Log("无可执行节点!"); } }