void Start() { makeDestroy(); thethis = this.GetComponent <PlayerBasic> (); theMoveController = this.GetComponentInChildren <NavMeshAgent> (); theAttackLlinkController = this.GetComponent <attackLinkController> (); theAnimator = this.GetComponentInChildren <Animator> (); //print ("AI stage is inited"); theStatesSaved = new List <FSMBasic> (); //这里需要建立一个初始的状态 //theStateNow = new FSM_Search (); theStateNow = getState(4); theStateNow.makeState(theMoveController, theAttackLlinkController, theAnimator, thethis); theStateNow.OnFSMStateStart(); makeAIStart(); //AI重新激活,刷新时间 //带MeshgRenderer的有部分可优化之处 MeshRenderer theRender = this.GetComponentInChildren <MeshRenderer>(); if (theRender) { theRender.gameObject.AddComponent <FSMRenderSwitch> (); } this.transform.root.tag = "AI"; //打上标记方便找 move theMoveControllerForPlayer = this.GetComponent <move> (); if (theMoveControllerForPlayer) { theMoveControllerForPlayer.enabled = false; //AI不需要这个move控制移动 } }
public override void OnBeAttack(PlayerBasic attacker) { if (this.theTypeForAI == AIType.NET) { return; } //Vector3 minus = new Vector3 (0f , attacker.transform.rotation.eulerAngles.y - this.transform.rotation.eulerAngles.y , 0f); //this.transform.rotation = Quaternion.Lerp(this.transform.rotation , Quaternion.Euler(minus + this.transform .rotation.eulerAngles) , 360f); this.transform.LookAt(attacker.transform); //FSM_Attack attack = new FSM_Attack (); //attack.makeState (this.theMoveController, this.theAttackLlinkController,this.theAnimator, this.thethis ,attacker); //theStateNow = attack; theStateNow = getState(1); theStateNow.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.thethis, attacker); theStateNow.OnFSMStateStart(); think(); //强制思考一下,否则会有延迟 // try // { // NavMeshAgent theAgent = this.GetComponent <NavMeshAgent> (); // if(theAgent.isActiveAndEnabled) // { // theAgent.isStopped = true; // theAgent.isStopped = false; // theAgent.SetDestination (this.transform .position); // } // } // catch // { // print ("当前处于无法被设置目标的状态,因此AI会继续向原先的目的地移动"); // } angerTimer = angetTimerMax; //收到攻击的时候刷新仇恨 makeAIStart(); //AI重新激活,刷新时间 }
public override FSMBasic moveToNextState(FSMStage theController) { if (theEMY == null || theEMY.isAlive == false) { //Debug.Log ("attack to search"); //FSM_Search search = new FSM_Search (); FSMBasic search = theController.getState(4); search.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis); return(search); } if (this.theThis.transform.position.y < this.theEMY.transform.position.y - 0.75) { //Debug.Log ("attack to jump"); //FSM_Jump jump = new FSM_Jump (); FSMBasic jump = theController.getState(2); jump.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theEMY); return(jump); } if (Vector3.Distance(this.theThis.transform.position, this.theEMY.transform.position) > this.theThis.theAttackAreaLength * 0.8f) { //Debug.Log ("attack to runafter"); //FSM_RunAfter runafter = new FSM_RunAfter (); FSMBasic runafter = theController.getState(3); runafter.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theEMY); return(runafter); } return(this); }
//有关AI计算状态----------------------------------------------------------- public void moveTotheState(FSMBasic theStateIn, PlayerBasic aim) { theStateIn.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.thethis, aim); theStateNow.OnFSMStateEnd(); theStateNow = theStateIn; theStateNow.OnFSMStateStart(); }
public override FSMBasic moveToNextState(FSMStage theController) { //为了确保能真的攻击到,留下多余空间 //this.theThis.theAttackAreaLength*1.5f在没有遇到之前就开始会挥舞兵器了 //至于移动就在于navMeshAgent了 if (Vector3.Distance(this.theThis.transform.position, this.theEMY.transform.position) <= this.theThis.theAttackAreaLength * 1.5f) { //Debug.Log ("runafter to attack"); //FSM_Attack attack = new FSM_Attack (); FSMBasic attack = theController.getState(1); attack.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theEMY); return(attack); } if (this.theThis.transform.position.y < this.theEMY.transform.position.y - 0.75) { //Debug.Log ("runafter to jump"); //FSM_Jump jump = new FSM_Jump (); FSMBasic jump = theController.getState(2); jump.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theEMY); return(jump); } //Debug.Log ("theEMY name is "+ theEMY.name); if (timer < 0) { //Debug.Log ("runafter to search"); //FSM_Search search = new FSM_Search (); FSMBasic search = theController.getState(4); search.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis); return(search); } return(this); }
public override FSMBasic moveToNextState(FSMStage theController) { if (this.theThis.transform.position.y >= this.theEMY.transform.position.y - 0.75 || timer < 0) { //Debug.Log ("jump to runafter"); //FSM_RunAfter runafter = new FSM_RunAfter (); FSMBasic runafter = theController.getState(3); runafter.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theEMY); this.theMoveController.enabled = true; return(runafter); } return(this); }
void think() { if (theStateNow != null && thethis.isAlive) { //AI转换状态 FSMBasic theStateNew = theStateNow.moveToNextState(this); //思考进入到下一个状态或许可以慢一点进行 if (theStateNew.geID() != theStateNow.geID()) { theStateNow.OnFSMStateEnd(); //结束效果 theStateNow = theStateNew; theStateNow.OnFSMStateStart(); //开始效果 } } }
//初始化 void Start() { this.transform.root.tag = "AI"; //打上标记方便找 thethis = this.GetComponent <Acter> (); theMoveController = this.GetComponentInChildren <NavMeshAgent> (); theAnimator = this.GetComponentInChildren <Animator> (); theStateNow = new FSM_Search(); theStateNow.makeState(theMoveController, theAnimator, thethis, null); theStateNow.setArea(attackarea, searchArea); theStateNow.OnFSMStateStart(); this.enabled = false; }
public override FSMBasic moveToNextState(FSMStage theContrtoller) { //找不到目标就继续找 if (theMainEMY == null) { return(this); } //找到了目标就转到下一个状态 else { //Debug.Log ("search to attack"); FSMBasic attack = theContrtoller.getState(1); attack.makeState(this.theMoveController, this.theAttackLlinkController, this.theAnimator, this.theThis, this.theMainEMY); return(attack); } }
//有关AI计算状态----------------------------------------------------------- //很多操作都是连续的,对于AI来说或许用连续的方法计算会比较好 void Update() { //出于优化考虑不必让AI一直计算下去 //此外这也可是“怪物僵直”状态的一个做法 if (theAiIsActing && thethis.isAlive && theStateNow != null) { theStateNow.actInThisState(); FSMBasic theStateNew = theStateNow.moveToNextState(); if (theStateNew != theStateNow) { OnStateChange(theStateNow, theStateNew); theStateNew.setArea(attackarea, searchArea); } theStateNow = theStateNew; } }
//有些东西在这里总控制会比较方便 //例如头顶生命条没有必要每一个AIState保留引用的 void OnStateChange(FSMBasic oldState = null, FSMBasic newState = null) { if (!thethis.theBloodSlider) { thethis.MakeHpSlider(); } if (newState.theThis && thethis.theBloodSlider) { if (newState is FSM_RunAfter || newState is FSM_Attack) { thethis.theBloodSlider.gameObject.SetActive(true); } else { thethis.theBloodSlider.gameObject.SetActive(false); } } }
public FSMBasic getState(int ID) { for (int i = 0; i < theStatesSaved.Count; i++) { if (theStatesSaved [i].geID() == ID) { return(theStatesSaved [i]); } } FSMBasic theOne = new FSMBasic(); switch (ID) { case 1: { theOne = new FSM_Attack(); theStatesSaved.Add(theOne); } break; case 2: { theOne = new FSM_Jump(); theStatesSaved.Add(theOne); } break; case 3: { theOne = new FSM_RunAfter(); theStatesSaved.Add(theOne); } break; case 4: { theOne = new FSM_Search(); theStatesSaved.Add(theOne); } break; } return(theOne); }
/// <summary> /// 强制显示生命条 /// </summary> public void GotAim() { if (!thethis.theBloodSlider) { thethis.MakeHpSlider(); } if (this.theStateNow is FSM_Search) { FSMBasic theStateNew = new FSM_RunAfter(); theStateNew.makeState(this.theMoveController, this.theAnimator, theStateNow.theThis, theStateNow.theAim); theStateNew.setArea(attackarea, searchArea); if (theStateNew != theStateNow) { OnStateChange(theStateNow, theStateNew); } theStateNow = theStateNew; } }