Magazine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class Magazine : MonoBehaviour
  6. {
  7. public bool Revolver = false; //revolver, can shout from built-in mags, same as shotgun
  8. public bool canLoad = true; //ammo allowed to be inserted
  9. public string ammoType; // ammo type
  10. public int capacity,ammo; // capacity and current ammo amount
  11. public List<Bullet> stickingAmmo; // sticking ammo
  12. public Transform[] ContainAmmo; // loaded ammo posistion
  13. public PrimitiveWeapon primitiveWeapon; // weapon which is attached to
  14. public Collider[] MagazineColliders; //IgnoreCollider
  15. PrimitiveWeapon primitiveWeaponRevolver; // revolver, which is attached to
  16. public float ang,id; //drum angle, id of current ammo
  17. [Header("Sounds Events")]
  18. public UnityEvent addBullet;
  19. void Start()
  20. {
  21. MagazineColliders = GetComponentsInChildren<Collider> ();
  22. if (Revolver)
  23. {
  24. primitiveWeaponRevolver = GetComponentInParent<PrimitiveWeapon>();
  25. stickingAmmo.AddRange(new Bullet[capacity]);
  26. enabled = true;
  27. }
  28. else
  29. {
  30. enabled = false;
  31. }
  32. }
  33. void Update(){
  34. ang = ((id<0?capacity+id:id) * 360 / capacity)%360;
  35. }
  36. public int getBulletIdRevolver(float tempAngle){
  37. float TempId = capacity/360f *(tempAngle<0?360f+tempAngle:tempAngle);
  38. return Mathf.RoundToInt (TempId)%capacity;
  39. }
  40. public void GrabStart(CustomHand hand){
  41. if (primitiveWeapon) {
  42. primitiveWeapon.attachMagazine = null;
  43. transform.parent = null;
  44. canLoad = true;
  45. for (int i = 0; i < primitiveWeapon.myCollidersToIgnore.Length; i++) {
  46. for (int j = 0; j < MagazineColliders.Length; j++) {
  47. Physics.IgnoreCollision(primitiveWeapon.myCollidersToIgnore[i],MagazineColliders[j],false);
  48. }
  49. }
  50. primitiveWeapon = null;
  51. }
  52. }
  53. void AddBullet(Bullet bullet){
  54. if (!canLoad)
  55. return;
  56. bullet.DettachBullet ();
  57. stickingAmmo.Add (bullet);
  58. ammo = stickingAmmo.Count;
  59. SortingBulletInMagazine();
  60. bullet.EnterMagazine ();
  61. addBullet.Invoke ();
  62. }
  63. void SortingBulletInMagazine() {
  64. for (int i = 0; i < stickingAmmo.Count; i++)
  65. {
  66. stickingAmmo[i].transform.parent = ContainAmmo[ammo-i-1];
  67. stickingAmmo[i].transform.localPosition = Vector3.zero;
  68. stickingAmmo[i].transform.localRotation = Quaternion.identity;
  69. }
  70. }
  71. void AddBulletClose(Bullet bullet){
  72. if (!Revolver&&!canLoad)
  73. return;
  74. float tempCloseDistance=float.MaxValue;
  75. int closeId=0;
  76. for (int i = 0; i < ContainAmmo.Length; i++) {
  77. if (stickingAmmo[i]==null&&Vector3.Distance(bullet.transform.position,ContainAmmo[i].position)<tempCloseDistance){
  78. tempCloseDistance = Vector3.Distance (bullet.transform.position, ContainAmmo [i].position);
  79. closeId = i;
  80. }
  81. }
  82. bullet.DettachBullet ();
  83. stickingAmmo [closeId] = bullet;
  84. ammo++;
  85. stickingAmmo [closeId].transform.parent = ContainAmmo [closeId];
  86. stickingAmmo [closeId].transform.localPosition = Vector3.zero;
  87. stickingAmmo [closeId].transform.localRotation = Quaternion.identity;
  88. bullet.EnterMagazine ();
  89. }
  90. public bool ShootFromMagazineRevolver(){
  91. if (!Revolver)
  92. return false;
  93. // int tempId = (primitiveWeaponRevolver.manualReload.revolverBulletID+1)%capacity;
  94. int tempId = primitiveWeaponRevolver.manualReload.revolverBulletID;
  95. if (stickingAmmo [tempId] != null&&stickingAmmo[tempId].armed) {
  96. stickingAmmo [tempId].ChangeModel ();
  97. return true;
  98. }
  99. return false;
  100. }
  101. public bool ShootFromMagazine(){//по очередди
  102. if (!Revolver)
  103. return false;
  104. for (int i = 0; i < stickingAmmo.Count; i++) {
  105. if (stickingAmmo [i] != null&&stickingAmmo[i].armed) {
  106. stickingAmmo [i].ChangeModel ();
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. public void UnloadMagazine(Vector3 outBulletSpeed){
  113. if (!Revolver) {
  114. return;
  115. }
  116. for (int i = 0; i < stickingAmmo.Count; i++) {
  117. if (stickingAmmo [i]) {
  118. stickingAmmo [i].transform.parent = null;
  119. stickingAmmo [i].OutMagazine ();
  120. stickingAmmo [i].MyRigidbody.AddRelativeForce (outBulletSpeed, ForceMode.VelocityChange);
  121. stickingAmmo [i] = null;
  122. }
  123. }
  124. ammo = 0;
  125. }
  126. public Bullet GetBullet(){
  127. Bullet tempReturn=stickingAmmo[ammo-1];
  128. stickingAmmo.RemoveAt(ammo-1);
  129. ammo = stickingAmmo.Count;
  130. SortingBulletInMagazine();
  131. return tempReturn;
  132. }
  133. void OnTriggerEnter(Collider c){
  134. if (Revolver) {
  135. if (c.attachedRigidbody && c.attachedRigidbody.GetComponent<Bullet> ()&&c.attachedRigidbody.GetComponent<Bullet> ().ammoType == ammoType) {
  136. if (ammo < capacity) {
  137. AddBulletClose (c.attachedRigidbody.GetComponent<Bullet> ());
  138. }
  139. }
  140. } else {
  141. if (c.attachedRigidbody && c.attachedRigidbody.GetComponent<Bullet> () && c.attachedRigidbody.GetComponent<Bullet> ().ammoType == ammoType) {
  142. if (ammo < capacity) {
  143. AddBullet (c.attachedRigidbody.GetComponent<Bullet> ());
  144. }
  145. }
  146. }
  147. }
  148. }