Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Linq;
  8. using TMPro;
  9. using UnityEditor;
  10. using UnityEditorInternal;
  11.  
  12.  
  13. public class ShopK : NetworkBehaviour {
  14.  
  15.     public GameObject BoxFab, ItemFab;
  16.     public Transform BoxHolder, WorldGO, ItemsHolder;
  17.  
  18.     public List<GameObject> Items;
  19.  
  20.     public ShopItems[] shopItems;
  21.  
  22.     public TMP_Text ToPayToday;
  23.  
  24.     public void Awake()
  25.     {
  26.         for (int i = 0; i < shopItems.Length; i++)
  27.         {
  28.             var index = i;
  29.             GameObject ab = (GameObject)Instantiate(ItemFab, ItemsHolder) as GameObject;
  30.  
  31.             ab.transform.name = shopItems[i].Name;
  32.  
  33.             ab.transform.GetChild(1).GetComponent<Image>().sprite = shopItems[i].Icon;
  34.             ab.transform.GetChild(2).GetComponent<TMP_Text>().text = shopItems[i].Name;
  35.             ab.transform.GetChild(3).GetComponent<TMP_Text>().text = shopItems[i].Description;
  36.             ab.transform.GetChild(6).GetComponent<TMP_InputField>().text = shopItems[i].HowMany.ToString();
  37.  
  38.             ab.transform.GetChild(2).GetComponent<TMP_Text>().fontSize = shopItems[i].NameSize;
  39.             ab.transform.GetChild(3).GetComponent<TMP_Text>().fontSize = shopItems[i].DescrSize;
  40.  
  41.             shopItems[i].IF = ab.transform.GetChild(6).GetComponent<TMP_InputField>();
  42.  
  43.             if (shopItems[i].ItemFab != null)
  44.             {
  45.                 ab.transform.GetChild(4).GetComponent<Button>().onClick.AddListener(() => Test11(shopItems[index].ItemFab));
  46.                 ab.transform.GetChild(5).GetComponent<Button>().onClick.AddListener(() => Test55(shopItems[index].ItemFab));
  47.                 ab.transform.GetChild(7).GetComponent<Button>().onClick.AddListener(() => Test5(shopItems[index].ItemFab));
  48.                 ab.transform.GetChild(8).GetComponent<Button>().onClick.AddListener(() => Test1(shopItems[index].ItemFab));
  49.             } else
  50.             {
  51.                 Debug.LogWarning("Something Went Wrong with the Buying Buttons!");
  52.             }
  53.  
  54.         }
  55.     }
  56.  
  57.     public void FixedUpdate()
  58.     {
  59.         for (int i = 0; i < shopItems.Length; i++)
  60.         {
  61.             shopItems[i].HowMany = Items.Where(x => x.gameObject == shopItems[i].ItemFab).ToArray().Length;
  62.             shopItems[i].HowMany = Mathf.Clamp(shopItems[i].HowMany, 0f, 9f);
  63.             shopItems[i].IF.text = shopItems[i].HowMany.ToString();
  64.         }
  65.         float hasToPay = 0f;
  66.         for (int i = 0; i < shopItems.Length; i++)
  67.         {
  68.             hasToPay += shopItems[i].HowMany * shopItems[i].Price;
  69.         }
  70.         ToPayToday.text = "Costs: " + hasToPay.ToString();
  71.     }
  72.  
  73.     public void Test1(GameObject Item)
  74.     {
  75.         if (Items.Where(x => x.gameObject == Item).ToArray().Length >= 9f)
  76.             return;
  77.         Items.Add(Item);
  78.     }
  79.  
  80.     public void Test11(GameObject Item)
  81.     {
  82.         if (Items.Where(x => x.gameObject == Item).ToArray().Length >= 1f)
  83.             Items.Remove(Item);
  84.     }
  85.  
  86.     public void Test5(GameObject Item)
  87.     {
  88.         if (Items.Where(x => x.gameObject == Item).ToArray().Length >= 6f)
  89.             return;
  90.         Items.Add(Item);
  91.         Items.Add(Item);
  92.         Items.Add(Item);
  93.         Items.Add(Item);
  94.         Items.Add(Item);
  95.     }
  96.  
  97.     public void Test55(GameObject Item)
  98.     {
  99.         if (Items.Where(x => x.gameObject == Item).ToArray().Length >= 5f)
  100.         {
  101.             Items.Remove(Item);
  102.             Items.Remove(Item);
  103.             Items.Remove(Item);
  104.             Items.Remove(Item);
  105.             Items.Remove(Item);
  106.         }        
  107.     }
  108.  
  109.     [Command]
  110.     public void CmdSpawnBox()
  111.     {
  112.         float AmountOfFurniture = 0f;
  113.         for (int i = 0; i < shopItems.Length; i++)
  114.         {
  115.             AmountOfFurniture += shopItems[i].HowMany;
  116.         }
  117.  
  118.         if (AmountOfFurniture == 0)
  119.             return;
  120.  
  121.         GameObject abc = (GameObject)Instantiate(BoxFab, BoxHolder.position, Quaternion.identity, BoxHolder) as GameObject;
  122.         NetworkServer.Spawn(abc);
  123.         abc.gameObject.GetComponent<UnboxingBox>().List = Items;
  124.         abc.gameObject.GetComponent<UnboxingBox>().WorldGO = WorldGO;
  125.         Items = new List<GameObject>();
  126.         for (int i = 0; i < shopItems.Length; i++)
  127.         {
  128.             shopItems[i].HowMany = 0f;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement