Advertisement
Unbalance

Weapon Setup part

Jan 2nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. public void InitializeGun()
  2.     {
  3.         print("InitializeGun");
  4.         if (Gun == null) { Debug.Log("Oh, i dont have a Gun."); return; }
  5.         if (AudioSource == null) { _audioSource = this.GetComponent<AudioSource>(); }
  6.         if (MainCamera == null) { MainCamera = Camera.main; }
  7.         cachedSocketPosition = gunSocket.transform.localPosition;
  8.  
  9.         if (photonView.isMine)
  10.         {
  11.             Gun.Set(this.gameObject);
  12.             Gun.Initialize(null);                                        //call Initialize from WeaponBase/GunBase class
  13.         }
  14.         else
  15.         {
  16.  
  17.             WeaponType = Gun.WeaponType;
  18.             GunName = Gun.WeaponName;
  19.             Description = Gun.Description;
  20.             FireSound = Gun.UseSound;
  21.             _audioSource.clip = FireSound;
  22.             ReloadSound = Gun.ReloadSound;
  23.             HitEffect = Gun.HitEffect;
  24.  
  25.         }
  26.  
  27.         if (GunObject == null)
  28.         {
  29.             GunObject = PhotonNetwork.Instantiate(Gun.ResourcesPath, new Vector3(0, 0, 0), Quaternion.identity, 0);     //Creating the Gun and parent it to the Socket in localSpace
  30.             //photonView.ObservedComponents.Add(GunObject.transform);
  31.             GunObject.transform.SetParent(gunSocket.transform, false);
  32.             GunObject.transform.localPosition = Gun.PositionOffset;                 //use Position we made in the Scriptable WeaponBase
  33.         }
  34.  
  35.         if (Gun.MuzzleFlash == null)
  36.         {
  37.             Debug.Log(GunName + " has no muzzleFlash.");
  38.         }
  39.         else
  40.         {
  41.             //MuzzleFlash = Instantiate(Gun.MuzzleFlash, GunObject.transform, false);
  42.             var t = PhotonNetwork.Instantiate(Gun.MuzzleFlashPath, new Vector3(0, 0, 0), Quaternion.identity, 0);
  43.             t.transform.SetParent(GunObject.transform, false);
  44.             MuzzleFlash = t.GetComponent<ParticleSystem>();
  45.             MuzzleFlash.transform.localPosition = Gun.MuzzleFlashPosition;                                                  //using a offset to well place the MuzzleFlash
  46.             MuzzleFlash.transform.rotation = Quaternion.LookRotation(GunObject.transform.forward, GunObject.transform.up);  //TODO simplify
  47.         }
  48.         if (Gun.WeaponType == GunTypes.Thrower)                                                                             //If the GunType is Thrower we have to setup some more Things
  49.         {
  50.             AudioSource.clip = FireSound;
  51.             AudioSource.loop = true;                                                                                        //the Sound should always Play if the Button is down
  52.         }
  53.  
  54.         SetupObjectPool();
  55.  
  56.     }
  57.  
  58.     private void SetupObjectPool()
  59.     {
  60.         if (HitEffect == null && Gun.WeaponType != GunTypes.Thrower) { Debug.LogError("No HitEffect attached."); }
  61.         projectileHolder = new GameObject("ProjectilePool").transform;
  62.         hiteffectHolder = new GameObject("HiteffectPool").transform;
  63.  
  64.         if (WeaponType == GunTypes.HeavyGun)
  65.         {
  66.             ProjectileBehaviour pb = Gun.Projectile.GetComponent<ProjectileBehaviour>();
  67.             float lifeTime = pb.LifeTime;
  68.             float poolSize = ((1 / Gun.FireRate) * lifeTime) * Gun.ProjectileCount;
  69.             t_Pool.CreateNewPool(Gun.Projectile,Gun.ProjectilePath, (int)poolSize, projectileHolder);
  70.         }
  71.         if (WeaponType != GunTypes.Thrower && WeaponType != GunTypes.HeavyGun)
  72.         {
  73.             float poolSize = ((1 / Gun.FireRate) * 1);
  74.             t_Pool.CreateNewPool(Gun.HitEffect.gameObject,Gun.HitEffectPath, (int)poolSize, hiteffectHolder);
  75.         }
  76.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement