Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.PostProcessing;
  6. using SciFiArsenal;
  7. using System;
  8.  
  9. public class Inventory : MonoBehaviour {
  10. public string[] NOME_ID = new string[0];
  11. public Text tipi;
  12. public Text Ammo;
  13. hudview hudscript;
  14. public GameObject ContenitoreIndicatori;
  15. List<int> fixedIndex = new List<int>();
  16. //Dictionary<int, int> fixedIndex = new Dictionary<int,int>();
  17. public Transform FPSCamera;
  18. public GameObject HUD;
  19. public RectTransform Selected;
  20. public float startingOffset = -90;
  21. protected vp_PlayerEventHandler Player;
  22. protected vp_Weapon m_Weapon = null;
  23. public vp_PlayerInventory vpi;
  24. public float wheelThicknessOffset = 10;
  25. public float rotation = 0.0f;
  26. public float slotAmplitude;
  27. public AudioSource cantopen;
  28.  
  29. public int typesCount;
  30.  
  31. public bool opened = false;
  32. public int selectedIndex = 0;
  33. public float scrollSensitivity = 2f;
  34.  
  35. public Dictionary<int, List<vp_ItemIdentifier>> typeToIndex = new Dictionary<int, List<vp_ItemIdentifier>>();
  36. public Dictionary<int, int> idToOrder = new Dictionary<int, int> ();
  37.  
  38. public Transform slotPrefab; //Il prefab dell'icona
  39. public Image Wheel; //Prefab della ruota
  40. public RectTransform slotContainer;
  41. private Transform cWeapon;
  42. private Transform selectedW;
  43.  
  44. public List<Slot> SlotsList; //Lista di icone, serve per la selezione e l'eliminazione
  45.  
  46. public int getIdByItemType(vp_ItemType itemType)
  47. {
  48. for(int i=0; i<NOME_ID.Length; i += 2)
  49. {
  50. if (NOME_ID[i].Contains(itemType.name))
  51. return Convert.ToInt32(NOME_ID[i + 1]);
  52. }
  53. return -1;
  54. }
  55.  
  56. void Update() {
  57. if (Input.GetKeyDown (KeyCode.Tab) && !AleksiVariable.VP_SimpleCrosshair.Hide) {
  58. if (selectedW != null) {
  59. if (Time.time > selectedW.GetComponent<vp_WeaponShooter> ().GetNextAlloweFireTime ())
  60. OpenInv ();
  61. else
  62. cantopen.Play();
  63. } else
  64. OpenInv ();
  65. }
  66.  
  67. if (Input.GetKeyUp (KeyCode.Tab) && opened) {
  68. CloseInv ();
  69. }
  70. if (opened) {
  71. if (Input.GetAxis ("Mouse ScrollWheel") > 0) { //Se scrolla su
  72. SlotsList [selectedIndex].GoUp ();
  73. }
  74.  
  75. if (Input.GetAxis ("Mouse ScrollWheel") < 0) { //Se scrolla giu
  76. SlotsList [selectedIndex].GoDown ();
  77. }
  78. }
  79.  
  80. if (opened)
  81. UpdateRotation (); //Se l'inventario è aperto aggiorna la rotazione
  82. }
  83.  
  84. void UpdateRotation() {
  85. Debug.Log(fixedIndex.Count);
  86. if (fixedIndex.Count == 0) {
  87. Ammo.text = "0/0";
  88. tipi.text = "0/0";
  89. return;
  90. }
  91. rotation += (-Input.GetAxis ("Mouse X")*scrollSensitivity)+(Input.GetAxis ("Mouse Y")*scrollSensitivity); //Aggiorna la rotazione
  92.  
  93. rotation %= (360/7)*typesCount; //Resetta la rotazione se supera 360
  94.  
  95. if (rotation < 0)
  96. rotation = ((360/7)*typesCount) - rotation; //Resetta la rotazione se minore di 0
  97.  
  98. Debug.Log("Rotation " + rotation);
  99.  
  100. selectedIndex = Mathf.CeilToInt((rotation/(360/7))); //Calcola l'elemento selezionato
  101.  
  102. if (selectedIndex >= fixedIndex.Count)
  103. selectedIndex = 0;
  104.  
  105. Debug.Log("Rotation: " + rotation + ", selectedIndex: " + selectedIndex + ", new: " + fixedIndex[selectedIndex]);
  106. selectedIndex = fixedIndex[selectedIndex];
  107.  
  108. highlightIcon (selectedIndex); //Lo "Illumina"
  109. }
  110. public void highlightIcon(int i) {
  111. int index = 0;
  112. foreach(Transform s in Selected)
  113. {
  114. if(index==selectedIndex)
  115. s.GetComponent<Image>().enabled = true;
  116. else
  117. s.GetComponent<Image>().enabled = false;
  118. index++;
  119. }
  120. for(int ci = 0; ci < SlotsList.Count; ci++)
  121. {
  122. if (ci == i)
  123. SlotsList [ci].GetComponent<RectTransform> ().localScale = new Vector2 (2.73f,2.73f);
  124. else
  125. SlotsList [ci].GetComponent<RectTransform> ().localScale = new Vector2 (2.33f,2.33f);
  126. }
  127. vp_Weapon arma_corrente = null;
  128. foreach(Transform a in FPSCamera){
  129. if(a.name.EndsWith(SlotsList [i].selected.GetItemType().DisplayName +"Transform")){
  130. arma_corrente = a.GetChild(0).GetComponent<vp_Weapon>();
  131. }
  132. }
  133. tipi.text = (SlotsList [i].itemIndex + 1) + "/" + (SlotsList [i].list.Count);
  134. if (arma_corrente.tag == "Plasma" || arma_corrente.tag == "Beam" || arma_corrente.tag == "Flamethrower")
  135. Ammo.text = (int)(hudscript.getPercentMp ()*100) + "%";
  136. else
  137. Ammo.text = vpi.GetAmmoInWeapon(arma_corrente) +"/" + vpi.GetExtraAmmoForWeapon(arma_corrente);
  138. }
  139.  
  140. public void OpenInv() {
  141. AleksiVariable.VP_SimpleCrosshair.Hide = true;
  142. foreach (Transform A in FPSCamera) {
  143. if (A.gameObject.activeSelf && A.gameObject.name != "WeaponCamera" && A.name.EndsWith("Transform")) {
  144. cWeapon = A.GetChild (0);
  145. break;
  146. }
  147. }
  148. if (cWeapon != null) {
  149. if (cWeapon.tag == "Plasma") {
  150. cWeapon.GetComponent<SciFiFireProjectile> ().enabled = false;
  151. } else if (cWeapon.tag == "Beam") {
  152. cWeapon.GetComponent<SciFiBeamScript> ().DestroyAll ();
  153. cWeapon.GetComponent<SciFiBeamScript> ().enabled = false;
  154.  
  155. } else if (cWeapon.tag == "Flamethrower") {
  156. cWeapon.GetComponent<ScifiFlamethrower> ().DestroyAll ();
  157. cWeapon.GetComponent<ScifiFlamethrower> ().enabled = false;
  158. }
  159. }
  160. HUD.SetActive(false);
  161. ContenitoreIndicatori.SetActive (true);
  162.  
  163. AleksiVariable.postProcessingBehaviour.profile = AleksiVariable.Sfocatura;
  164. GetComponent<vp_FPInput> ().enabled = false;
  165. int order = 0;
  166. Dictionary<int, int> indexes = new Dictionary<int, int>();
  167. for (int ak = 0; ak < 7; ak++) {
  168. indexes.Add (ak, ak);
  169. typeToIndex.Add (ak, new List<vp_ItemIdentifier> ());
  170. }
  171. foreach(vp_ItemIdentifier it in vpi.WeaponIdentifiers.Values)
  172. {
  173. //Debug.Log("Aggiunto "+it.ID+" alla posizione "+indexes[Mathf.FloorToInt (it.ID / 10)] + "; Esiste: " + typeToIndex.ContainsKey(indexes[Mathf.FloorToInt (it.ID / 10)]));
  174. if (vpi.HaveItem (it.Type, it.ID)) {
  175. typeToIndex [indexes [Mathf.FloorToInt (it.ID / 10)]].Add (it);
  176. idToOrder.Add (it.ID, order);
  177. order++;
  178. }
  179. }
  180. //typesCount = 7; //Invece di settare lo prende dallo script di UFPS (Il totale di tipi di armi)
  181. opened = true; //Setta l'inventario su "aperto"
  182. slotAmplitude = 360/7; //Calcola la lunghezza dell'arco di ogni slot
  183. //rotation = fixedIndex.IndexOf(selectedIndex)*(360/7);
  184. //fixedIndex.Clear();
  185. //rotation = (slotAmplitude*selectedIndex)+(slotAmplitude/2);
  186. placeSlots (); //Crea le icone nel posto giusto
  187. Wheel.enabled = true; //Mostra la ruota
  188. if(!AleksiVariable.Multiplayer)
  189. Time.timeScale = 0.02f;
  190. }
  191.  
  192. public void CloseInv() {
  193. Debug.Log("Indice alla chiusura: " + selectedIndex);
  194. AleksiVariable.VP_SimpleCrosshair.Hide = false;
  195. opened = false; //Chiude l'inventario
  196.  
  197. Wheel.enabled = false; //nasconde la ruota
  198.  
  199. Time.timeScale = 1;
  200. if (fixedIndex.Count > 0){
  201. //if (fixedIndex.Count > selectedIndex) {
  202. if (cWeapon != null) {
  203. if (cWeapon.tag == "Plasma") {
  204. cWeapon.GetComponent<SciFiFireProjectile> ().enabled = true;
  205. } else if (cWeapon.tag == "Beam") {
  206. cWeapon.GetComponent<SciFiBeamScript> ().enabled = true;
  207. } else if (cWeapon.tag == "Flamethrower") {
  208. cWeapon.GetComponent<ScifiFlamethrower> ().enabled = true;
  209. }
  210. }
  211. vp_ItemType tmpItemType = SlotsList[selectedIndex].selected.GetItemType();
  212.  
  213. string weaponName = tmpItemType.DisplayName;
  214. Debug.Log("Name weapon: " + weaponName);
  215. Player.SetWeapon.TryStart (SetWeaponByName (weaponName));
  216. if (tmpItemType.UnlimitedAmmo)
  217. {
  218. hudview.HUD.currentAmmo.text = "∞";
  219. hudview.HUD.currentClip.text = "";
  220. hudview.HUD.unlimited = true;
  221. }
  222. else
  223. hudview.HUD.unlimited = false;
  224. AleksiVariable.BPlasmaManager.RefreshEffect (weaponName);
  225. //AleksiVariable.BPlasmaManager.TryReloadEffect (SlotsList [selectedIndex].selected.GetItemType ().DisplayName);
  226. //Player.SetWeapon.TryStart(idToOrder[SlotsList[selectedIndex].selected.ID]+1);
  227. //Player.SetWeapon.TryStart(numeroArma+1);
  228. selectedW = SlotsList [selectedIndex].selected.transform;
  229. }
  230. GetComponent<vp_FPInput> ().enabled = true;
  231. ContenitoreIndicatori.SetActive (false);
  232. HUD.SetActive(true);
  233. AleksiVariable.postProcessingBehaviour.profile = null;
  234. vp_LocalPlayer.InputManager.MouseCursorBlocksMouseLook = false;
  235. //AleksiVariable.postProcessingBehaviour.enabled = false;
  236. deleteChildren (); //Cancella il contenuto (Verrà reinstanziato dopo)
  237. }
  238.  
  239. public int SetWeaponByName(string nome_arma){
  240. int identificatore = 1;
  241. foreach (vp_ItemIdentifier itid in vpi.WeaponIdentifiers.Values) {
  242. if (itid.name.EndsWith(nome_arma))
  243. return identificatore;
  244. identificatore++;
  245. }
  246. return 0;
  247. }
  248.  
  249. void Start(){
  250. Wheel.enabled = false;
  251. for (int i_NOME_ID = 0; i_NOME_ID < NOME_ID.Length; i_NOME_ID += 2) {
  252. GameObject.Find (NOME_ID [i_NOME_ID]).GetComponent<vp_ItemIdentifier> ().ID = int.Parse (NOME_ID [i_NOME_ID + 1]);
  253. }
  254. }
  255.  
  256. void Awake(){
  257. hudscript = GetComponent<hudview>();
  258. m_Weapon = transform.GetComponent<vp_Weapon>();
  259. Player = GetComponent<vp_FPPlayerEventHandler> ();
  260. }
  261.  
  262. /*void SetArma(){
  263. foreach(Transform t in FPSCamera)
  264. {
  265. if(t.name.EndsWith(SlotsList[selectedIndex].selected.GetItemType().Icon.name+"Transform"))
  266. {
  267. Debug.Log (t.name[0]);
  268. }
  269. }
  270. }*/
  271.  
  272. public void placeSlots() {
  273. //Calcoli per il posizionamento
  274.  
  275. float halfAmplitude = slotAmplitude / 2;
  276. float wheelRadius = Wheel.rectTransform.rect.width/2;
  277. float slotRadius = slotPrefab.GetComponent<RectTransform> ().rect.width / 2;
  278.  
  279. //float startingOffset = -20; //Offset di partenza
  280.  
  281. SlotsList.Clear ();
  282. int index2 = 0;
  283. typesCount = 0;
  284. for(int i=0; i < 7; i++)
  285. {
  286. //Debug.Log("Settando lo slot "+i);
  287. float x = (wheelRadius-slotRadius-wheelThicknessOffset)*Mathf.Cos (Mathf.Deg2Rad*((i*slotAmplitude)+halfAmplitude+startingOffset));
  288. float y = (wheelRadius-slotRadius-wheelThicknessOffset)*Mathf.Sin (Mathf.Deg2Rad*((i*slotAmplitude)+halfAmplitude+startingOffset));
  289.  
  290. Slot slot = GameObject.Instantiate (slotPrefab, Vector3.zero, Quaternion.identity).GetComponent<Slot>();
  291.  
  292. slot.GetComponent<RectTransform>().SetParent (slotContainer, false);
  293. slot.GetComponent<RectTransform>().localPosition = new Vector2 (x,y);
  294.  
  295. SlotsList.Add (slot);
  296. if(typeToIndex.ContainsKey(i) && typeToIndex[i].Count > 0){
  297. fixedIndex.Add (index2);
  298. typesCount++;
  299. for (int item = 0; item < typeToIndex[i].Count; item++) {
  300. //Debug.Log("Aggiungendo arma ("+typeToIndex[i][item].name+") allo slot "+i + " con id " + typeToIndex[i][item].ID);
  301. slot.list.Add(typeToIndex[i][item]);
  302. if(slot.list.Count == 1)
  303. slot.GetComponent<RawImage>().texture = slot.list[0].GetItemType().Icon;
  304. slot.GetComponent<RawImage> ().SetNativeSize ();
  305. if(typeToIndex[i][item] == vpi.CurrentWeaponIdentifier){ //Se è l'ggetto selezionato
  306. rotation = (fixedIndex.Count-1) * (360 / 7) + (180 / 7);
  307. slot.switchItem(slot.list.Count-1); //Imposta l'arma selezionata come prima in lista
  308. }
  309. }
  310. }
  311. index2++;
  312. }
  313. /*foreach (KeyValuePair<int, int> p in fixedIndex) {
  314. Debug.Log (p.Key + "," + p.Value);
  315. }*/
  316. }
  317.  
  318. public void deleteChildren() {
  319. foreach (Slot i in SlotsList) {
  320. GameObject.Destroy (i.gameObject);
  321. }
  322. foreach (Transform s in Selected) {
  323. s.GetComponent<Image> ().enabled = false;
  324. }
  325. idToOrder.Clear ();
  326. typeToIndex.Clear ();
  327. SlotsList.Clear ();
  328. fixedIndex.Clear ();
  329. }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement