Exemple #1
0
        // PRIVATE METHODS: ----------------------------------------------------

        private void ShootGeneric(CharacterShooter shooter, float deviation,
                                  CharacterShooter.ShotType shotType)
        {
            shooter.UpdateShootFireRate(this.ammoID);
            shooter.PlayAudio(this.audioShoot);

            if (this.animationShoot && shooter.animator != null)
            {
                shooter.animator.CrossFadeGesture(
                    this.animationShoot, 1f,
                    this.maskShoot,
                    0.05f, 0.2f
                    );
            }

            if (this.prefabMuzzleFlash)
            {
                GameObject muzzleInstance = PoolManager.Instance.Pick(this.prefabMuzzleFlash);
                muzzleInstance.transform.SetPositionAndRotation(
                    shooter.muzzle.GetPosition(),
                    shooter.muzzle.GetRotation()
                    );
            }

            if (this.delay < 0.01f)
            {
                this.ShootSelection(shooter, deviation, shotType);
            }
            else
            {
                CoroutinesManager.Instance.StartCoroutine(
                    this.ShootSelectionDelayed(shooter, deviation, shotType)
                    );
            }
        }
        public void OnRecevieShot(CharacterShooter shooter, CharacterShooter.ShotType type)
        {
            if (((int)type & (int)this.filterByShotType) == 0)
            {
                return;
            }
            bool tagFilter = (
                string.IsNullOrEmpty(this.filterByTag) ||
                this.filterByTag == "Untagged" ||
                shooter.CompareTag(this.filterByTag)
                );

            if (!tagFilter)
            {
                return;
            }

            if (this.filterByWeapon && shooter.currentWeapon != this.filterByWeapon)
            {
                return;
            }
            if (this.filterByAmmo && shooter.currentAmmo != this.filterByAmmo)
            {
                return;
            }

            this.storeShooter.Set(shooter.gameObject, gameObject);
            this.ExecuteTrigger(shooter.gameObject);
        }
Exemple #3
0
        private void ShootTrajectoryCast(CharacterShooter shooter, float deviation, CharacterShooter.ShotType shotType)
        {
            TrajectoryRenderer.TrajectoryResult result = this.GetTrajectoryResult(shooter);

            if (result.hit.collider)
            {
                Vector3 pointA = result.hit.point;
                Vector3 pointB = result.hit.point;

                if (result.points.Length > 2)
                {
                    pointA = result.points[result.count - 2];
                    pointB = result.points[result.count - 1];
                }

                Vector3    direction = (pointA - pointB).normalized;
                Quaternion rotation  = Quaternion.FromToRotation(Vector3.forward, direction);
                GameObject other     = result.hit.collider.gameObject;


                this.ExecuteShootActions(
                    other,
                    result.hit.point,
                    rotation
                    );

                IgniterOnReceiveShot[] igniters = other.GetComponentsInChildren <IgniterOnReceiveShot>();
                foreach (IgniterOnReceiveShot igniter in igniters)
                {
                    if (igniter)
                    {
                        igniter.OnRecevieShot(shooter, shotType);
                    }
                }

                if (!Mathf.Approximately(this.pushForce, 0f) && result.hit.rigidbody && direction != Vector3.zero)
                {
                    result.hit.rigidbody.AddForceAtPosition(
                        -direction * this.pushForce,
                        result.hit.point,
                        ForceMode.Impulse
                        );
                }

                if (this.prefabImpactEffect)
                {
                    GameObject impact = PoolManager.Instance.Pick(this.prefabImpactEffect);
                    impact.transform.SetPositionAndRotation(result.hit.point, rotation);
                }

                this.shootTrail.position1 = shooter.muzzle.GetPosition();
                this.shootTrail.position2 = result.hit.point;
            }

            if (this.shootTrail.useShootingTrail)
            {
                ShootingTrailRenderer.Create(this.shootTrail, result);
            }
        }
Exemple #4
0
        private IEnumerator ShootSelectionDelayed(CharacterShooter shooter, float deviation,
                                                  CharacterShooter.ShotType shotType)
        {
            WaitForSeconds wait = new WaitForSeconds(this.delay);

            yield return(wait);

            if (shooter)
            {
                this.ShootSelection(shooter, deviation, shotType);
            }
        }
Exemple #5
0
        public bool CanShoot(CharacterShooter shooter, CharacterShooter.ShotType shotType, bool subtract)
        {
            if (!this.ShootingRequirements(shooter))
            {
                return(false);
            }
            if (((int)this.chargeType & (int)shotType) == 0)
            {
                return(false);
            }
            if (!this.ClipRequirements(shooter, subtract))
            {
                return(false);
            }

            return(true);
        }
Exemple #6
0
        public bool Shoot(CharacterShooter shooter, float deviation, CharacterShooter.ShotType shotType)
        {
            if (!this.CanShoot(shooter, shotType, true))
            {
                return(false);
            }
            this.ShootGeneric(shooter, deviation, shotType);

            shooter.eventShoot.Invoke(shooter.currentWeapon, this);

            if (shooter.GetAmmoInClip(this.ammoID) <= 0)
            {
                this.RemoveAmmoPrefab(shooter);
            }

            return(true);
        }
Exemple #7
0
        private void ShootSelection(CharacterShooter shooter, float deviation,
                                    CharacterShooter.ShotType shotType)
        {
            switch (this.shootType)
            {
            case ShootType.Projectile: this.ShootProjectile(shooter, deviation, shotType); break;

            case ShootType.Raycast: this.ShootGenericCast(shooter, deviation, shotType, CastType.RaycastOne); break;

            case ShootType.RaycastAll: this.ShootGenericCast(shooter, deviation, shotType, CastType.RaycastAll); break;

            case ShootType.TrajectoryCast: this.ShootTrajectoryCast(shooter, deviation, shotType); break;

            case ShootType.SphereCast: this.ShootGenericCast(shooter, deviation, shotType, CastType.SphereCastOne); break;

            case ShootType.SphereCastAll: this.ShootGenericCast(shooter, deviation, shotType, CastType.SphereCastAll); break;
            }
        }
Exemple #8
0
        private void ShootGenericCast(CharacterShooter shooter, float deviation,
                                      CharacterShooter.ShotType shotType, CastType castType)
        {
            ShootData shootData = this.GetShootData(shooter);

            Vector3 shootPositionRaycast = shootData.originRaycast;
            Vector3 shootPositionWeapon  = shootData.originWeapon;
            Vector3 shootPositionTarget  = shootData.destination;

            shootPositionTarget += this.CalculateError(
                shooter.gameObject,
                shootPositionRaycast,
                shootPositionTarget,
                deviation
                );

            Vector3 direction = (shootPositionTarget - shootPositionRaycast).normalized;

            int hitCounter = 0;

            switch ((((int)castType) & 2) >> 1)
            {
            case 0:     // Raycast
                hitCounter = Physics.RaycastNonAlloc(
                    shootPositionRaycast, direction, this.bufferCastHits,
                    this.distance, this.layerMask, this.triggersMask
                    );
                break;

            case 1:     // SphereCast
                hitCounter = Physics.SphereCastNonAlloc(
                    shootPositionRaycast, this.radius, direction, this.bufferCastHits,
                    this.distance, this.layerMask, this.triggersMask
                    );
                break;
            }

            int maxCount = Mathf.Min(hitCounter, this.bufferCastHits.Length);

            Array.Sort(this.bufferCastHits, 0, maxCount, SHOT_COMPARE);

            for (int i = 0; hitCounter > 0 && i < maxCount; ++i)
            {
                Vector3 point = this.bufferCastHits[i].point;
                if (this.bufferCastHits[i].distance <= 0.01f && point == Vector3.zero)
                {
                    // special case in which sphere sweep overlaps the initial value:
                    point = shootPositionWeapon + (direction.normalized * 0.1f);
                }

                if (this.bufferCastHits[i].collider.gameObject.Equals(shooter.gameObject))
                {
                    continue;
                }

                Vector3    rotationDirection = (shootPositionWeapon - point).normalized;
                Quaternion rotation          = Quaternion.FromToRotation(Vector3.forward, rotationDirection);

                GameObject other = this.bufferCastHits[i].collider.gameObject;
                this.ExecuteShootActions(other, point, rotation);

                IgniterOnReceiveShot[] igniters = other.GetComponentsInChildren <IgniterOnReceiveShot>();
                foreach (IgniterOnReceiveShot igniter in igniters)
                {
                    if (igniter)
                    {
                        igniter.OnRecevieShot(shooter, shotType);
                    }
                }

                if (!Mathf.Approximately(this.pushForce, 0f) && this.bufferCastHits[i].rigidbody)
                {
                    this.bufferCastHits[i].rigidbody.AddForceAtPosition(
                        -rotationDirection * this.pushForce,
                        point,
                        ForceMode.Impulse
                        );
                }

                shootPositionTarget = point;

                if (this.prefabImpactEffect)
                {
                    GameObject impact = PoolManager.Instance.Pick(this.prefabImpactEffect);
                    impact.transform.SetPositionAndRotation(shootPositionTarget, rotation);
                }

                if ((((int)castType) & 1) == 0)
                {
                    break;
                }
            }

            if (this.shootTrail.useShootingTrail)
            {
                this.shootTrail.position1 = shootPositionWeapon;
                this.shootTrail.position2 = shootPositionTarget;
                ShootingTrailRenderer.Create(this.shootTrail);
            }
        }
Exemple #9
0
        private void ShootProjectile(CharacterShooter shooter, float deviation,
                                     CharacterShooter.ShotType shotType)
        {
            ShootData  shootData = this.GetShootData(shooter);
            GameObject bullet    = null;
            float      velocity  = 1f;

            if (this.aimingMode == AimType.Crosshair || this.aimingMode == AimType.None)
            {
                Vector3 shootPositionA = shootData.originWeapon;
                Vector3 shootPositionB = shootData.destination;

                shootPositionB += this.CalculateError(
                    shooter.gameObject,
                    shootPositionA,
                    shootPositionB,
                    deviation
                    );

                velocity = this.projectileVelocity;

                bullet = PoolManager.Instance.Pick(this.prefabProjectile);
                bullet.transform.SetPositionAndRotation(
                    shootPositionA,
                    Quaternion.LookRotation(shootPositionB - shootPositionA)
                    );
            }
            else if (this.aimingMode == AimType.Trajectory)
            {
                Vector3 shootPositionA = shootData.originWeapon;

                velocity = Mathf.Lerp(
                    this.trajectory.minVelocity,
                    this.trajectory.maxVelocity,
                    shooter.GetCharge()
                    );

                Vector3 shootDirection = shooter.muzzle.GetDirection();

                bullet = PoolManager.Instance.Pick(this.prefabProjectile);
                bullet.transform.SetPositionAndRotation(
                    shootPositionA,
                    Quaternion.LookRotation(shootDirection)
                    );
            }

            if (bullet)
            {
                Rigidbody bulletRB = bullet.GetComponent <Rigidbody>();
                if (bulletRB)
                {
                    Vector3 direction = bullet.transform.TransformDirection(Vector3.forward);
                    bulletRB.velocity        = Vector3.zero;
                    bulletRB.angularVelocity = Vector3.zero;

                    bulletRB.AddForce(direction * velocity, ForceMode.VelocityChange);
                }

                this.ExecuteShootActions(
                    shooter.gameObject,
                    shooter.muzzle.GetPosition(),
                    shooter.muzzle.GetRotation()
                    );
            }
        }