WeaponsBaseLegacy.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.Characters.FirstPerson;
  6. [RequireComponent(typeof(UnityEngine.AudioSource))]
  7. public abstract class WeaponsBaseLegacy : MonoBehaviour
  8. {
  9. #region Fields
  10. public WeaponAnimations weaponAnimations;
  11. protected Animation[] myAnims;
  12. protected bool canFire = true;
  13. public AudioClip TakeSound;
  14. public AudioClip FireSound;
  15. [HideInInspector] public bool isAimed;
  16. protected bool canAim;
  17. private Quaternion defaultCamRot;
  18. protected bool isReloading = false;
  19. protected bool disableWeapon = false;
  20. protected AudioSource Source;
  21. [HideInInspector]
  22. public bool blockHit = false;
  23. #endregion
  24. protected virtual void Awake()
  25. {
  26. Source = GetComponent<AudioSource>();
  27. myAnims = GetComponentsInChildren <Animation> ();
  28. disableWeapon = false;
  29. }
  30. protected virtual void Start()
  31. {
  32. defaultCamRot = Camera.main.transform.localRotation;
  33. }
  34. protected virtual void OnEnable()
  35. {
  36. Source.clip = TakeSound;
  37. Source.Play();
  38. canFire = true;
  39. canAim = true;
  40. disableWeapon = false;
  41. playerSync.UpdateCurrentWeapon (GetComponent <WeaponsBaseLegacy> ());
  42. playerSync.WpState = WeaponState.Raising;
  43. }
  44. protected virtual void OnDisable ()
  45. {
  46. disableWeapon = false;
  47. }
  48. void Update()
  49. {
  50. InputUpdate();
  51. Aim();
  52. SyncState();
  53. }
  54. protected abstract void InputUpdate ();
  55. protected abstract void SyncState ();
  56. protected virtual void Aim () {}
  57. protected void PlayWeaponAnimation (string name, WrapMode wp = WrapMode.Default)
  58. {
  59. // myAnims.wrapMode = wp;
  60. //
  61. // if (wp == WrapMode.Loop)
  62. // myAnims.CrossFade (name);
  63. // else
  64. // myAnims.Play (name);
  65. foreach (Animation a in myAnims)
  66. {
  67. a.wrapMode = wp;
  68. if (a.wrapMode == WrapMode.Loop)
  69. a.CrossFade (name);
  70. else
  71. a.Play (name);
  72. }
  73. }
  74. protected IEnumerator WaitAnimationFinish (AnimationClip clip)
  75. {
  76. float reloadTime = clip.length;
  77. yield return new WaitForSeconds (reloadTime);
  78. }
  79. public virtual void DisableWeapon ()
  80. {
  81. canFire = false;
  82. disableWeapon = true;
  83. StopAllCoroutines();
  84. }
  85. public virtual IEnumerator ReloadNormal ()
  86. {
  87. yield return null;
  88. }
  89. protected FirstPersonController controller
  90. {
  91. get
  92. {
  93. return transform.root.GetComponent<FirstPersonController>();
  94. }
  95. }
  96. protected PlayerSyncLegacy playerSync
  97. {
  98. get
  99. {
  100. return PlayerSyncLegacy.Instance;
  101. }
  102. }
  103. protected PlayerManager playerManager
  104. {
  105. get
  106. {
  107. return PlayerManager.Instance;
  108. }
  109. }
  110. public bool CanFire
  111. {
  112. get
  113. {
  114. if (canFire && !isReloading && !controller.IsRunning)
  115. // if (canFire && !isReloading && !controller.IsRunning && isLoaded)
  116. return true;
  117. else
  118. return false;
  119. }
  120. }
  121. public bool CanAim
  122. {
  123. get
  124. {
  125. if (canAim && !controller.IsRunning )
  126. return true;
  127. else
  128. return false;
  129. }
  130. }
  131. public bool IsReloading
  132. {
  133. get
  134. {
  135. return isReloading;
  136. }
  137. }
  138. } // class