BowControlLegacy.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.Characters.FirstPerson;
  6. public class BowControlLegacy : WeaponsBaseLegacy
  7. {
  8. #region Fields
  9. [SerializeField] bool useSmooth = true;
  10. [Range (1f, 10f)]
  11. [SerializeField] float aimSmooth;
  12. [Range(1, 179)]
  13. [SerializeField] float aimFov = 35;
  14. float defaultFov;
  15. float currentFov;
  16. [HideInInspector] public float baseSpread = 1.0f;
  17. public float maxSpread = 4.0f;
  18. private float defaultSpread;
  19. private float defaultMaxSpread;
  20. public GameObject arrow = null;
  21. public Transform mountPoint = null;
  22. public GameObject attachedArrow = null;
  23. bool isFiring = false;
  24. // basic stats
  25. public int range = 300;
  26. public float damage = 20.0f;
  27. public float maxPenetration = 3.0f;
  28. public float fireRate = 0.5f;
  29. public int impactForce = 50;
  30. public float arrowSpeed = 60.0f;
  31. bool isLoaded = false;
  32. #endregion
  33. protected override void Awake ()
  34. {
  35. base.Awake ();
  36. defaultSpread = baseSpread;
  37. defaultMaxSpread = maxSpread;
  38. }
  39. protected override void Start ()
  40. {
  41. base.Start ();
  42. defaultFov = Camera.main.fieldOfView;
  43. canAim = true;
  44. }
  45. protected override void InputUpdate()
  46. {
  47. if (Input.GetMouseButton (1) && CanAim)
  48. {
  49. isAimed = true;
  50. if (!isFiring)
  51. EmptySlot (false);
  52. if (Input.GetMouseButtonDown (0) && CanFire && isLoaded)
  53. {
  54. isFiring = true;
  55. StartCoroutine (Fire ());
  56. }
  57. }
  58. else
  59. {
  60. isAimed = false;
  61. isLoaded = false;
  62. }
  63. }
  64. protected override void SyncState()
  65. {
  66. if (isAimed && !isLoaded && !isFiring && !disableWeapon)
  67. {
  68. if (playerSync)
  69. {
  70. if (playerSync.WpState != WeaponState.PrepareAiming)
  71. {
  72. playerSync.WpState = WeaponState.PrepareAiming;
  73. StartCoroutine (LoadArrow ());
  74. }
  75. }
  76. }
  77. else if (isAimed && isLoaded && !isFiring && !disableWeapon)
  78. {
  79. if (playerSync)
  80. {
  81. if (playerSync.WpState != WeaponState.AimedIdle)
  82. {
  83. playerSync.WpState = WeaponState.AimedIdle;
  84. PlayWeaponAnimation (weaponAnimations.Weapon_ADS_Idle.name, WrapMode.Loop);
  85. }
  86. }
  87. }
  88. else if (isAimed && isFiring && !disableWeapon)
  89. {
  90. if (playerSync)
  91. {
  92. if (playerSync.WpState != WeaponState.AimedFiring) {
  93. playerSync.WpState = WeaponState.AimedFiring;
  94. }
  95. }
  96. }
  97. else if (disableWeapon)
  98. {
  99. if (playerSync)
  100. {
  101. playerSync.WpState = WeaponState.Lowering;
  102. }
  103. }
  104. else if (controller.IsRunning && !disableWeapon && !isFiring && !isAimed)
  105. {
  106. if (playerSync)
  107. {
  108. playerSync.WpState = WeaponState.Running;
  109. }
  110. }
  111. else if (controller.IsWalking && !disableWeapon && !isFiring && !isAimed)
  112. {
  113. if (playerSync)
  114. {
  115. playerSync.WpState = WeaponState.Walking;
  116. PlayWeaponAnimation (weaponAnimations.Weapon_Idle.name, WrapMode.Loop);
  117. }
  118. }
  119. else
  120. {
  121. if (playerSync)
  122. {
  123. if (playerSync.WpState != WeaponState.Idle)
  124. {
  125. playerSync.WpState = WeaponState.Idle;
  126. PlayWeaponAnimation (weaponAnimations.Weapon_Idle.name, WrapMode.Loop);
  127. }
  128. }
  129. }
  130. }
  131. protected override void Aim()
  132. {
  133. if (isAimed)
  134. {
  135. currentFov = aimFov;
  136. baseSpread = defaultSpread / 2f;
  137. maxSpread = defaultMaxSpread / 2f;
  138. }
  139. else
  140. {
  141. currentFov = defaultFov;
  142. baseSpread = defaultSpread;
  143. maxSpread = defaultMaxSpread;
  144. }
  145. Camera.main.fieldOfView = useSmooth ?
  146. Mathf.Lerp(Camera.main.fieldOfView, currentFov, Time.deltaTime * (aimSmooth * 3)) : //apply fog distance
  147. Mathf.Lerp(Camera.main.fieldOfView, currentFov, Time.deltaTime * aimSmooth);
  148. }
  149. IEnumerator Fire()
  150. {
  151. StartCoroutine (FireOneShot());
  152. EmptySlot (true);
  153. isLoaded = false;
  154. Source.clip = FireSound;
  155. Source.spread = Random.Range (1.0f, 1.5f);
  156. Source.pitch = Random.Range (1.0f, 1.05f);
  157. Source.Play();
  158. PlayWeaponAnimation (weaponAnimations.Weapon_ADS_Fire.name, WrapMode.Once);
  159. yield return StartCoroutine (WaitAnimationFinish (weaponAnimations.Weapon_ADS_Fire));
  160. isFiring = false;
  161. // yield return null;
  162. }
  163. IEnumerator FireOneShot()
  164. {
  165. Vector3 position = mountPoint.position;
  166. // set the gun's info into an array to send to the bullet
  167. MissileInfo info = new MissileInfo();
  168. info.damage = damage;
  169. info.impactForce = impactForce;
  170. info.maxPenetration = maxPenetration;
  171. info.maxspread = maxSpread;
  172. info.speed = arrowSpeed;
  173. info.position = this.transform.root.position;
  174. info.lifeTime = range;
  175. Quaternion q = Quaternion.Euler (new Vector3 (0, transform.eulerAngles.y - 90f, -transform.eulerAngles.x));
  176. GameObject newArrow = Instantiate (arrow, position, q) as GameObject;
  177. newArrow.GetComponent<Missile>().SetUp(info);
  178. // newArrow.transform.RotateAround (newArrow.transform.position, newArrow.transform.right, 90f);
  179. newArrow.transform.RotateAround (newArrow.transform.position, newArrow.transform.forward, 90f);
  180. Source.clip = FireSound;
  181. Source.spread = Random.Range (1.0f, 1.5f);
  182. Source.Play();
  183. yield return null;
  184. }
  185. void EmptySlot (bool value)
  186. {
  187. if (value)
  188. attachedArrow.SetActive (false);
  189. else
  190. attachedArrow.SetActive (true);
  191. }
  192. IEnumerator LoadArrow ()
  193. {
  194. PlayWeaponAnimation (weaponAnimations.Weapon_IdleToADS.name);
  195. yield return StartCoroutine (WaitAnimationFinish (weaponAnimations.Weapon_IdleToADS));
  196. isLoaded = true;
  197. }
  198. public override IEnumerator ReloadNormal()
  199. {
  200. PlayWeaponAnimation (weaponAnimations.Weapon_Reload.name, WrapMode.Once);
  201. EmptySlot (false);
  202. yield return StartCoroutine (WaitAnimationFinish (weaponAnimations.Weapon_Reload));
  203. isReloading = false;
  204. canAim = true;
  205. canFire = true;
  206. }
  207. public override void DisableWeapon()
  208. {
  209. canAim = false;
  210. isReloading = false;
  211. attachedArrow.SetActive (false);
  212. base.DisableWeapon ();
  213. }
  214. public bool CanReload
  215. {
  216. get
  217. {
  218. if (!isReloading)
  219. {
  220. if (controller.IsWalking || controller.IsQuiet)
  221. return true;
  222. return false;
  223. }
  224. return false;
  225. }
  226. }
  227. } // class