Quellcode durchsuchen

[2.0.5.2] added chip shop stands

Zariteis vor 4 Jahren
Ursprung
Commit
c9d4cb4d87

+ 3 - 2
API/ShopPlatform.cs

@@ -57,9 +57,10 @@ namespace ScrapYard.API
     {
       foreach (var e in Entries)
         if (e.CurrencyItemID == row.CurrencyItemID
-          && e.ItemID == row.ItemID
+          && e.Id == row.Id
           && e.Quantity == row.Quantity
-          && e.Price == row.Price)
+          && e.Price == row.Price
+          && e.EntryType == row.EntryType)
           return;
       Entries.Add(row);
     }

+ 19 - 4
API/ShopPlatformEntry.cs

@@ -1,18 +1,33 @@
-namespace ScrapYard.API
+using System;
+
+namespace ScrapYard.API
 {
   public class ShopPlatformEntry
   {
-    public int ItemID { get; protected set; }
+    [Obsolete("Use Id insted (since this now also allows chip Ids)")]
+    public int ItemID { get { return Id; } }
+    public int Id { get; protected set; }
     public int Price { get; protected set; }
     public int Quantity { get; protected set; }
     public int CurrencyItemID { get; protected set; }
+    public ShopPlatformEntryType EntryType { get; protected set; }
+
+    public ShopPlatformEntry(int id, int price, int quantity, int currencyItemID)
+    {
+      this.Id = id;
+      this.Price = price;
+      this.CurrencyItemID = currencyItemID;
+      this.Quantity = quantity;
+      this.EntryType = ShopPlatformEntryType.Item;
+    }
 
-    public ShopPlatformEntry(int itemID, int price, int quantity = 1, int currencyItemID = 57)
+    public ShopPlatformEntry(int id, int price, int quantity = 1, int currencyItemID = 57, ShopPlatformEntryType type = ShopPlatformEntryType.Item)
     {
-      this.ItemID = itemID;
+      this.Id = id;
       this.Price = price;
       this.CurrencyItemID = currencyItemID;
       this.Quantity = quantity;
+      this.EntryType = type;
     }
   }
 }

+ 7 - 0
API/ShopPlatformEntryType.cs

@@ -0,0 +1,7 @@
+namespace ScrapYard.API
+{
+  public enum ShopPlatformEntryType
+  {
+    Item, Chip
+  }
+}

BIN
Assets/scrapYardStandChip.png


+ 28 - 3
ScrapYard.cs

@@ -11,7 +11,7 @@ namespace ScrapYard
   [Gadget("ScrapYard", RequiredOnClients: true, GadgetVersionSpecificity: VersionSpecificity.NONBREAKING)]
   public class ScrapYard : Gadget<ScrapYard>
   {
-    public const string MOD_VERSION = "1.6"; // Set this to the version of your mod.
+    public const string MOD_VERSION = "1.7"; // Set this to the version of your mod.
     public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.
 
     protected override void LoadConfig()
@@ -239,9 +239,9 @@ namespace ScrapYard
         GadgetCoreAPI.AddCustomResource("prop/ScrapYard/metalPlatform", gameObject);
       }
 
+      var assetStand = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
       { // Shop Stand
-        var asset = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
-        var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/scrapyardstand.prefab"));
+        var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)assetStand.LoadAsset("assets/resources/scrapyardstand.prefab"));
         scrapYardStand.SetActive(false);
         scrapYardStand.AddComponent<ScrapYardShopStandScript>();
         scrapYardStand.name = "scrapyardstand";
@@ -265,6 +265,31 @@ namespace ScrapYard
         tile.OnInteract += OnInteractStand;
       }
 
+      { // Shop Stand Chip
+        var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)assetStand.LoadAsset("assets/resources/scrapyardstand.prefab"));
+        scrapYardStand.SetActive(false);
+        scrapYardStand.AddComponent<ScrapYardShopStandChipScript>();
+        scrapYardStand.name = "scrapyardstandchip";
+        GadgetCoreAPI.AddCustomResource("prop/ScrapYard/standchip", scrapYardStand);
+
+        scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
+        {
+          mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandChip.png")
+        };
+
+        scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
+        {
+          mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
+        };
+
+        ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstandchip", "Degug item.", null as Texture2D).Register();
+        TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstandchip"), tileItem);
+        int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstandchip");
+        if (tileID == -1)
+          throw new System.Exception("Could not register Tile " + "scrapyardstandchip");
+        tile.OnInteract += OnInteractStand;
+      }
+
       ShopPlatform.Reset();
 
       // Setting up default shop platforms

+ 3 - 0
ScrapYard.csproj

@@ -232,6 +232,9 @@
     <None Update="Assets\scrapYardStand.png">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>
+    <None Update="Assets\scrapYardStandChip.png">
+      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+    </None>
     <None Update="Assets\scrapYardStandInfo.png">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>

+ 2 - 2
ScrapYardDecoBlinkScript.cs

@@ -9,13 +9,13 @@ namespace ScrapYard
   {
     private void Awake()
     {
-      StartCoroutine(Update());
+      StartCoroutine(Run());
     }
 
     private static Material materialOff = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("shopStand.png") };
     private static Material materialOn = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("shopStand_blink.png") };
 
-    private IEnumerator Update()
+    private IEnumerator Run()
     {
       while (true)
       {

+ 9 - 2
ScrapYardGenerator.cs

@@ -95,8 +95,15 @@ namespace ScrapYard
               temp++;
             }
             var step = (i % platform.Value.GetSpacesPerRow()) / (float)(platform.Value.GetSpacesPerRow() - 1);
-            networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(300 - (40 / 2) + (step * 40), rowNumber * 8f + 0.5f - 0.05f, 0.2f), Quaternion.identity, 0);
-            networkStuff[temp].GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
+            if (entry.EntryType == ShopPlatformEntryType.Item)
+            {
+              networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(300 - (40 / 2) + (step * 40), rowNumber * 8f + 0.5f - 0.05f, 0.2f), Quaternion.identity, 0);
+              networkStuff[temp].GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.Id, entry.Price, entry.Quantity, entry.CurrencyItemID);
+            }else if (entry.EntryType == ShopPlatformEntryType.Chip)
+            {
+              networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/standchip"), new Vector3(300 - (40 / 2) + (step * 40), rowNumber * 8f + 0.5f - 0.05f, 0.2f), Quaternion.identity, 0);
+              networkStuff[temp].GetComponent<ScrapYardShopStandChipScript>().StartCallSet(entry.Id, entry.Price, entry.CurrencyItemID);
+            }
             temp++;
           }
         }

+ 138 - 0
ScrapYardShopStandChipScript.cs

@@ -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;
+  }
+}