CassataGames

Untitled

Feb 13th, 2022 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. using Photon.Pun;
  5.  
  6. using Sirenix.OdinInspector;
  7.  
  8. using UnityEngine;
  9. using UnityEngine.Events;
  10. using UnityEngine.Rendering;
  11.  
  12. public class CanFireBullets : ArenaObject
  13. {
  14.     [FoldoutGroup("Additional Properties"), PropertyOrder(4)]
  15.     [Tooltip("Prefab used as the bullet."), AssetsOnly, AssetSelector(Paths = "Assets/Resources")]
  16.     public GameObject _bulletPrefab;
  17.     [FoldoutGroup("Additional Properties"), PropertyOrder(4)]
  18.     [Required("Spawning transform required to spawn bullet!", InfoMessageType.Info), ChildGameObjectsOnly]
  19.     public Transform spawningTransform;
  20.     [FoldoutGroup("Additional Properties"), PropertyOrder(4)]
  21.     public float flareIntensity;
  22.  
  23.     [BoxGroup("Clip")]
  24.     public GunClip clip;
  25.     internal IEnumerator _startReloading;
  26.  
  27.     [BoxGroup("Rotation/Aiming")]
  28.     public GunRotation gunRotation;
  29.  
  30.     public BulletProperties bulletProperties;
  31.  
  32.     [HideInInspector]
  33.     public ShootFlare _shootFlare;
  34.  
  35.     [FoldoutGroup("UnityEvents"), PropertyOrder(-1)]
  36.     public UnityEvent onBulletFire;
  37.  
  38.     private bool _syncLock;
  39.  
  40.     #region Start/Update
  41.     void Start()
  42.     {
  43.         bulletProperties.launchParameters.spawnTransform = spawningTransform;
  44.         _shootFlare.barrelFlare = GetComponentInChildren<LensFlareComponentSRP>();
  45.     }
  46.  
  47.     void Update()
  48.     {
  49.         _shootFlare.UpdateFlare();
  50.     }
  51.  
  52.     #endregion    
  53.     [Button("Shoot Gun"), FoldoutGroup("Function Calls"), DisableInEditorMode, PropertyOrder(0), Tooltip("Title kind of gives it away, don'it?")]
  54.     public void FireOnce()
  55.     {
  56.         if (_syncLock || clip._ammoCurrent <= 0) return;
  57.         _syncLock = true;
  58.         photonView.RPC(nameof(Network_SingleShot), RpcTarget.All);
  59.     }
  60.  
  61.     // todo StartShooting / StopShooting
  62.  
  63.     [PunRPC]
  64.     private void Network_SingleShot()
  65.     {
  66.         _syncLock = false;
  67.  
  68.         var launchParameters = bulletProperties.launchParameters;
  69.         var damageUnit = bulletProperties.damageUnit;
  70.         SpawnBullet(launchParameters, damageUnit);
  71.         _shootFlare.ShowFlare(flareIntensity);
  72.  
  73.         clip.ReduceClip(1);
  74.         _startReloading = clip.IsReloading();
  75.         StartCoroutine(_startReloading);
  76.  
  77.         onBulletFire.Invoke();
  78.     }
  79.  
  80.     private void SpawnBullet(BulletProperties.LaunchParameters launch, BulletProperties.DamageUnit damage)
  81.     {
  82.         var newBullet = Instantiate(_bulletPrefab, spawningTransform.position, spawningTransform.transform.rotation);
  83.         newBullet.GetComponent<Rigidbody>().velocity = launch.speed * launch.spawnTransform.transform.forward;
  84.         newBullet.GetComponent<Bullet>().damageUnit = damage;
  85.     }
  86. }
Add Comment
Please, Sign In to add comment