Untitled

mail@pastecode.io avatar
unknown
plain_text
3 months ago
11 kB
4
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Alteruna.Trinity;
using Alteruna;
using System.Security.Policy;

public class MultiplayerShooting : AttributesSync
{
    public Color[] bulletColors;
    public float bounceDuration = 10;
    public float pierceDuration = 10;
    // The damage inflicted by each bullet.
    public int damagePerShot = 20;
    public int numberOfBullets = 1;
    // The time between each shot.
    public float timeBetweenBullets = 0.15f;
    public float angleBetweenBullets = 10f;
    // The distance the gun can fire.
    public float range = 100f;
    // A layer mask so the raycast only hits things on the shootable layer.
    public LayerMask shootableMask;
    private MultiplayerHealth multiplayerHealth;
    // Reference to the UI's green health bar.

  //  private Image bounceImage;
    // Reference to the UI's red health bar.

   // private  Image pierceImage;
    public GameObject bullet;
    public Transform bulletSpawnAnchor;
    public Transform GunBarrelEnd;

    // private GameObject pierceTimerObj;

    //private  GameObject bounceTimerObj;
    private Alteruna.Avatar _avatar;
    private bool _isPossessed = false;
    private bool _isPossessor = false;
    private Spawner _spawner;
    [SerializeField] private int indexToSpawn = 4;
    // A timer to determine when to fire.
    float timer;
    // A ray from the gun end forwards.
    Ray shootRay;
    // A raycast hit to get information about what was hit.
    RaycastHit shootHit;
    // Reference to the particle system.
    ParticleSystem gunParticles;
    // Reference to the line renderer.
    LineRenderer gunLine;
    // Reference to the audio source.
    AudioSource gunAudio;
    // Reference to the light component.
    Light gunLight;
    public static MultiplayerShooting Instance;
    // The proportion of the timeBetweenBullets that the effects will display for.
    float effectsDisplayTime = 0.2f;
    float bounceTimer;
    float pierceTimer;
    bool bounce;
    bool piercing;
    Color bulletColor;
    //private Spawner _spawner;
   // [SerializeField] private int indexToSpawn = 4;
    private CanvasManager canvasManager;
    public float timeBetweenShots = 0.2f; // Adjust this value to control the time between shots.
    private bool isShooting = false;
    public float BounceTimer
    {
        get { return bounceTimer; }
        set { bounceTimer = value; }
    }

    public float PierceTimer
    {
        get { return pierceTimer; }
        set { pierceTimer = value; }
    }

    void Awake()
    {
        // Set up the references.
        gunParticles = GetComponent<ParticleSystem>();
        gunAudio = GetComponent<AudioSource>();
        gunLight = GetComponentInChildren<Light>();
        _avatar = GetComponent<Alteruna.Avatar>();
        bounceTimer = bounceDuration;
        pierceTimer = pierceDuration;
        canvasManager = CanvasManager.Instance;
        // bounceImage = CanvasManager.Instance.bounceImage;
        //pierceImage = CanvasManager.Instance.pierceImage;
        if (Instance == null)
        {
            Instance = this;
        }
        _spawner = GameObject.FindGameObjectWithTag("NetworkManager").GetComponent<Spawner>();
    }
    public override void Possessed(bool isMe, User user)
    {
        _isPossessed = true;

        // also known as is owner
        _isPossessor = isMe;

        // Inside CommunicationBridge, we can access the Multiplayer component through the Multiplayer property.
        // We can use it to get the avatar for a user.
        _avatar = Multiplayer.GetAvatar(user.Index);

        Debug.LogWarning("_avatar Is Possessed");
    }


    void Start()
    {
        // Set up the references.
        Image bounceImage = CanvasManager.Instance.bounceImage;
        Image pierceImage = CanvasManager.Instance.pierceImage;
        multiplayerHealth = GetComponent<MultiplayerHealth>();
       GameObject bounceTimerObj = CanvasManager.Instance.bounceTimerObj;
       GameObject pierceTimerObj = CanvasManager.Instance.pierceTimerObj;
        _avatar = GetComponent<Alteruna.Avatar>();
    }

    void Update()
    {
        // Disabling the timer labels for bounce and pierce
        CanvasManager.Instance.bounceTimerObj.SetActive(false);
        CanvasManager.Instance.pierceTimerObj.SetActive(false);

        if (bounceTimer < bounceDuration)
        {
            bounce = true;
        }
        else
        {
            bounce = false;
        }

        if (pierceTimer < pierceDuration)
        {
            piercing = true;
        }
        else
        {
            piercing = false;
        }

       // bulletColor = bulletColors[0];
        if (bulletColors != null && bulletColors.Length > 0)
        {
            bulletColor = bulletColors[0];
           
        }
        if (bounce)
        {
            // setting and enabling label
            CanvasManager.Instance.bounceTimerObj.SetActive(true);
           // bounceTimerObj.SetActive(true);
            Text bounceTime = CanvasManager.Instance.pierceTimerObj.GetComponent<Text>();
            float floatVal = bounceDuration - bounceTimer;
            int val = Mathf.CeilToInt(floatVal);
            bounceTime.text = val.ToString();

            bulletColor = bulletColors[1];
            CanvasManager.Instance.bounceImage.color = bulletColors[1];
        }
        CanvasManager.Instance.bounceImage.gameObject.SetActive(bounce);

        if (piercing)
        {
            // setting and enabling label
            CanvasManager.Instance.pierceTimerObj.SetActive(true);
            Text pierceTime = CanvasManager.Instance.pierceTimerObj.GetComponent<Text>();
            // pierceTimerObj.SetActive(true);
           // Text pierceTime = pierceTimerObj.GetComponent<Text>();
            float floatVal = pierceDuration - pierceTimer;
            int val = Mathf.CeilToInt(floatVal);
            pierceTime.text = val.ToString();

            bulletColor = bulletColors[2];
            CanvasManager.Instance.pierceImage.color = bulletColors[2];
        }
        CanvasManager.Instance.pierceImage.gameObject.SetActive(piercing);

        if (piercing & bounce)
        {
            bulletColor = bulletColors[3];
            CanvasManager.Instance.bounceImage.color = bulletColors[3];
            CanvasManager.Instance.pierceImage.color = bulletColors[3];
        }

        var main = gunParticles.main;
        main.startColor = bulletColor;
        // For some reason the color I had selected originally looked extremely
        // reddish after I switched to deferred rendering and linear mode so 
        // I'm hardcoding in a lighter, more yellow light color if you have
        // both the pierce and bounce powerup active.
        gunLight.color = (piercing & bounce) ? new Color(1, 140f / 255f, 30f / 255f, 1) : bulletColor;

        // Add the time since Update was last called to the timer.
        bounceTimer += Time.deltaTime;
        pierceTimer += Time.deltaTime;
        timer += Time.deltaTime;
        // If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for...
        if (timer >= timeBetweenBullets * effectsDisplayTime)
        {
            // ... disable the effects.
            DisableEffects();
        }
    }


    
    public void DisableEffects()
    {
        // Disable the line renderer and the light.
        gunLight.enabled = false;
    }

    public void StartShoot()
    {

         if (timer >= timeBetweenBullets && Time.timeScale != 0 && _avatar.IsMe)
        {
            Vector3 shootOrigin = GunBarrelEnd.transform.position;
            Vector3 shootDirection = GunBarrelEnd.transform.forward;
           // BroadcastRemoteMethod(nameof(Shoot), GunBarrelEnd.transform.position, GunBarrelEnd.transform.eulerAngles, bulletSpawnAnchor.transform.position);
            InvokeRemoteMethod(nameof(Shoot), UserId.AllInclusive, GunBarrelEnd.transform.position, GunBarrelEnd.transform.eulerAngles, bulletSpawnAnchor.transform.position);
        }
    }


    [SynchronizableMethod]
    public void Shoot(Vector3 origin, Vector3 direction, Vector3 bulletOrigin)
    {


        Quaternion q = Quaternion.Euler(direction);
        // Set the shootRay so that it starts at the end of the gun and points forward from the barrel.
        shootRay.origin = origin;
        shootRay.direction = q * Vector3.forward;
        // if (!_avatar.IsOwner)
        // return;

        // Reset the timer.

        timer = 0f;

        // Play the gun shot audioclip.
        gunAudio.pitch = Random.Range(1.2f, 1.3f);
        if (bounce)
        {
            gunAudio.pitch = Random.Range(1.1f, 1.2f);
        }
        if (piercing)
        {
            gunAudio.pitch = Random.Range(1.0f, 1.1f);
        }
        if (piercing & bounce)
        {
            gunAudio.pitch = Random.Range(0.9f, 1.0f);
        }
        gunAudio.Play();

        // Enable the light.
        gunLight.intensity = 2 + (0.25f * (numberOfBullets - 1));
        gunLight.enabled = true;

        // Stop the particles from playing if they were, then start the particles.
        gunParticles.Stop();
        var main = gunParticles.main;
        main.startSize = 1 + (0.1f * (numberOfBullets - 1));
        gunParticles.Play();
        // shootRay.origin = origin;
        // shootRay.direction = direction;
        // Set the shootRay so that it starts at the end ofres the gun and points forward from the barrel.
        // shootRay.origin = transform.position;
        // shootRay.direction = transform.forward;

        for (int i = 0; i < numberOfBullets; i++)
        {
            // Make sure our bullets spread out in an even pattern.
            float angle = i * angleBetweenBullets - ((angleBetweenBullets / 2) * (numberOfBullets - 1));
            Quaternion rot = q * Quaternion.AngleAxis(angle, Vector3.up);
            GameObject instantiatedBullet = Instantiate(bullet, bulletOrigin, rot) as GameObject;
            instantiatedBullet.GetComponent<BulletMultiplayer>().piercing = piercing;
            instantiatedBullet.GetComponent<BulletMultiplayer>().bounce = bounce;
            instantiatedBullet.GetComponent<BulletMultiplayer>().bulletColor = bulletColor;


        }

    }
}
Leave a Comment