ScrapYard.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using GadgetCore.API;
  2. using ScrapYard.API;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace ScrapYard
  9. {
  10. [Gadget("ScrapYard", GadgetVersionSpecificity: VersionSpecificity.NONBREAKING)]
  11. public class ScrapYard : Gadget<ScrapYard>
  12. {
  13. public const string MOD_VERSION = "1.4.1"; // Set this to the version of your mod.
  14. public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.
  15. protected override void LoadConfig()
  16. {
  17. Config.Load();
  18. string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  19. if (fileVersion != CONFIG_VERSION)
  20. {
  21. Config.Reset();
  22. Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  23. }
  24. Config.Save();
  25. }
  26. public override string GetModDescription()
  27. {
  28. return "A mod that adds a scryp yard planet. Lite Version.";
  29. }
  30. protected override void Initialize()
  31. {
  32. Logger.Log("Scrap Yard Lite v" + Info.Mod.Version);
  33. Core.logger = Logger;
  34. Texture2D textureScrapTrophy = GadgetCoreAPI.LoadTexture2D("iScrapTrophy.png");
  35. ItemInfo item = new ItemInfo(ItemType.GENERIC, "Scrap Trophy", "A rusty trophy.\nUsed for purchasing\nspecial items.", textureScrapTrophy);
  36. int itemID = ItemRegistry.Singleton.Register(item, "Scrap Trophy");
  37. if (itemID == -1)
  38. throw new System.Exception("Could not register item " + "Scrap Trophy");
  39. { // Shop Stand
  40. var asset = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
  41. scrapYardStand = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/scrapyardstand.prefab"));
  42. scrapYardStand.SetActive(false);
  43. scrapYardStand.AddComponent<ScrapYardShopStandScript>();
  44. scrapYardStand.name = "scrapyardstand";
  45. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  46. {
  47. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  48. };
  49. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  50. {
  51. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  52. };
  53. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/stand", scrapYardStand);
  54. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstand", "Degug item.", null as Texture2D).Register();
  55. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstand"), tileItem);
  56. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstand");
  57. if (tileID == -1)
  58. throw new System.Exception("Could not register Tile " + "scrapyardstand");
  59. tile.OnInteract += OnInteractStand;
  60. }
  61. ShopPlatform.Reset();
  62. // Setting up default shop platforms
  63. new ShopPlatform("Default Objects", ShopPlatformSpacingType.Near).Register("DefaultObjects");
  64. new ShopPlatform("Default Blocks", ShopPlatformSpacingType.Near).Register("DefaultBlocks");
  65. new ShopPlatform("Default Walls", ShopPlatformSpacingType.Near).Register("DefaultWalls");
  66. SceneManager.sceneLoaded += OnSceneLoaded;
  67. }
  68. private static GameObject scrapYardStand;
  69. internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  70. {
  71. if (scene.buildIndex == 1)
  72. {
  73. InstanceTracker.GameScript.StartCoroutine(CreateStands());
  74. try
  75. {
  76. var storageMenu = GameObject.Find("Ship").transform.Find("SHIPPLACES").transform.Find("NPCs").transform.Find("npcFlora").localPosition = new Vector3(-134.58f, 11.72f, -0.19f);
  77. }
  78. catch (System.Exception e) { Core.logger.LogError(e.Message); }
  79. }
  80. }
  81. private static IEnumerator CreateStands()
  82. {
  83. while (!Network.isServer && !Network.isClient)
  84. {
  85. yield return new WaitForSeconds(0.25f);
  86. }
  87. yield return new WaitForSeconds(0.75f);
  88. try
  89. {
  90. {
  91. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * -5.5f, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  92. stand.GetComponent<ScrapYardShopStandScript>().GetComponent<ScrapYardShopStandScript>().StartCallSet(ItemRegistry.GetItemIDByRegistryName("ScrapYard:Scrap Trophy"), 9999, 1, 57);
  93. }
  94. List<ShopPlatformEntry> blocks = new List<ShopPlatformEntry>();
  95. List<ShopPlatformEntry> objects = new List<ShopPlatformEntry>();
  96. foreach (var p in ShopPlatform.GetShopPlatforms())
  97. if (p == ShopPlatform.DefaultBlocks || p == ShopPlatform.DefaultWalls)
  98. foreach (var e in p.Entries)
  99. blocks.Add(e);
  100. else
  101. foreach (var e in p.Entries)
  102. objects.Add(e);
  103. for (int i = 0; i < System.Math.Min(objects.Count, 3); i++)
  104. {
  105. var entry = objects[i];
  106. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * i, -5.99f - 2 + 0.5f, 0.19f), Quaternion.identity, 0);
  107. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
  108. }
  109. for (int i = 0; i < System.Math.Min(blocks.Count, 5); i++)
  110. {
  111. var entry = blocks[i];
  112. var stand = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(-250f + 4 * 7 + 4 * i, -5.99f + 6 + 0.5f, 0.19f), Quaternion.identity, 0);
  113. stand.GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
  114. }
  115. if (objects.Count > 3 || blocks.Count > 5)
  116. Core.logger.LogWarning("There are more stands than can be added to your ship. U should use the non Lite version of Scrap Yard");
  117. }
  118. catch (System.Exception e)
  119. {
  120. Core.logger.LogError(e);
  121. }
  122. yield break;
  123. }
  124. public static readonly FieldInfo canInteractField = typeof(PlayerScript).GetField("canInteract", BindingFlags.NonPublic | BindingFlags.Instance);
  125. public static readonly FieldInfo interactingField = typeof(PlayerScript).GetField("interacting", BindingFlags.NonPublic | BindingFlags.Instance);
  126. private IEnumerator OnInteractStand()
  127. {
  128. try
  129. {
  130. PlayerScript.curInteractObj.SendMessage("Request");
  131. canInteractField.SetValue(InstanceTracker.PlayerScript, true);
  132. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  133. InstanceTracker.PlayerScript.w.SetActive(true);
  134. }
  135. catch (System.Exception e)
  136. {
  137. Core.logger.Log(e);
  138. }
  139. yield break;
  140. }
  141. }
  142. }