|
|
@@ -0,0 +1,138 @@
|
|
|
+using GadgetCore.API;
|
|
|
+using System.Collections;
|
|
|
+using System.Reflection;
|
|
|
+using UnityEngine;
|
|
|
+
|
|
|
+namespace ScrapYard
|
|
|
+{
|
|
|
+ public class ScrapYardShopStandChipScript : MonoBehaviour
|
|
|
+ {
|
|
|
+ public static readonly FieldInfo inventoryField = typeof(GameScript).GetField("inventory", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
+
|
|
|
+ private void Awake()
|
|
|
+ {
|
|
|
+ UpdateUI();
|
|
|
+ }
|
|
|
+
|
|
|
+ [RPC]
|
|
|
+ private void Set(int chipID, int price, int currencyItemID)
|
|
|
+ {
|
|
|
+ this.chipID = ChipRegistry.Singleton.ConvertIDToLocal(chipID);
|
|
|
+ this.currencyItemID = ItemRegistry.Singleton.ConvertIDToLocal(currencyItemID);
|
|
|
+ this.price = price;
|
|
|
+ UpdateUI();
|
|
|
+ }
|
|
|
+
|
|
|
+ internal void StartCallSet(int chipID, int price, int currencyItemID)
|
|
|
+ {
|
|
|
+ StartCoroutine(CallSet(chipID, price, currencyItemID));
|
|
|
+ }
|
|
|
+
|
|
|
+ private IEnumerator CallSet(int chipID, int price, int currencyItemID)
|
|
|
+ {
|
|
|
+ yield return new WaitForSeconds(0.1f);
|
|
|
+ chipID = ChipRegistry.Singleton.ConvertIDToHost(chipID);
|
|
|
+ currencyItemID = ItemRegistry.Singleton.ConvertIDToHost(currencyItemID);
|
|
|
+ gameObject.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { chipID, price, currencyItemID });
|
|
|
+ yield break;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void UpdateUI()
|
|
|
+ {
|
|
|
+ gameObject.transform.GetChild(1).GetComponent<Animation>().Play("animationStand");
|
|
|
+ gameObject.transform.GetChild(1).GetComponent<Renderer>().material = (Material)Resources.Load("cc/cc" + chipID);
|
|
|
+ gameObject.transform.GetChild(2).GetChild(1).GetComponent<Renderer>().material = (Material)Resources.Load("i/i" + currencyItemID);
|
|
|
+
|
|
|
+ string title = InstanceTracker.GameScript.GetChipName(chipID);
|
|
|
+
|
|
|
+ gameObject.transform.GetChild(2).GetChild(0).GetComponent<TextMesh>().text = title;
|
|
|
+ gameObject.transform.GetChild(2).GetChild(0).GetChild(0).GetComponent<TextMesh>().text = title;
|
|
|
+ gameObject.transform.GetChild(2).GetChild(2).GetComponent<TextMesh>().text = "" + price;
|
|
|
+ gameObject.transform.GetChild(2).GetChild(2).GetChild(0).GetComponent<TextMesh>().text = "" + price;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnTriggerEnter(Collider c)
|
|
|
+ {
|
|
|
+ if (c.gameObject.layer == 8)
|
|
|
+ {
|
|
|
+ gameObject.transform.GetChild(2).gameObject.SetActive(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnTriggerExit(Collider c)
|
|
|
+ {
|
|
|
+ if (c.gameObject.layer == 8)
|
|
|
+ {
|
|
|
+ gameObject.transform.GetChild(2).gameObject.SetActive(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int GetItemAmount(GameScript gameScript, int id)
|
|
|
+ {
|
|
|
+ if (id == 0) return 0;
|
|
|
+ int num = 0;
|
|
|
+ Item[] inventory = (Item[])inventoryField.GetValue(gameScript);
|
|
|
+ for (int i = 0; i < 45; i++)
|
|
|
+ {
|
|
|
+ if (inventory[i].id == id)
|
|
|
+ {
|
|
|
+ num += inventory[i].q;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void RemoveItemAmount(GameScript gameScript, int id, int amount)
|
|
|
+ {
|
|
|
+ Item[] inventory = (Item[])inventoryField.GetValue(gameScript);
|
|
|
+ for (int i = 0; i < 45; i++)
|
|
|
+ {
|
|
|
+ if (inventory[i].id == id)
|
|
|
+ {
|
|
|
+ if (amount - inventory[i].q > 0)
|
|
|
+ {
|
|
|
+ amount -= inventory[i].q;
|
|
|
+ inventory[i] = gameScript.EmptyItem();
|
|
|
+ gameScript.RefreshSlot(i);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (amount - inventory[i].q == 0)
|
|
|
+ {
|
|
|
+ inventory[i] = gameScript.EmptyItem();
|
|
|
+ gameScript.RefreshSlot(i);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ inventory[i].q -= amount;
|
|
|
+ gameScript.RefreshSlot(i);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Request()
|
|
|
+ {
|
|
|
+ var gameScript = (GameScript)Camera.main.GetComponent("GameScript");
|
|
|
+ int num = GetItemAmount(gameScript, currencyItemID);
|
|
|
+
|
|
|
+ if (num >= price)
|
|
|
+ {
|
|
|
+ base.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/purchase"), Menuu.soundLevel / 10f);
|
|
|
+ RemoveItemAmount(gameScript, currencyItemID, price);
|
|
|
+
|
|
|
+ Item chip = new Item(chipID, 1, 0, 0, 0, new int[3], new int[3]);
|
|
|
+ GadgetCoreAPI.DropItem(base.transform.position, chip, true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ GameObject gameObject = (GameObject)Object.Instantiate(Resources.Load("txtError"), MenuScript.player.transform.position, Quaternion.identity);
|
|
|
+ gameObject.SendMessage("InitError", "Insufficient Currency!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int chipID = 1;
|
|
|
+ public int price = 1;
|
|
|
+ public int currencyItemID = 1;
|
|
|
+ }
|
|
|
+}
|