//添加新状态到字典 protected void AddState <T>() where T : EnemyStateBase { //添加状态类的脚本到物体 EnemyStateBase state = gameObject.AddComponent <T>(); state.OnInit(); //初始化 states.Add(state.GetType(), state); //添加到状态集合 }
void Start() { playerObject = GameObject.FindGameObjectsWithTag("Player"); friendly = GameObject.FindGameObjectsWithTag("Person"); if (!PhotonNetwork.isMasterClient) { gameObject.GetComponent <EnemyStateManager>().enabled = false; } navMeshAgent = GetComponent <NavMeshAgent>(); myStats = gameObject.GetComponent <Character>(); //get the stats attackTimer = timeBetweenAttacks; //so the AI can attack as soon as possible scanTimer = 10.0f; prevPosition = transform.position; if (!stateSetElsewhere) { switch (startingState) { case State.guarding: activeState = new GuardingState(this); Debug.Log("State: " + activeState); break; case State.patrolling: activeState = new PatrollingState(this); break; case State.wandering: activeState = new WanderingState(this); break; case State.attacking: activeState = new AttackingState(this); break; case State.retreating: activeState = new RetreatingState(this); break; case State.fleeing: activeState = new FleeingState(this); break; case State.runOut: activeState = new RunOutState(this); break; default: activeState = new GuardingState(this); break; } } //Animation mixing //animation ["attack"].layer = 2; //animation ["attack"].AddMixingTransform (upperBody); }
private void Start() { this.factory = new EnemyStateFactory(this.gameObject, this.GetComponentInChildren <Finder>(), this.dashSpeed, this.dashRotSpeed, this.breakInterval, this.actionNum); this.state = this.factory.Create(this.currentStateId); }
public void ChangeState(EnemyStateBase newState) { if (currentState != null) { currentState.Exit(); } newState.Enter(); currentState = newState; }
protected void ChangeState(EnemyStateBase state) { if (_currentState != null) { _currentState.ExitState(); } _currentState = state; _currentState.EnterState(); }
//State Related Methods//////////////////// public void SetState(Type state) { if (_currentState.GetType() == state) { return; } Debug.Log("Enemy Changing from: " + _currentState.GetType() + " to: " + state); _currentState.EndState(); _currentState = _states[state]; _currentState.BeginState(); }
private void Update() { if (GameState.state == (int)GameState.STATE.PLAY) { this.currentStateId = this.state.Update(); if (this.currentStateId != this.prevStateId) { this.state = this.factory.Create(currentStateId); } this.prevStateId = this.currentStateId; } }
//切换状态 public bool ChangeState <T>() where T : EnemyStateBase { if (!states.ContainsKey(typeof(T))) //如果不存在该状态 { return(false); } if (currentState != null) { currentState.OnExit(); //旧状态 离开回调 } currentState = states[typeof(T)]; //重新赋值当前状态 currentState.OnEnter(); //新状态 进入回调 return(true); }
public override void Start() { //Basics/////////////////////////////// base.Start(); Position = CurrentNode.Position; _EnemyStats = GetComponent <EnemyStats>(); //Setting up the Cache///////////////// _states = new Dictionary <Type, EnemyStateBase>(); _states.Add(typeof(EnemyStateWalking), new EnemyStateWalking(this)); _states.Add(typeof(EnemyStateInteractionPlayer), new EnemyStateInteractionPlayer(this)); _states.Add(typeof(EnemyStateIdle), new EnemyStateIdle(this)); //Starting First State Manually//////// _currentState = _states[typeof(EnemyStateIdle)]; _currentState.BeginState(); }
public EnemyStateBase Create(EnemyAI.STATE state) { EnemyStateBase ret = null; switch (state) { case EnemyAI.STATE.ATTACK: ret = new EnemyStateAttack(this.obj, this.finder, this.dashSpeed, dashRotSpeed); break; case EnemyAI.STATE.BREAKTIME: ret = new EnemyStateBreaktime(this.obj, this.finder, this.breakInterval); break; case EnemyAI.STATE.SEARCH: ret = new EnemyStateSearch(this.obj, this.finder, this.actionNum); break; } return(ret); }
private void Awake() { navMeshAgent = GetComponent <NavMeshAgentController>(); /* Set States */ states = new Dictionary <EnemySatate, EnemyStateBase>(); states.Add(EnemySatate.Patrol, new PatrolState(this)); states.Add(EnemySatate.Alert, new AlertState(this)); states.Add(EnemySatate.Chase, new ChaseState(this)); states.Add(EnemySatate.Fire, new FireState(this)); currentState = states[EnemySatate.Patrol]; orginalPosition = transform.position; player = GameObject.FindGameObjectWithTag("Player").transform; if (Fire) { ammo = GetComponent <AmmoInventory>(); } }
void Start() { playerObject = GameObject.FindGameObjectsWithTag ("Player"); friendly = GameObject.FindGameObjectsWithTag ("Person"); if (!PhotonNetwork.isMasterClient) { gameObject.GetComponent<EnemyStateManager>().enabled = false; } navMeshAgent = GetComponent<NavMeshAgent>(); myStats = gameObject.GetComponent<Character>(); //get the stats attackTimer = timeBetweenAttacks; //so the AI can attack as soon as possible scanTimer = 10.0f; prevPosition = transform.position; if (!stateSetElsewhere){ switch(startingState){ case State.guarding: activeState = new GuardingState(this); Debug.Log ("State: " + activeState); break; case State.patrolling: activeState = new PatrollingState(this); break; case State.wandering: activeState = new WanderingState(this); break; case State.attacking: activeState = new AttackingState(this); break; case State.retreating: activeState = new RetreatingState(this); break; case State.fleeing: activeState = new FleeingState(this); break; case State.runOut: activeState = new RunOutState(this); break; default: activeState = new GuardingState(this); break; } } //Animation mixing //animation ["attack"].layer = 2; //animation ["attack"].AddMixingTransform (upperBody); }
public void setState(EnemyStateBase state) { stateSetElsewhere = true; activeState = state; }
//Method that change's the AI's current active state public void SwitchState(EnemyStateBase newState) { activeState = newState; }
/* Change States */ public void MakeTransition(EnemySatate state) { currentState = states[state]; currentState.StartState(); }