Пример #1
0
        /// <summary>
        ///     Creates a new <see cref="NetworkedWeapon" />. Designed to send over the network
        /// </summary>
        /// <param name="weapon"></param>
        /// <param name="setMaxBullets"></param>
        public NetworkedWeapon(TCWeapon weapon, bool setMaxBullets = true)
        {
            associatedTCWeapon = weapon;
            Weapon             = weapon.weapon;

            if (setMaxBullets)
            {
                Reload();
            }
        }
Пример #2
0
        /// <summary>
        ///     Updates ammo text
        /// </summary>
        /// <param name="netWeapon"></param>
        public void UpdateAmmoUI(NetworkedWeapon netWeapon)
        {
            if (clientUI == null || clientUI.WeaponManager == null)
            {
                return;
            }

            TCWeapon weapon = netWeapon.GetTCWeapon();

            ammoText.text    = netWeapon.CurrentBulletAmount.ToString();
            maxAmmoText.text = weapon.maxBullets.ToString();
            reloadTextGameObject.SetActive(netWeapon.IsReloading);
        }
Пример #3
0
        private void RpcDoWeaponShootEffects(WeaponShootEffectsTargets hitTargets)
        {
            TCWeapon       weapon         = weaponManager.GetActiveWeapon().GetTCWeapon();
            WeaponGraphics weaponGraphics = weaponManager.GetActiveWeaponGraphics();

            for (int i = 0; i < hitTargets.Targets.Length; i++)
            {
                //Do bullet tracer
                BulletTracer tracer =
                    Instantiate(weapon.bulletTracerEffect, weaponGraphics.bulletTracerPosition.position,
                                weaponGraphics.bulletTracerPosition.rotation).GetComponent <BulletTracer>();
                tracer.Play(hitTargets.Targets[i]);

                //Do bullet holes
                Instantiate(weapon.bulletHitEffectPrefab, hitTargets.Targets[i],
                            Quaternion.LookRotation(hitTargets.TargetNormals[i]));
                Instantiate(weapon.bulletHolePrefab, hitTargets.Targets[i],
                            Quaternion.FromToRotation(Vector3.back, hitTargets.TargetNormals[i]));
            }
        }
Пример #4
0
        internal void ShootWeapon(bool buttonDown)
        {
            if (ClientUI.IsPauseMenuOpen)
            {
                return;
            }

            //Cache our current weapon
            NetworkedWeapon networkedWeapon = weaponManager.GetActiveWeapon();

            //Looks like the weapon isn't setup yet
            if (networkedWeapon == null)
            {
                return;
            }

            if (networkedWeapon.IsReloading)
            {
                return;
            }

            TCWeapon weapon = networkedWeapon.GetTCWeapon();

            if (weapon == null)
            {
                return;
            }

            if (buttonDown && weapon.fireMode == TCWeapon.WeaponFireMode.Semi)
            {
                ClientCallServerShoot();
            }
            if (buttonDown && weapon.fireMode == TCWeapon.WeaponFireMode.Auto)
            {
                InvokeRepeating(nameof(ClientCallServerShoot), 0f, 1f / weapon.fireRate);
            }
            if (!buttonDown && weapon.fireMode == TCWeapon.WeaponFireMode.Auto)
            {
                CancelInvoke(nameof(ClientCallServerShoot));
            }
        }
Пример #5
0
        private void WeaponRayCast()
        {
            //Next, get what weapon the player was using
            TCWeapon tcWeapon = weaponManager.GetActiveWeapon().GetTCWeapon();

            if (tcWeapon == null)
            {
                return;
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            //Get the direction the player was facing
            Transform playerFacingDirection = localPlayerCamera.transform;

            //Create a list here, so we know later where the bullets landed
            List <Vector3> targets       = new List <Vector3>();
            List <Vector3> targetsNormal = new List <Vector3>();

            for (int i = 0; i < tcWeapon.bulletsPerShot; i++)
            {
                //Calculate random spread
                Vector3 direction = playerFacingDirection.forward;
                direction += playerFacingDirection.TransformDirection(new Vector3(
                                                                          Random.Range(-tcWeapon.spreadFactor, tcWeapon.spreadFactor),
                                                                          Random.Range(-tcWeapon.spreadFactor, tcWeapon.spreadFactor),
                                                                          Random.Range(-tcWeapon.spreadFactor, tcWeapon.spreadFactor)));

                //Now do our raycast
                // ReSharper disable once Unity.PreferNonAllocApi
                RaycastHit[] hits = RaycastHelper.RaycastAllSorted(playerFacingDirection.position, direction,
                                                                   tcWeapon.range, raycastLayerMask);
                foreach (RaycastHit hit in hits)
                {
                    //Don't count if we hit the shooting player
                    if (hit.collider.name == transform.name)
                    {
                        continue;
                    }

                    //Do impact effect on all clients
                    targets.Add(hit.point);
                    targetsNormal.Add(hit.normal);

                    //So if we hit a player then do damage
                    PlayerManager hitPlayer = hit.collider.GetComponent <PlayerManager>();
                    if (hitPlayer == null)
                    {
                        break;
                    }

                    hitPlayer.TakeDamage(tcWeapon.damage, transform.name);
                    break;
                }
            }

            //Send where the bullets hit in one big message
            RpcDoWeaponShootEffects(new WeaponShootEffectsTargets
            {
                Targets       = targets.ToArray(),
                TargetNormals = targetsNormal.ToArray()
            });

            stopwatch.Stop();
            Logger.Debug("Took {@Milliseconds}ms to fire {@Player}'s {@Weapon}", stopwatch.Elapsed.TotalMilliseconds,
                         transform.name, tcWeapon.weapon);
        }