/// <summary>
 /// Start this instance.
 /// </summary>
 void Start()
 {
     // Get all AI states from this gameobject
     IAiState[] states = GetComponents <IAiState>();
     if (states.Length > 0)
     {
         foreach (IAiState state in states)
         {
             // Add state to list
             aiStates.Add(state);
         }
         if (defaultState != null)
         {
             // Set active and previous states as default state
             previousState = currentState = GetComponent(defaultState) as IAiState;
             if (currentState != null)
             {
                 // Go to active state
                 ChangeState(currentState.GetType().ToString());
             }
             else
             {
                 Debug.LogError("Incorrect default AI state " + defaultState);
             }
         }
         else
         {
             Debug.LogError("AI have no default state");
         }
     }
     else
     {
         Debug.LogError("No AI states found");
     }
 }
예제 #2
0
파일: Mob.cs 프로젝트: zvinch/SharpDungeon
        public override void RestoreFromBundle(Bundle bundle)
        {
            base.RestoreFromBundle(bundle);

            var state = bundle.GetString(STATE);

            if (state.Equals(AIStateSleeping.Tag))
            {
                State = Sleepeing;
            }
            else
            if (state.Equals(Wandering.Tag))
            {
                State = WANDERING;
            }
            else
            if (state.Equals(Hunting.TAG))
            {
                State = HUNTING;
            }
            else
            if (state.Equals(Fleeing.Tag))
            {
                State = FLEEING;
            }
            else
            if (state.Equals(Passive.Tag))
            {
                State = PASSIVE;
            }

            Target = bundle.GetInt(TARGET);
        }
예제 #3
0
파일: Mob.cs 프로젝트: zvinch/SharpDungeon
        public override void Add(Buff buff)
        {
            Actor.Add(buff);
            if (buff is Amok)
            {
                if (base.Sprite != null)
                {
                    base.Sprite.ShowStatus(CharSprite.Negative, TxtRage);
                }

                State = HUNTING;
            }
            else
            if (buff is Terror)
            {
                State = FLEEING;
            }
            else
            if (buff is Sleep)
            {
                if (base.Sprite != null)
                {
                    new Flare(4, 32).Color(0x44ffff, true).Show(base.Sprite, 2f);
                }

                State = Sleepeing;
                Postpone(Sleep.Sws);
            }
        }
예제 #4
0
 private void Awake()
 {
     _mover                = GetComponent <IDirectionMoverComponent>();
     _fieldOfView          = GetComponent <IFieldOfView>();
     CharacterStateMachine = GetComponent <ICharacterStateMachine>();
     _currentState         = wanderState;
 }
예제 #5
0
    void Update()
    {
        playerInSightRange  = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
        playerInAttachRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);

        currentState     = currentState.DoState(this);
        currentStateName = currentState.ToString();
    }
    /// <summary>
    /// Send OnStateEnter notification to new state.
    /// </summary>
    private void NotifyOnStateEnter()
    {
        string   prev   = previousState.GetType().ToString();
        string   active = currentState.GetType().ToString();
        IAiState state  = GetComponent(active) as IAiState;

        state.OnStateEnter(prev, active);
    }
예제 #7
0
 public void AddState(string name, IAiState state)
 {
     _states.Add(name, state);
     if (CurrentState == null)
     {
         CurrentState = name;
     }
 }
예제 #8
0
        /// <summary>
        /// Gets if the Ai state stack contains a reference to the Ai state instance
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HasState(IAiState value)
        {
            Validation.IsNotNull(value, "value");

            var stateList = _stateStack.ToList();

            return(stateList.Any(aiState => aiState == value));
        }
 /// <summary>
 /// Set AI to defalt state.
 /// </summary>
 public void GoToDefaultState()
 {
     previousState = currentState;
     currentState  = GetComponent(defaultState) as IAiState;
     NotifyOnStateExit();
     DisableAllStates();
     EnableNewState();
     NotifyOnStateEnter();
 }
예제 #10
0
        /// <summary>
        /// Pushes a new state onto the Ai state stack.
        /// </summary>
        /// <param name="state">Instance object that is implements the IAiState interface</param>
        public void PushState(IAiState state)
        {
            Validation.IsNotNull(state, "state");

            Messages.Add(string.Format(Resources.MSG_STATE_PUSHED, Owner.ID, Owner.Name, state.ID, state.Name));
            _stateStack.Push(state);

            OnStateChange?.Invoke(this, null);
        }
예제 #11
0
파일: Mob.cs 프로젝트: zvinch/SharpDungeon
        public virtual void Beckon(int cell)
        {
            Notice();

            if (State != HUNTING)
            {
                State = WANDERING;
            }

            Target = cell;
        }
예제 #12
0
파일: Mob.cs 프로젝트: zvinch/SharpDungeon
        public override void Remove(Buff buff)
        {
            base.Remove(buff);
            if (!(buff is Terror))
            {
                return;
            }

            base.Sprite.ShowStatus(CharSprite.Negative, TxtRage);
            State = HUNTING;
        }
예제 #13
0
파일: Mob.cs 프로젝트: zvinch/SharpDungeon
        public override void Damage(int dmg, object src)
        {
            Terror.Recover(this);

            if (State == Sleepeing)
            {
                State = WANDERING;
            }
            Alerted = true;

            base.Damage(dmg, src);
        }
 /// <summary>
 /// Change Ai state.
 /// </summary>
 /// <param name="state">State.</param>
 public void ChangeState(string state)
 {
     if (state != "")
     {
         // Try to find such state in list
         foreach (IAiState aiState in aiStates)
         {
             if (state == aiState.GetType().ToString())
             {
                 previousState = currentState;
                 currentState  = aiState;
                 NotifyOnStateExit();
                 DisableAllStates();
                 EnableNewState();
                 NotifyOnStateEnter();
                 return;
             }
         }
         Debug.Log("No such state " + state);
         // If have no such state - go to default state
         GoToDefaultState();
         Debug.Log("Go to default state " + aiStates[0].GetType().ToString());
     }
 }
예제 #15
0
 private void OnEnable()
 {
     currentState = patrolingState;
 }
예제 #16
0
 // Update is called once per frame
 void Update()
 {
     _currentState = _currentState.Execute(this, Time.deltaTime);
 }