PrimitiveWeapon.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using Valve.VR;
  6. public class PrimitiveWeapon : PhysicalObject
  7. {
  8. [Header("PrimitiveWeapon")]
  9. public Trigger trigger;//trigger handler ( script )
  10. public SteamVR_Skeleton_Poser triggerPoser;//shooting poser
  11. public Transform magazineAttachPoint,bulletInsidePoint,reloadBulletSpawn; //mag attach point, ammo inside of weapon position, sleeve extraction
  12. [Header("Recoil")]
  13. public Transform recoil;//recoil calculation object
  14. public float recoilAngle, recoilAngleReturn, recoilMaxAngle,recoilDistance,recoilDistanceReturn,recoilMaxDistance;
  15. public float recoilCurrentAngle;
  16. [Space]
  17. public bool detachableMag,armed,typeRevolver;//detach mags, gun ready to shoo, revolver/shotgun
  18. public string ammoType; //ammo type
  19. public Magazine attachMagazine; //attached mag
  20. public Bullet bulletInside; //ammo inside
  21. public Vector3 outBulletSpeed; // casing/ammo extraction speed
  22. public ManualReload manualReload; // reload handler ( script )
  23. // [HideInInspector]
  24. public Collider[] myCollidersToIgnore; //to ignore mag colliders
  25. [Header("Sounds Events")]
  26. public UnityEvent ShootEvent;
  27. public UnityEvent ShootEmptyEvent;
  28. public UnityEvent MagazineLoad,MagazineUnload;
  29. void Start()
  30. {
  31. Initialize();
  32. trigger = GetComponentInChildren<Trigger>();
  33. manualReload = GetComponentInChildren<ManualReload>();
  34. if (!detachableMag)
  35. {
  36. attachMagazine = GetComponentInChildren<Magazine>();
  37. }
  38. }
  39. new public void GrabStart(CustomHand hand){
  40. GrabStartCustom (hand);
  41. }
  42. new public void GrabUpdate(CustomHand hand){
  43. GrabUpdateCustom (hand);
  44. if (GetMyGrabPoser(hand)==triggerPoser)
  45. trigger.customUpdate (hand);
  46. if (recoil) {
  47. MyRigidbody.velocity += transform.TransformDirection (recoil.localPosition/Time.fixedDeltaTime);
  48. MyRigidbody.angularVelocity += PhysicalObject.GetAngularVelocities (transform.rotation, recoil.rotation, hand.GetBlendPose());
  49. }
  50. RecoilReturn ();
  51. }
  52. new public void GrabEnd(CustomHand hand){
  53. recoilCurrentAngle = 0;
  54. recoil.localPosition = Vector3.zero;
  55. GrabEndCustom (hand);
  56. }
  57. public void LoadBullet(){
  58. if (attachMagazine&& attachMagazine.ammo>0){
  59. bulletInside = attachMagazine.GetBullet ();
  60. bulletInside.transform.parent = bulletInsidePoint;
  61. bulletInside.transform.localPosition = Vector3.zero;
  62. bulletInside.transform.localRotation = Quaternion.identity;
  63. armed = true;
  64. }
  65. }
  66. public void RevolverArmed(){
  67. armed = true;
  68. attachMagazine.canLoad = false;
  69. }
  70. public void RevolverNoArmed(){
  71. armed = false;
  72. attachMagazine.canLoad = true;
  73. }
  74. public void UnloadBullet(){
  75. if (bulletInside) {
  76. bulletInside.transform.parent = null;
  77. bulletInside.transform.position = reloadBulletSpawn.position;
  78. bulletInside.transform.rotation = reloadBulletSpawn.rotation;
  79. bulletInside.OutMagazine ();
  80. bulletInside.MyRigidbody.AddRelativeForce (outBulletSpeed, ForceMode.VelocityChange);
  81. bulletInside=null;
  82. armed = false;
  83. }
  84. }
  85. public void Recoil(){
  86. recoil.localPosition -= Vector3.forward * recoilDistance;
  87. recoilCurrentAngle -= recoilAngle;
  88. }
  89. void RecoilReturn(){
  90. if (recoil) {
  91. recoilCurrentAngle = Mathf.Clamp (recoilCurrentAngle + recoilAngleReturn*Time.deltaTime, -recoilMaxAngle, 0);
  92. recoil.localPosition = new Vector3 (0, 0, Mathf.Clamp (recoil.localPosition.z + recoilDistanceReturn * Time.deltaTime, -recoilMaxDistance, 0));
  93. recoil.localEulerAngles = new Vector3 (-recoilCurrentAngle, 0, 0);
  94. }
  95. }
  96. public bool Shoot(){
  97. bool IsShoot = false;
  98. if (detachableMag) {
  99. if (bulletInside && bulletInside.armed) {
  100. Recoil ();
  101. bulletInside.ChangeModel ();
  102. IsShoot = true;
  103. }
  104. }else{
  105. if (typeRevolver) {
  106. if (manualReload.typeReload == ManualReload.TypeReload.Revolver) {
  107. if (attachMagazine.ShootFromMagazineRevolver ()) {
  108. Recoil ();
  109. IsShoot = true;
  110. }
  111. }
  112. if (manualReload.typeReload == ManualReload.TypeReload.Cracking) {
  113. if (attachMagazine.ShootFromMagazine ()) {
  114. Recoil ();
  115. IsShoot = true;
  116. }
  117. }
  118. } else {
  119. if (bulletInside && bulletInside.armed) {
  120. Recoil ();
  121. bulletInside.ChangeModel ();
  122. IsShoot = true;
  123. }
  124. }
  125. }
  126. if (IsShoot) {
  127. ShootEvent.Invoke ();
  128. } else {
  129. ShootEmptyEvent.Invoke ();
  130. }
  131. return IsShoot;
  132. }
  133. public void UnloadMagazine(){
  134. attachMagazine.UnloadMagazine (outBulletSpeed);
  135. MagazineUnload.Invoke ();
  136. }
  137. void OnTriggerEnter(Collider c){
  138. if (detachableMag&&!attachMagazine) {
  139. Magazine tempMagazine = c.GetComponentInParent<Magazine> ();
  140. if (tempMagazine&&tempMagazine.ammoType==ammoType) {
  141. //игнор колайтеров магазина
  142. myCollidersToIgnore = GetComponentInParent<PrimitiveWeapon> ().gameObject.GetComponentsInChildren<Collider> ();
  143. for (int j = 0; j < myCollidersToIgnore.Length; j++) {
  144. for (int k = 0; k < tempMagazine.MagazineColliders.Length; k++) {
  145. Physics.IgnoreCollision(myCollidersToIgnore[j],tempMagazine.MagazineColliders[k]);
  146. }
  147. }
  148. PhysicalObject tempPhysicalObject = tempMagazine.GetComponent<PhysicalObject>();
  149. if (tempPhysicalObject)
  150. {
  151. tempPhysicalObject.DettachHands();
  152. tempPhysicalObject.MyRigidbody.isKinematic = true;
  153. }
  154. tempMagazine.transform.parent = magazineAttachPoint;
  155. tempMagazine.transform.localPosition = Vector3.zero;
  156. tempMagazine.transform.localRotation = Quaternion.identity;
  157. attachMagazine = tempMagazine;
  158. tempMagazine.primitiveWeapon = this;
  159. tempMagazine.canLoad = false;
  160. MagazineLoad.Invoke ();
  161. return;
  162. }
  163. }
  164. }
  165. }