Esempio n. 1
0
    private void Update()
    {
        /** https://youtu.be/LPNSh9mwT4w?t=1284 */
        //code for setting attack animation
        //animator.SetBool("attacking",true);

        //check for ground in front of enemy


        Vector2 lineCastPos   = GetLineCastPosition();
        Vector2 playerCastPos = GetPlayerCastPosition();

        Vector2 groundCheckPos = GetGroundCheckPosition(lineCastPos);
        Vector2 wallCheckPos   = GetWallCheckPosition(lineCastPos);

        bool isGrounded = Physics2D.Linecast(lineCastPos + toVec2(-transform.right) * 0.3f + Vector2.up * 0.2f, groundCheckPos + Vector2.up * 0.1f, enemyMask);

        Debug.DrawLine(lineCastPos + toVec2(-transform.right) * 0.3f + Vector2.up * 0.2f, groundCheckPos + Vector2.up * 0.1f, isGrounded ? Color.red : Color.white);

        bool iAmAboutToHurtMyFeetsies = Physics2D.Linecast(lineCastPos, groundCheckPos + Vector2.up * 0.1f + toVec2(-transform.right) * 0.1f, avoidWalkingOnMask);

        Debug.DrawLine(lineCastPos, groundCheckPos + Vector2.up * 0.1f + toVec2(-transform.right) * 0.1f, iAmAboutToHurtMyFeetsies ? Color.red : Color.grey);

        bool isBlocked = Physics2D.Linecast(lineCastPos, wallCheckPos, enemyMask) || iAmAboutToHurtMyFeetsies;

        Debug.DrawLine(lineCastPos, wallCheckPos, isBlocked ? Color.red : Color.white);

        attacking = Physics2D.Linecast(playerCastPos, playerCastPos + toVec2(-transform.right) * 0.1f, attackMask);
        Debug.DrawLine(playerCastPos, playerCastPos + toVec2(-transform.right) * 0.1f, attacking ? Color.red : Color.blue);

//		if (!isBlocked)
//		{
//			isBlocked = Physics2D.Linecast(playerCastPos, playerCastPos - toVec2(enemyTrans.right) / 8, enemyMask);
//		}

        //if attacking, lock him into an attack where he doesn't do anything else

        if (remainingAttackTime <= 0)
        {
            remainingAttackTime = attackTime;
            animator.SetBool("attacking", false);
        }
        else
        {
            remainingAttackTime -= Time.deltaTime;
        }


        //check to see if player is in front
        if (attacking)
        {
            walking = false;
            animator.SetBool("attacking", true);
        }
        else
        {
            walking = true;
            animator.SetBool("attacking", false);
        }

        if (walking)
        {
            //if no ground ahead, turn around
            if (!isGrounded || isBlocked)
            {
                //pause before changing direction
                if (timer > 0)
                {
                    animator.SetFloat("speed", 0);                     //play idle animation

                    timer -= Time.deltaTime;
                    return;
                }

                timer = rand.Next(1, maxTurnDelaySeconds);

                //flip Y 180 degrees
                Vector3 currentRotation = enemyTrans.eulerAngles;
                currentRotation.y     += 180;
                enemyTrans.eulerAngles = currentRotation;
            }

            //move forwards
            Vector2 enemyVelocity = m_rb.velocity;
            enemyVelocity.x = enemyTrans.right.x * speed;
            m_rb.velocity   = enemyVelocity;
            animator.SetFloat("speed", m_rb.velocity.x * m_rb.velocity.x);             //play walking animation
        }
        //else attacking
        else
        {
            player.AttemptTakeDamage(DamageDealt);
        }
    }
Esempio n. 2
0
    private void Update()
    {
        //todo: have to clean this whole f****r up.
        Vector2 lineCastPos   = GetLineCastPosition();
        Vector2 playerCastPos = GetPlayerCastPosition();

        Vector2 groundCheckPos = GetGroundCheckPosition(lineCastPos);
        Vector2 wallCheckPos   = GetWallCheckPosition(lineCastPos);

        //todo: make these use the facing and also clean it the hell up. Build the debug draw into a method.
        bool isGrounded = Physics2D.Linecast(lineCastPos + toVec2(transform.right * _facingDirection) * 0.3f + Vector2.up * 0.2f,
                                             groundCheckPos + Vector2.up * 0.1f, _avoidWalkingIntoMask);

        Debug.DrawLine(lineCastPos + toVec2(transform.right * _facingDirection) * 0.3f + Vector2.up * 0.2f,
                       groundCheckPos + Vector2.up * 0.1f, isGrounded ? Color.red : Color.white);

        bool iAmAboutToHurtMyFeetsies = Physics2D.Linecast(lineCastPos,
                                                           groundCheckPos + Vector2.up * 0.1f + toVec2(transform.right * _facingDirection) * 0.1f, _avoidWalkingOnMask);

        Debug.DrawLine(lineCastPos, groundCheckPos + Vector2.up * 0.1f + toVec2(transform.right * _facingDirection) * 0.1f,
                       iAmAboutToHurtMyFeetsies ? Color.red : Color.grey);

        bool isBlocked = Physics2D.Linecast(lineCastPos + toVec2(transform.right * -0.1f * _facingDirection), wallCheckPos, _avoidWalkingIntoMask) ||
                         iAmAboutToHurtMyFeetsies;

        Debug.DrawLine(lineCastPos + toVec2(transform.right * -0.1f * _facingDirection), wallCheckPos, isBlocked ? Color.red : Color.white);

        bool attackableInRange =
            Physics2D.Linecast(playerCastPos, playerCastPos + toVec2(transform.right * _facingDirection) * 0.25f, _hostileToMask);

        Debug.DrawLine(playerCastPos, playerCastPos + toVec2(transform.right * _facingDirection) * 0.25f,
                       attackableInRange ? Color.red : Color.blue);


        //Debug.Log(_currentState);
        var dT = Time.deltaTime;

        switch (_currentState)
        {
        case EnemyState.Idle:
        {
            RunEnemyTimers(_idleSprites, _idleDuration);
            if (StateTimerExceeds(_idleDuration))
            {
                TurnAround();
                ChangeStateTo(EnemyState.Patrolling);
            }
            else if (attackableInRange || PlayerDetected())
            {
                ChangeStateTo(EnemyState.Alerted);
                _soundManager.PlayHurtSFX();
            }

            break;
        }

        case EnemyState.Patrolling:
        {
            RunEnemyTimers(_walkSprites, _walkCycleDuration);
            _rb.velocity = new Vector2(_movementSpeed * _facingDirection, _rb.velocity.y);

            if (attackableInRange || PlayerDetected())
            {
                ChangeStateTo(EnemyState.Alerted);
                FacePlayer();
                _soundManager.PlayHurtSFX();
            }

            bool decideToStop = StateTimerExceeds(_patrolDuration) ||
                                iAmAboutToHurtMyFeetsies ||
                                isBlocked || !isGrounded ||
                                Math.Abs(_rb.velocity.x) < 0.01f;
            if (decideToStop)
            {
                ChangeStateTo(EnemyState.Idle);
            }

            break;
        }

        case EnemyState.Alerted:
        {
            RunEnemyTimers(_idleSprites, _alertDuration);

            if (StateTimerExceeds(_alertDuration))
            {
                if (PlayerDetected() || attackableInRange)
                {
                    if (isGrounded)
                    {
                        ChangeStateTo(EnemyState.Pursuing);
                        FacePlayer();
                    }
                    else
                    {
                        FacePlayer();
                        ChangeStateTo(EnemyState.Windup);
                    }
                }
                else if (iAmAboutToHurtMyFeetsies || isBlocked || !isGrounded)
                {
                    ChangeStateTo(EnemyState.Idle);
                }
                else
                {
                    if (_rng.NextDouble() < 0.33)
                    {
                        TurnAround();                                 //this is to make them look a little confused sometimes.
                    }

                    ChangeStateTo(EnemyState.Idle);
                }
            }

            break;
        }

        case EnemyState.Pursuing:
        {
            RunEnemyTimers(_walkSprites, _chasingCycleDuration);
            _rb.velocity = new Vector2(!isBlocked ? _pursuitSpeed * _facingDirection : _rb.velocity.x * 0.99f, _rb.velocity.y);

            bool decideToStop = StateTimerExceeds(_pursueDuration) ||
                                iAmAboutToHurtMyFeetsies ||
                                isBlocked || !isGrounded ||
                                Math.Abs(_rb.velocity.x) < 0.01f;
            if (decideToStop)
            {
                if (!isGrounded)
                {
                    _rb.velocity = new Vector2(_rb.velocity.x * 0.5f, _rb.velocity.y);
                }
                ChangeStateTo(EnemyState.Alerted);
            }

            if (attackableInRange)
            {
                ChangeStateTo(EnemyState.Windup);
            }

            if (PlayerDetected())
            {
                _stateTimer -= dT;
            }

            break;
        }

        case EnemyState.Windup:
        {
            RunEnemyTimers(_windupSprites, _attackWindupDuration);
            if (StateTimerExceeds(_attackWindupDuration))
            {
                ChangeStateTo(EnemyState.Attacking);
            }

            break;
        }

        case EnemyState.Attacking:
        {
            RunEnemyTimers(_attackSprites, _attackOutDuration);

            var filter = new ContactFilter2D();
            filter.layerMask    = _hostileToMask;
            filter.useLayerMask = true;
            Collider2D[] contacts = new Collider2D[1];
            _attackHurtbox.OverlapCollider(filter, contacts);
            if (contacts[0] != null)
            {
                //Debug.Log(contacts[0]);
                _player.AttemptTakeDamage(_damageDealt);
                _player.GetComponent <Rigidbody2D>().velocity
                    += new Vector2(Math.Sign(_player.transform.position.x - transform.position.x) * 5f, 0);
            }

            if (StateTimerExceeds(_attackOutDuration))
            {
                ChangeStateTo(EnemyState.Recovery);
            }

            break;
        }

        case EnemyState.Recovery:
        {
            RunEnemyTimers(_recoverySprites, _attackRecoveryDuration);
            if (StateTimerExceeds(_attackRecoveryDuration))
            {
                FacePlayer();
                ChangeStateTo(EnemyState.Alerted);
            }

            break;
        }
        }
    }