Example #1
0
        /// <summary>
        /// Logic for when the AI is in the AttackTarget state. Will make sure target is still a valid one at try to deal damage.
        /// If the target is no longer valid it will try to reaquire it or find a new target.
        /// </summary>
        private void StateCheckAttackTarget()
        {
            if (!IsTargetAlive())
            {
                SetToIdle();
                return;
            }

            if (!CheckLineOfSight())
            {
                if (!IsInCover)
                {
                    SetToMoving();
                }
                else
                {
                    SetToIdle();
                }
                return;
            }

            if (CheckInRange(CurrTarget.Trans.position, CurrWeapon.Range))
            {
                if (CurrCoverType == CoverType.No || CurrCoverType == CoverType.Up)
                {
                    trans.LookAt(CurrTarget.Trans);
                }

                if (fireTimer >= CurrWeapon.FireRate && (CurrWeapon.Type != WeaponType.rifle || CurrWeapon.CanFireBurst()))
                {
                    bool isTargetInCover = CurrTarget.IsInCover;

                    // Check if they are flanking
                    if (isTargetInCover)
                    {
                        Vector3 startPos  = trans.position + (Vector3.up * GUNSHOTDIST);
                        Vector3 direction = CurrTarget.Trans.position + (Vector3.up * INCOVERDIST) - startPos;
                        direction.Normalize();

                        Ray        ray = new Ray(startPos, direction);
                        RaycastHit hit;
                        if (Physics.Raycast(ray, out hit, 1000))
                        {
                            if (hit.collider.gameObject.GetInstanceID() == CurrTarget.gameObject.GetInstanceID())
                            {
                                isTargetInCover = false;
                            }
                        }
                    }

                    if (CurrWeapon.GetRandomAccuracy(isTargetInCover, Vector3.Distance(CurrTarget.Trans.position, trans.position)))
                    {
                        float damage = CurrWeapon.GetDamage(critChanceModifier);
                        CurrTarget.TakeDamage(damage, this);
                        PopupTextManager.Instance.NewPopupText(damage.ToString("F2"), CurrTarget.Trans.position, Color.red);
                    }
                    else
                    {
                        PopupTextManager.Instance.NewPopupText("Missed", CurrTarget.Trans.position, Color.red);
                    }

                    CurrWeapon.PlayGunshot();
                    BulletHandler.Instance.CreateBullet(currWeapon.transform.position, GetSightCheckPosition(CurrTarget.transform, CurrTarget.CoverNormal, CoverType.No));

                    if (CurrWeapon.Type != WeaponType.rifle || CurrWeapon.FinishedBurst())
                    {
                        fireTimer = 0.0f;
                    }
                }
            }
            else
            {
                if (!IsInCover)
                {
                    SetToMoving();
                }
                else
                {
                    SetToIdle();
                }
            }
        }