Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.PostProcessing;
- using SciFiArsenal;
- using System;
- public class Inventory : MonoBehaviour {
- public string[] NOME_ID = new string[0];
- public Text tipi;
- public Text Ammo;
- hudview hudscript;
- public GameObject ContenitoreIndicatori;
- List<int> fixedIndex = new List<int>();
- //Dictionary<int, int> fixedIndex = new Dictionary<int,int>();
- public Transform FPSCamera;
- public GameObject HUD;
- public RectTransform Selected;
- public float startingOffset = -90;
- protected vp_PlayerEventHandler Player;
- protected vp_Weapon m_Weapon = null;
- public vp_PlayerInventory vpi;
- public float wheelThicknessOffset = 10;
- public float rotation = 0.0f;
- public float slotAmplitude;
- public AudioSource cantopen;
- public int typesCount;
- public bool opened = false;
- public int selectedIndex = 0;
- public float scrollSensitivity = 2f;
- public Dictionary<int, List<vp_ItemIdentifier>> typeToIndex = new Dictionary<int, List<vp_ItemIdentifier>>();
- public Dictionary<int, int> idToOrder = new Dictionary<int, int> ();
- public Transform slotPrefab; //Il prefab dell'icona
- public Image Wheel; //Prefab della ruota
- public RectTransform slotContainer;
- private Transform cWeapon;
- private Transform selectedW;
- public List<Slot> SlotsList; //Lista di icone, serve per la selezione e l'eliminazione
- public int getIdByItemType(vp_ItemType itemType)
- {
- for(int i=0; i<NOME_ID.Length; i += 2)
- {
- if (NOME_ID[i].Contains(itemType.name))
- return Convert.ToInt32(NOME_ID[i + 1]);
- }
- return -1;
- }
- void Update() {
- if (Input.GetKeyDown (KeyCode.Tab) && !AleksiVariable.VP_SimpleCrosshair.Hide) {
- if (selectedW != null) {
- if (Time.time > selectedW.GetComponent<vp_WeaponShooter> ().GetNextAlloweFireTime ())
- OpenInv ();
- else
- cantopen.Play();
- } else
- OpenInv ();
- }
- if (Input.GetKeyUp (KeyCode.Tab) && opened) {
- CloseInv ();
- }
- if (opened) {
- if (Input.GetAxis ("Mouse ScrollWheel") > 0) { //Se scrolla su
- SlotsList [selectedIndex].GoUp ();
- }
- if (Input.GetAxis ("Mouse ScrollWheel") < 0) { //Se scrolla giu
- SlotsList [selectedIndex].GoDown ();
- }
- }
- if (opened)
- UpdateRotation (); //Se l'inventario è aperto aggiorna la rotazione
- }
- void UpdateRotation() {
- Debug.Log(fixedIndex.Count);
- if (fixedIndex.Count == 0) {
- Ammo.text = "0/0";
- tipi.text = "0/0";
- return;
- }
- rotation += (-Input.GetAxis ("Mouse X")*scrollSensitivity)+(Input.GetAxis ("Mouse Y")*scrollSensitivity); //Aggiorna la rotazione
- rotation %= (360/7)*typesCount; //Resetta la rotazione se supera 360
- if (rotation < 0)
- rotation = ((360/7)*typesCount) - rotation; //Resetta la rotazione se minore di 0
- Debug.Log("Rotation " + rotation);
- selectedIndex = Mathf.CeilToInt((rotation/(360/7))); //Calcola l'elemento selezionato
- if (selectedIndex >= fixedIndex.Count)
- selectedIndex = 0;
- Debug.Log("Rotation: " + rotation + ", selectedIndex: " + selectedIndex + ", new: " + fixedIndex[selectedIndex]);
- selectedIndex = fixedIndex[selectedIndex];
- highlightIcon (selectedIndex); //Lo "Illumina"
- }
- public void highlightIcon(int i) {
- int index = 0;
- foreach(Transform s in Selected)
- {
- if(index==selectedIndex)
- s.GetComponent<Image>().enabled = true;
- else
- s.GetComponent<Image>().enabled = false;
- index++;
- }
- for(int ci = 0; ci < SlotsList.Count; ci++)
- {
- if (ci == i)
- SlotsList [ci].GetComponent<RectTransform> ().localScale = new Vector2 (2.73f,2.73f);
- else
- SlotsList [ci].GetComponent<RectTransform> ().localScale = new Vector2 (2.33f,2.33f);
- }
- vp_Weapon arma_corrente = null;
- foreach(Transform a in FPSCamera){
- if(a.name.EndsWith(SlotsList [i].selected.GetItemType().DisplayName +"Transform")){
- arma_corrente = a.GetChild(0).GetComponent<vp_Weapon>();
- }
- }
- tipi.text = (SlotsList [i].itemIndex + 1) + "/" + (SlotsList [i].list.Count);
- if (arma_corrente.tag == "Plasma" || arma_corrente.tag == "Beam" || arma_corrente.tag == "Flamethrower")
- Ammo.text = (int)(hudscript.getPercentMp ()*100) + "%";
- else
- Ammo.text = vpi.GetAmmoInWeapon(arma_corrente) +"/" + vpi.GetExtraAmmoForWeapon(arma_corrente);
- }
- public void OpenInv() {
- AleksiVariable.VP_SimpleCrosshair.Hide = true;
- foreach (Transform A in FPSCamera) {
- if (A.gameObject.activeSelf && A.gameObject.name != "WeaponCamera" && A.name.EndsWith("Transform")) {
- cWeapon = A.GetChild (0);
- break;
- }
- }
- if (cWeapon != null) {
- if (cWeapon.tag == "Plasma") {
- cWeapon.GetComponent<SciFiFireProjectile> ().enabled = false;
- } else if (cWeapon.tag == "Beam") {
- cWeapon.GetComponent<SciFiBeamScript> ().DestroyAll ();
- cWeapon.GetComponent<SciFiBeamScript> ().enabled = false;
- } else if (cWeapon.tag == "Flamethrower") {
- cWeapon.GetComponent<ScifiFlamethrower> ().DestroyAll ();
- cWeapon.GetComponent<ScifiFlamethrower> ().enabled = false;
- }
- }
- HUD.SetActive(false);
- ContenitoreIndicatori.SetActive (true);
- AleksiVariable.postProcessingBehaviour.profile = AleksiVariable.Sfocatura;
- GetComponent<vp_FPInput> ().enabled = false;
- int order = 0;
- Dictionary<int, int> indexes = new Dictionary<int, int>();
- for (int ak = 0; ak < 7; ak++) {
- indexes.Add (ak, ak);
- typeToIndex.Add (ak, new List<vp_ItemIdentifier> ());
- }
- foreach(vp_ItemIdentifier it in vpi.WeaponIdentifiers.Values)
- {
- //Debug.Log("Aggiunto "+it.ID+" alla posizione "+indexes[Mathf.FloorToInt (it.ID / 10)] + "; Esiste: " + typeToIndex.ContainsKey(indexes[Mathf.FloorToInt (it.ID / 10)]));
- if (vpi.HaveItem (it.Type, it.ID)) {
- typeToIndex [indexes [Mathf.FloorToInt (it.ID / 10)]].Add (it);
- idToOrder.Add (it.ID, order);
- order++;
- }
- }
- //typesCount = 7; //Invece di settare lo prende dallo script di UFPS (Il totale di tipi di armi)
- opened = true; //Setta l'inventario su "aperto"
- slotAmplitude = 360/7; //Calcola la lunghezza dell'arco di ogni slot
- //rotation = fixedIndex.IndexOf(selectedIndex)*(360/7);
- //fixedIndex.Clear();
- //rotation = (slotAmplitude*selectedIndex)+(slotAmplitude/2);
- placeSlots (); //Crea le icone nel posto giusto
- Wheel.enabled = true; //Mostra la ruota
- if(!AleksiVariable.Multiplayer)
- Time.timeScale = 0.02f;
- }
- public void CloseInv() {
- Debug.Log("Indice alla chiusura: " + selectedIndex);
- AleksiVariable.VP_SimpleCrosshair.Hide = false;
- opened = false; //Chiude l'inventario
- Wheel.enabled = false; //nasconde la ruota
- Time.timeScale = 1;
- if (fixedIndex.Count > 0){
- //if (fixedIndex.Count > selectedIndex) {
- if (cWeapon != null) {
- if (cWeapon.tag == "Plasma") {
- cWeapon.GetComponent<SciFiFireProjectile> ().enabled = true;
- } else if (cWeapon.tag == "Beam") {
- cWeapon.GetComponent<SciFiBeamScript> ().enabled = true;
- } else if (cWeapon.tag == "Flamethrower") {
- cWeapon.GetComponent<ScifiFlamethrower> ().enabled = true;
- }
- }
- vp_ItemType tmpItemType = SlotsList[selectedIndex].selected.GetItemType();
- string weaponName = tmpItemType.DisplayName;
- Debug.Log("Name weapon: " + weaponName);
- Player.SetWeapon.TryStart (SetWeaponByName (weaponName));
- if (tmpItemType.UnlimitedAmmo)
- {
- hudview.HUD.currentAmmo.text = "∞";
- hudview.HUD.currentClip.text = "";
- hudview.HUD.unlimited = true;
- }
- else
- hudview.HUD.unlimited = false;
- AleksiVariable.BPlasmaManager.RefreshEffect (weaponName);
- //AleksiVariable.BPlasmaManager.TryReloadEffect (SlotsList [selectedIndex].selected.GetItemType ().DisplayName);
- //Player.SetWeapon.TryStart(idToOrder[SlotsList[selectedIndex].selected.ID]+1);
- //Player.SetWeapon.TryStart(numeroArma+1);
- selectedW = SlotsList [selectedIndex].selected.transform;
- }
- GetComponent<vp_FPInput> ().enabled = true;
- ContenitoreIndicatori.SetActive (false);
- HUD.SetActive(true);
- AleksiVariable.postProcessingBehaviour.profile = null;
- vp_LocalPlayer.InputManager.MouseCursorBlocksMouseLook = false;
- //AleksiVariable.postProcessingBehaviour.enabled = false;
- deleteChildren (); //Cancella il contenuto (Verrà reinstanziato dopo)
- }
- public int SetWeaponByName(string nome_arma){
- int identificatore = 1;
- foreach (vp_ItemIdentifier itid in vpi.WeaponIdentifiers.Values) {
- if (itid.name.EndsWith(nome_arma))
- return identificatore;
- identificatore++;
- }
- return 0;
- }
- void Start(){
- Wheel.enabled = false;
- for (int i_NOME_ID = 0; i_NOME_ID < NOME_ID.Length; i_NOME_ID += 2) {
- GameObject.Find (NOME_ID [i_NOME_ID]).GetComponent<vp_ItemIdentifier> ().ID = int.Parse (NOME_ID [i_NOME_ID + 1]);
- }
- }
- void Awake(){
- hudscript = GetComponent<hudview>();
- m_Weapon = transform.GetComponent<vp_Weapon>();
- Player = GetComponent<vp_FPPlayerEventHandler> ();
- }
- /*void SetArma(){
- foreach(Transform t in FPSCamera)
- {
- if(t.name.EndsWith(SlotsList[selectedIndex].selected.GetItemType().Icon.name+"Transform"))
- {
- Debug.Log (t.name[0]);
- }
- }
- }*/
- public void placeSlots() {
- //Calcoli per il posizionamento
- float halfAmplitude = slotAmplitude / 2;
- float wheelRadius = Wheel.rectTransform.rect.width/2;
- float slotRadius = slotPrefab.GetComponent<RectTransform> ().rect.width / 2;
- //float startingOffset = -20; //Offset di partenza
- SlotsList.Clear ();
- int index2 = 0;
- typesCount = 0;
- for(int i=0; i < 7; i++)
- {
- //Debug.Log("Settando lo slot "+i);
- float x = (wheelRadius-slotRadius-wheelThicknessOffset)*Mathf.Cos (Mathf.Deg2Rad*((i*slotAmplitude)+halfAmplitude+startingOffset));
- float y = (wheelRadius-slotRadius-wheelThicknessOffset)*Mathf.Sin (Mathf.Deg2Rad*((i*slotAmplitude)+halfAmplitude+startingOffset));
- Slot slot = GameObject.Instantiate (slotPrefab, Vector3.zero, Quaternion.identity).GetComponent<Slot>();
- slot.GetComponent<RectTransform>().SetParent (slotContainer, false);
- slot.GetComponent<RectTransform>().localPosition = new Vector2 (x,y);
- SlotsList.Add (slot);
- if(typeToIndex.ContainsKey(i) && typeToIndex[i].Count > 0){
- fixedIndex.Add (index2);
- typesCount++;
- for (int item = 0; item < typeToIndex[i].Count; item++) {
- //Debug.Log("Aggiungendo arma ("+typeToIndex[i][item].name+") allo slot "+i + " con id " + typeToIndex[i][item].ID);
- slot.list.Add(typeToIndex[i][item]);
- if(slot.list.Count == 1)
- slot.GetComponent<RawImage>().texture = slot.list[0].GetItemType().Icon;
- slot.GetComponent<RawImage> ().SetNativeSize ();
- if(typeToIndex[i][item] == vpi.CurrentWeaponIdentifier){ //Se è l'ggetto selezionato
- rotation = (fixedIndex.Count-1) * (360 / 7) + (180 / 7);
- slot.switchItem(slot.list.Count-1); //Imposta l'arma selezionata come prima in lista
- }
- }
- }
- index2++;
- }
- /*foreach (KeyValuePair<int, int> p in fixedIndex) {
- Debug.Log (p.Key + "," + p.Value);
- }*/
- }
- public void deleteChildren() {
- foreach (Slot i in SlotsList) {
- GameObject.Destroy (i.gameObject);
- }
- foreach (Transform s in Selected) {
- s.GetComponent<Image> ().enabled = false;
- }
- idToOrder.Clear ();
- typeToIndex.Clear ();
- SlotsList.Clear ();
- fixedIndex.Clear ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement