示例#1
0
    public void SwitchState(EnemyBaseState newstate)
    {
        ExitState();

        newstate.EnterState();
        _ctx.currentState = newstate;
    }
 public void TransitionToState(EnemyBaseState state)
 {
     // Set current state field to the param.
     currentState = state;
     //call enter state message as "this" current instance of the fsm class.
     currentState.EnterState(this);
 }
示例#3
0
 /// <summary>
 /// 切换状态的方法
 /// </summary>
 /// <param name="state">传入一个状态机对象切换敌人的状态,切换之后进入该状态</param>
 public void TransitionToState(EnemyBaseState state)
 {
     // 切换状态 当前状态 = 传入的状态
     _currentState = state;
     // 切换之后敌人进入当前状态
     _currentState.EnterState(this);
 }
示例#4
0
    private void OnCollisionEnter(Collision collision)
    {
        if (thrown)
        {
            Collider[] colls = Physics.OverlapSphere(transform.position, radius);
            foreach (Collider col in colls)
            {
                Vector3 hit;
                if (col.gameObject.CompareTag("Player"))
                {
                    hit = col.ClosestPoint(transform.position);

                    Player player = col.gameObject.GetComponent <Player>();
                    player.Damage(damage - hit.x - hit.z);
                }
                if (col.gameObject.CompareTag("Enemy"))
                {
                    hit = col.ClosestPoint(transform.position);

                    EnemyBaseState enemyState = (EnemyBaseState)col.gameObject.GetComponent <Enemy>().GetCurrentState();

                    //enemyState.Damage(damage - hit.x - hit.z);
                    enemyState.Damage(damage);
                }
            }
            Instantiate(explosion1, transform.position, Quaternion.identity);
            Instantiate(explosion2, transform.position, Quaternion.identity);

            LoseDurability();
        }
    }
 private void SwitchToNewState(Type nextState)
 {
     currState.Exit();
     currState = availableStates[nextState];
     currState.Enter();
     OnStateChanged?.Invoke(currState); //??
 }
示例#6
0
 private void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.CompareTag("Enemy") && rb.velocity.magnitude >= lowestVelocityToDoDamage)
     {
         EnemyBaseState enemyState = (EnemyBaseState)collision.collider.GetComponent <Enemy>().GetCurrentState();
         enemyState.Damage(rb.velocity.magnitude, rb.mass, damage);
         LoseDurability();
     }
 }
示例#7
0
 public void SetEnemyState(EnemyBaseState newState)
 {
     if (currentState != null)
     {
         currentState.ExitState(this);
     }
     currentState = newState;
     currentState.EnterState(this);
 }
    protected virtual void Hurt()
    {
        flinchTimer -= Time.deltaTime;

        if (flinchTimer <= 0f)
        {
            flinchTimer = 0f;
            enemyState  = previousState;
            //enemyState = EnemyBaseState.Pursuit;
        }
    }
    //==================================//
    // FUNCTIONS FOR HANDLING STATES    //
    // Note: State switching has to be  //
    // handled in derived classes       //
    //==================================//
    //NOTE: These are default behaviors. Custom behaviors
    //      are implemented in derived classes by overriding
    //      these functions

    protected virtual void Roam()
    {
        //Check radius to see if player is within range
        float distance = Vector2.Distance(transform.position, playerTransform.position);

        if (distance <= pursuitRange)
        {
            //If within range, switch to pursuit state
            enemyState = EnemyBaseState.Pursuit;
        }
    }
    private void Update()
    {
        if (currState == null)
        {
            currState = availableStates.Values.First(); //first state is default
        }

        var nextState = currState?.Tick();

        if (nextState != null && nextState != currState?.GetType())
        {
            SwitchToNewState(nextState);
        }
    }
示例#11
0
    public void UpdateStateMachine()
    {
        //Debug.Log(_currentState);
        if (_currentState == null)
        {
            _currentState = _possibleStates.First();
        }

        var nextState = _currentState?.UpdateState();

        if (nextState != null &&
            nextState != _currentState.GetType())
        {
            ChangeState(nextState);
        }
    }
    protected virtual void ChargeAttack()
    {
        //When attack charge animation is over,
        //switch to attack state
        attackTimer -= Time.deltaTime;

        if (attackTimer <= 0f)
        {
            attackTimer = attackDurationTime;
            enemyState  = EnemyBaseState.Attacking;

            //Get player's position at this point in time
            Vector2 attackDirection = (Vector2)playerTransform.position - (Vector2)transform.position;
            attackDirection.Normalize();
            attackInfo.direction = attackDirection;
        }
    }
    protected virtual void Search()
    {
        timer -= Time.deltaTime;

        if (timer <= 0f)
        {
            timer      = roamPauseTime;
            enemyState = EnemyBaseState.Roaming;
        }

        //Check if player is within range
        float distance = Vector2.Distance(transform.position, playerTransform.position);

        if (distance <= pursuitRange)
        {
            //If player is within range, switch to pursuit state
            enemyState = EnemyBaseState.Pursuit;
            timer      = 0f;
        }
    }
    //Function for when the enemy is hit
    protected virtual void ObjectHit(AttackInfoContainer obj)
    {
        Debug.Log("DASH ENEMY HIT");
        healthComponent.TakeDamage(obj.damage);

        //If already in attack state, don't interrupt it
        if (enemyState != EnemyBaseState.Attacking)
        {
            //Change to hurt state
            flinchTimer = flinchTime;

            //If already in hurt state, don't update previous state
            if (enemyState != EnemyBaseState.Hurt)
            {
                previousState = enemyState;
            }
            enemyState = EnemyBaseState.Hurt;
        }

        rb.AddForce(obj.direction * obj.force);
    }
    // Update is called once per frame
    private void Update()
    {
        if (CurrentState == null) // set state
        {
            CurrentState = enemyStates[typeof(SpawnState)];
        }

        if (CurrentState == enemyStates[typeof(SpawnState)])
        {
            LookAtPlayer.enabled = false;
        }
        else
        {
            LookAtPlayer.enabled = true;
        }

        Type nextState = CurrentState?.StatePerform(); // get next state

        if (nextState != null && nextState != CurrentState?.GetType())
        {
            SwitchToNewState(nextState);
        }
    }
    protected virtual void Pursuit()
    {
        //Check radius to see if player got out of range
        float distance = Vector2.Distance(transform.position, playerTransform.position);

        if (distance > pursuitRange)
        {
            //Player has escaped enemy
            enemyState   = EnemyBaseState.Searching;
            pursuitState = PursuitState.Chasing;             //reset
            timer        = searchTime;
        }

        //Handle pursuit substates
        //Check if we are within attacking distance and not too close to player
        if (distance <= stoppingDistance && distance >= retreatDistance)
        {
            //Stop moving for a better attack shot
            //pursuitState = PursuitState.Stopped;
            rb.velocity = Vector2.zero; //stop enemy movement
            enemyState  = EnemyBaseState.ChargingAttack;
            timer       = 0f;           //reset
            attackTimer = attackChargeTime;

            //Face the target as well
        }
        else if (distance < retreatDistance)
        {
            //If too close to player, retreat
            pursuitState = PursuitState.Retreating;
        }
        else
        {
            //Keep chasing
            pursuitState = PursuitState.Chasing;
        }
    }
    //Reference to animator

    // Use this for initialization
    protected virtual void Start()
    {
        //Get player's script and controller
        //Get animator component
        //anim = GetComponent<Animator>();
        Debug.Log("Base Start");

        //Initialize to default values
        attackInfo            = GetComponent <AttackInfoContainer> ();
        attackInfo.attackType = AttackType.MeleeWeakAttack;
        attackInfo.direction  = Vector2.zero;
        attackInfo.force      = baseAttackForce;

        //playerObject = GameObject.Find ("Player");
        playerController = GameObject.Find("Player").GetComponent <PlayerController>();
        playerTransform  = GameObject.Find("Player").GetComponent <Transform> ();
        rb           = GetComponent <Rigidbody2D> ();
        enemyState   = EnemyBaseState.Roaming;
        pursuitState = PursuitState.Chasing;
        attackState  = AttackState.Attacking;
        timer        = 0f;
        attackTimer  = 0f;
        flinchTimer  = 0f;
    }
    protected virtual void Attack()
    {
        switch (attackState)
        {
        case AttackState.Attacking:
            //NOTE: Will have to change this so that
            //      we switch out of this state when
            //      the attack animation is over.
            attackTimer -= Time.deltaTime;

            if (attackTimer <= 0f)
            {
                attackTimer = 0f;                 //reset
                attackState = AttackState.Cooldown;
                timer       = attackCooldownTime;
            }

            break;

        case AttackState.Cooldown:
            //When cooldown animation is over, switch to pursuit state
            //For this, we use the regular "timer" instead of
            //attack timer because cooldown can be interrupted
            //by the player attacking
            timer -= Time.deltaTime;

            if (timer <= 0f)
            {
                timer       = 0f;                    //reset
                attackState = AttackState.Attacking; //reset
                enemyState  = EnemyBaseState.Pursuit;
            }

            break;
        }
    }
示例#19
0
 private void ChangeState(Type nextState)
 {
     _currentState = _possibleStates.FirstOrDefault(s => s.GetType() == nextState);
 }
示例#20
0
 public void ChangeState(EnemyBaseState newState)
 {
     currentState.Exit();
     currentState = newState;
     newState.Enter();
 }
 private void SwitchToNewState(Type nextState)
 {
     CurrentState = enemyStates[nextState];
     OnStateChanged?.Invoke(CurrentState);
 }
示例#22
0
文件: Enemy.cs 项目: LsaNa1s/MyGame
 public void TransitionToState(EnemyBaseState state) //状态机切换
 {
     currentState = state;                           //进入状态方法
     currentState.EnterState(this);
 }
示例#23
0
 public void SetEnemyState(EnemyBaseState newState)
 {
     _state = newState;
 }
示例#24
0
 public void TransitionToState(EnemyBaseState state)
 {
     currentState = state;
     currentState.EnterState(this);
 }
示例#25
0
 public Enemy()
 {
     _state = new EnemyStandingState(this);
 }