Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5.  
  6. namespace UltimateSurvival.GUISystem
  7. {
  8. public class ItemUnload : GUIBehaviour
  9. {
  10.  
  11. //Get the main ItemInspector
  12. // chama a função loadammo usando o item no slot atual
  13. private ItemInspector itemInspector;
  14.  
  15. //Audio for Reload
  16. // Áudio para recarregar
  17. [SerializeField]
  18. [Tooltip("Sounds that will play when trying to unload")]
  19. private SoundPlayer m_UnloadAudio;
  20.  
  21. private void Awake()
  22. {
  23. itemInspector = transform.GetComponentInParent<ItemInspector>();
  24. }
  25.  
  26. // empty the weapon
  27. // esvazie a arma
  28. public void emptyAmmo(SavableItem weaponItem, int ammoStack)
  29. {
  30. if (weaponItem.HasProperty ("Ammo") && weaponItem.HasProperty ("Ammo Type") && weaponItem.HasProperty ("Ammo Capacity"))
  31. {
  32. //put ammo from the gun in the maggazine
  33. // coloca munição da arma no maggazine
  34. var m_AmmoAmount = weaponItem.GetPropertyValue ("Ammo");
  35. var ammo = m_AmmoAmount.Int;
  36. ammo.Current = ammoStack;
  37. m_AmmoAmount.SetValue (ItemProperty.Type.Int, ammo);
  38. }
  39. }
  40.  
  41. //Set capacity of itemInspector
  42. // Definir capacidade do itemInspector
  43. public void setCapacity(SavableItem ammoMagItem, int ammoCapacity){
  44. //set the magazine capacity
  45. // define a capacidade do magazine
  46. if (ammoMagItem.HasProperty ("Ammo Capacity")){
  47. var m_AmmoCapacity = ammoMagItem.GetPropertyValue ("Ammo Capacity");
  48. var AmmoCapacity = m_AmmoCapacity.Int;
  49. AmmoCapacity.Current = ammoCapacity;
  50. m_AmmoCapacity.SetValue (ItemProperty.Type.Int, AmmoCapacity);
  51. }
  52. }
  53.  
  54. //Create ammo magazine
  55. // Criar revista de munição
  56. public bool unloadMag(string magName, string ammoType, int ammoStack, int ammoCapacity, out SavableItem savableItem)
  57. {
  58. savableItem = null;
  59. SavableItem ammoMagItem;
  60. ItemData ItemData;
  61.  
  62. //find the item by its namespace
  63. // localiza o item pelo seu namespace
  64. InventoryController.Instance.Database.FindItemByName (magName, out ItemData);
  65.  
  66. //only 1 magazine in a stack
  67. // apenas uma magazine em uma pilha
  68. ammoMagItem = new SavableItem (ItemData, 1);
  69. ammoMagItem.CurrentInStack = 1;
  70.  
  71. if (ammoMagItem.HasProperty ("Ammo") && ammoMagItem.HasProperty ("Ammo Type") && ammoMagItem.HasProperty ("Ammo Capacity")) {
  72. //put ammo from the gun un the magazine
  73. // coloca munição da arma na magazine
  74. emptyAmmo (ammoMagItem, ammoStack);
  75.  
  76. //set the ammo type
  77. // define o tipo de munição
  78. var m_AmmoType = ammoMagItem.GetPropertyValue ("Ammo Type");
  79. m_AmmoType.SetValue (ItemProperty.Type.String, ammoType);
  80.  
  81. //set the magazine capacity
  82. // define a capacidade do magazine
  83. setCapacity(ammoMagItem, ammoCapacity);
  84.  
  85. //output the magazine
  86. // produz a magazine
  87. return savableItem = ammoMagItem;
  88. } else {
  89. return false;
  90. }
  91. }
  92.  
  93. // UNLOAD ITEM >>
  94. // DESCARREGAR ITEM >>
  95. public void On_UnloadClicked()
  96. {
  97. bool wasUnloaded = false;
  98. var item = itemInspector.InspectedSlot.CurrentItem;
  99. SavableItem ammoMagItem;
  100.  
  101. //just make sure that item has the properties we will use the IF statement
  102. // apenas certifique-se de que o item tenha as propriedades, usaremos a instrução IF
  103. if (item.HasProperty("Ammo Type") && item.HasProperty("Ammo") && item.HasProperty("Ammo Capacity"))
  104. {
  105. //"Ammo Magazine" is the generic magazine item
  106. // "magazine Munição" é o item genérico da magazine
  107. string magName = "Ammo Magazine";
  108. string ammoType = item.GetPropertyValue ("Ammo Type").String;
  109.  
  110. //how much ammo is in the item
  111. // quanta munição há no item
  112. int ammoStack = item.GetPropertyValue ("Ammo").Int.Current;
  113.  
  114. //get the current capacity of the item
  115. // obtém a capacidade atual do item
  116. int ammoCapacity = item.GetPropertyValue ("Ammo Capacity").Int.Current;
  117.  
  118. //check if the item requires an ammo mag, which mean it is likely a weapon
  119. // verifica se o item requer uma munição, o que significa que provavelmente é uma arma
  120. if (item.HasProperty ("Requires Ammo Mag")) {
  121.  
  122. if (item.HasProperty ("Has Ammo Mag") && item.GetPropertyValue ("Has Ammo Mag").Bool) {
  123. //flag that the magazine has been ejected from the weapon
  124. // sinaliza que a revista foi ejetada da arma
  125. var hasAmmo = item.GetPropertyValue ("Has Ammo Mag");
  126. hasAmmo.SetValue (ItemProperty.Type.Bool, false);
  127.  
  128.  
  129. //empty ane ammo from the weapon
  130. // esvazie munição ane da arma
  131. emptyAmmo (item, 0);
  132.  
  133. //can use generic ammo magazine or specific
  134. // pode usar uma revista de munição genérica ou específica
  135. unloadMag (magName, ammoType, ammoStack, ammoCapacity, out ammoMagItem);
  136. itemInspector.m_InventoryContainer.TryAddItem (ammoMagItem);
  137.  
  138. //set that the item was unloaded
  139. // define que o item foi descarregado
  140. wasUnloaded = true;
  141. }
  142. }
  143. //if item doesnt require an ammo mag
  144. // se o item não exigir uma munição
  145. else if (ammoStack > 0 && wasUnloaded == false) //only upload ammo mag if it has ammo // só carrega munição de munição se ela tiver munição
  146. {
  147. //unload ammo that match what was in the magazine
  148. // descarrega munição que corresponde ao que estava na revista
  149. itemInspector.m_InventoryContainer.TryAddItem (ammoType, ammoStack);
  150. //empty ane ammo from the weapon
  151. // esvazie munição ane da arma
  152. emptyAmmo (item, 0);
  153.  
  154. //check if it is an ammo mag itself
  155. // verifica se é uma própria munição
  156. if (item.HasProperty ("Is Ammo Mag")) {
  157. //if the item is amm the remove the original item in the slot
  158. // se o item for amm, remova o item original no slot
  159. itemInspector.InspectedSlot.ItemHolder.SetItem (null);
  160.  
  161. //can use generic ammo magazine or especific
  162. // pode usar uma revista de munição genérica ou específica
  163. unloadMag (magName, ammoType, 0, ammoCapacity, out ammoMagItem);
  164. itemInspector.m_InventoryContainer.TryAddItem (ammoMagItem);
  165. }
  166. //set that the item was unloaded
  167. // define que o item foi descarregado
  168. wasUnloaded = true;
  169. }
  170. }
  171.  
  172. //get the name of the object, if it has a displayname the use it
  173. // obtém o nome do objeto, se ele tiver um nome de exibição, use-o
  174. string itemName;
  175. if (item.ItemData.DisplayName.Length > 0 && item.ItemData.DisplayName != null) {
  176. itemName = item.ItemData.DisplayName;
  177. } else {
  178. //else use its item name // use o nome do item
  179. itemName = item.ItemData.Name;
  180. }
  181.  
  182. //only show message that ammo was unloaded if the bool is true
  183. // mostra apenas a mensagem de que a munição foi descarregada se o bool for verdadeiro
  184. if (wasUnloaded) {
  185. MessageDisplayer.Instance.PushMessage (string.Format ("<color=yellow{0}</color> Has been unloaded", itemName));
  186. m_UnloadAudio.Play2D ();
  187. } else {
  188. MessageDisplayer.Instance.PushMessage (string.Format ("<color=yellow{0}</color> Has nothing to unload", itemName));
  189. }
  190. }
  191.  
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement