ScrapYard.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using GadgetCore.API;
  2. using ScrapYard.API;
  3. using System.Collections;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. namespace ScrapYard
  8. {
  9. [Gadget("ScrapYard")]
  10. public class ScrapYard : Gadget<ScrapYard>
  11. {
  12. public const string MOD_VERSION = "1.2"; // Set this to the version of your mod.
  13. public const string CONFIG_VERSION = "1.0"; // Increment this whenever you change your mod's config file.
  14. protected override void LoadConfig()
  15. {
  16. Config.Load();
  17. string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  18. if (fileVersion != CONFIG_VERSION)
  19. {
  20. Config.Reset();
  21. Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
  22. }
  23. Config.Save();
  24. }
  25. public override string GetModDescription()
  26. {
  27. return "A mod that adds a scryp yard planet.";
  28. }
  29. protected override void Initialize()
  30. {
  31. Logger.Log("Scrap Yard v" + Info.Mod.Version);
  32. Core.logger = Logger;
  33. Texture2D texturePlanetIcon = GadgetCoreAPI.LoadTexture2D("planetScrapYard.png");
  34. Texture2D texturePlanetPrevIcon = GadgetCoreAPI.LoadTexture2D("planetScrapYardPrev.png");
  35. Texture2D texturePortalSign = GadgetCoreAPI.LoadTexture2D("signScrapYard.png");
  36. Texture2D textureBG = GadgetCoreAPI.LoadTexture2D("bgScrapYard.png");
  37. Texture2D textureBGExtension = GadgetCoreAPI.LoadTexture2D("bgScrapYardExtension.png");
  38. Texture2D textureBGTop = GadgetCoreAPI.LoadTexture2D("bgScrapYardTop.png");
  39. Texture2D textureParalex = GadgetCoreAPI.LoadTexture2D("parallax.png");
  40. Texture2D textureParalex0 = GadgetCoreAPI.LoadTexture2D("bScrapYardbg0.png");
  41. Texture2D textureParalex1 = GadgetCoreAPI.LoadTexture2D("bScrapYardbg1.png");
  42. Texture2D textureParalex2 = GadgetCoreAPI.LoadTexture2D("bScrapYardbg2.png");
  43. Texture2D textureParalex3 = GadgetCoreAPI.LoadTexture2D("bScrapYardbg3.png");
  44. Texture2D textureEntrance = GadgetCoreAPI.LoadTexture2D("entranceScrapYard.png");
  45. Texture2D textureSmallSide = GadgetCoreAPI.LoadTexture2D("sideSmallScrapYard.png");
  46. Texture2D textureBigSide = GadgetCoreAPI.LoadTexture2D("sideBigScrapYard.png");
  47. Texture2D textureMid0 = GadgetCoreAPI.LoadTexture2D("midScrapYardChunk0.png");
  48. Texture2D textureMid1 = GadgetCoreAPI.LoadTexture2D("midScrapYardChunk1.png");
  49. Texture2D textureScrapTrophy = GadgetCoreAPI.LoadTexture2D("iScrapTrophy.png");
  50. Logger.Log("Loaded Textures");
  51. PlanetInfo scrapYardPlanet = new PlanetInfo(PlanetType.SPECIAL, "Scrap Yard", new GadgetCore.Util.Tuple<int, int>[] { new GadgetCore.Util.Tuple<int, int>(1, 100) });
  52. scrapYardPlanet.SetPortalInfo(texturePortalSign, texturePlanetPrevIcon, texturePlanetIcon);
  53. scrapYardPlanet.SetBackgroundInfo(textureParalex, textureParalex0, textureParalex1, textureParalex2, textureParalex3);
  54. scrapYardPlanet.SetTerrainInfo(textureEntrance, textureBG, textureMid0, textureMid1, textureBigSide, textureSmallSide);
  55. scrapYardPlanet.OnGenerateWorld += ScrapYardGenerator.GenarateTown;
  56. scrapYardPlanet.Register("Scrap Yard");
  57. scrapYardPlanet.PortalUses = -1;
  58. Logger.Log("Created Planet");
  59. new ItemInfo(ItemType.GENERIC, "Scrap Trophy", "A rusty trophy.\nUsed for purchasing\nspecial items.", textureScrapTrophy).Register("Scrap Trophy");
  60. { // Scrap Yard Bulding
  61. var scrapYardShopBuilding = Object.Instantiate((GameObject)Resources.Load("prop/2501"));
  62. scrapYardShopBuilding.transform.localScale *= 8;
  63. Texture2D texture = GadgetCoreAPI.LoadTexture2D("shopStand.png");
  64. var renderer = scrapYardShopBuilding.GetComponentInChildren<Renderer>();
  65. renderer.material = new Material(Shader.Find("Unlit/Transparent"))
  66. {
  67. mainTexture = texture
  68. };
  69. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/scrapYardShopBuilding", scrapYardShopBuilding);
  70. }
  71. { // Scrap Yard Shop Owner
  72. var scrapYardMerchant = Object.Instantiate((GameObject)Resources.Load("npc/perceval"));
  73. Texture2D textureBody = GadgetCoreAPI.LoadTexture2D("merchantBody.png");
  74. Texture2D textureHead = GadgetCoreAPI.LoadTexture2D("merchantHead.png");
  75. var gameObjectBody = scrapYardMerchant.transform.Find("e").Find("perceval").FindChild("Plane_002");
  76. var rendererBody = gameObjectBody.gameObject.GetComponentInChildren<Renderer>();
  77. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  78. {
  79. mainTexture = textureBody
  80. };
  81. var gameObjectHead = scrapYardMerchant.transform.Find("e").Find("perceval").FindChild("Plane");
  82. var rendererHead = gameObjectHead.gameObject.GetComponentInChildren<Renderer>();
  83. rendererHead.material = new Material(Shader.Find("Unlit/Transparent"))
  84. {
  85. mainTexture = textureHead
  86. };
  87. //scrapYardMerchant.layer = 0;
  88. scrapYardMerchant.transform.Find("e").rotation = Quaternion.Euler(0f, 180f, 0f);
  89. scrapYardMerchant.name = "scrapyardmerchant";
  90. Component.DestroyImmediate(scrapYardMerchant.GetComponent<npcTurnScript>());
  91. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/scrapYardMerchant", scrapYardMerchant);
  92. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardmerchant")).Register("scrapyardmerchant");
  93. tile.OnInteract += OnInteractMerchant;
  94. }
  95. { // Part Semicolider Platform
  96. var asset = GadgetCoreAPI.LoadAssetBundle("platform");
  97. var platform = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/platform.prefab"));
  98. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/component/platform", platform);
  99. }
  100. { // Bottom Chunk
  101. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  102. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  103. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  104. {
  105. mainTexture = textureBG
  106. };
  107. for (int i = 0; i < 6; i++)
  108. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  109. for (int i = 0; i < 16; i++)
  110. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  111. gameObject.transform.GetChild(0).GetChild(7).localScale += new Vector3(1.5f, 0, 0);
  112. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(5).gameObject);
  113. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(1).gameObject);
  114. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  115. var platformComponent = Object.Instantiate((GameObject)Resources.Load("prop/ScrapYard/component/platform"));
  116. platformComponent.transform.SetParent(gameObject.transform);
  117. platformComponent.transform.localScale = new Vector3(0.5625f, 10, 1);
  118. platformComponent.transform.localPosition = new Vector3(0, 0.25f, 0);
  119. platformComponent.transform.localRotation = Quaternion.Euler(90, 0, 0);
  120. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkBase", gameObject);
  121. }
  122. { // Extension Chunk
  123. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  124. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  125. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  126. {
  127. mainTexture = textureBGExtension
  128. };
  129. for (int i = 0; i < 6; i++)
  130. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  131. for (int i = 0; i < 16; i++)
  132. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  133. for (int i = 0; i < 6; i++)
  134. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);
  135. gameObject.transform.GetChild(0).GetChild(0).localScale = new Vector3(0.4375f, 2, 1);
  136. gameObject.transform.GetChild(0).GetChild(0).localPosition = new Vector3(0.78125f, 0, 0);
  137. gameObject.transform.GetChild(0).GetChild(1).localScale = new Vector3(0.4375f, 2, 1);
  138. gameObject.transform.GetChild(0).GetChild(1).localPosition = new Vector3(-0.78125f, 0, 0);
  139. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  140. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkExtension", gameObject);
  141. }
  142. { // Top Chunk
  143. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  144. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  145. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  146. {
  147. mainTexture = textureBGTop
  148. };
  149. for (int i = 0; i < 6; i++)
  150. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  151. for (int i = 0; i < 16; i++)
  152. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  153. for (int i = 0; i < 5; i++)
  154. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);
  155. gameObject.transform.GetChild(0).GetChild(0).localPosition = new Vector3(0.78125f, 0, 0);
  156. gameObject.transform.GetChild(0).GetChild(0).localScale = new Vector3(0.4375f, 2, 1);
  157. gameObject.transform.GetChild(0).GetChild(1).localPosition = new Vector3(-0.78125f, 0, 0);
  158. gameObject.transform.GetChild(0).GetChild(1).localScale = new Vector3(0.4375f, 2, 1);
  159. gameObject.transform.GetChild(0).GetChild(2).localPosition = new Vector3(0, 0.5f, 0);
  160. gameObject.transform.GetChild(0).GetChild(2).localScale = new Vector3(2f, 0.5f, 1);
  161. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  162. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkTop", gameObject);
  163. }
  164. { // Platform
  165. var gameObject = Object.Instantiate((GameObject)Resources.Load("prop/2501"));
  166. gameObject.transform.localScale = new Vector3(12, 2, 1);
  167. Texture2D texture = GadgetCoreAPI.LoadTexture2D("metalPlatform.png");
  168. var renderer = gameObject.GetComponentInChildren<Renderer>();
  169. renderer.material = new Material(Shader.Find("Unlit/Transparent"))
  170. {
  171. mainTexture = texture
  172. };
  173. var platformComponent = Object.Instantiate((GameObject)Resources.Load("prop/ScrapYard/component/platform"));
  174. platformComponent.transform.SetParent(gameObject.transform);
  175. platformComponent.transform.localScale = new Vector3(2, 2, 1);
  176. platformComponent.transform.localPosition = new Vector3(0, 0, 0);
  177. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/metalPlatform", gameObject);
  178. }
  179. { // Shop Stand
  180. var asset = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
  181. var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/scrapyardstand.prefab"));
  182. scrapYardStand.SetActive(false);
  183. scrapYardStand.AddComponent<ScrapYardShopStandScript>();
  184. scrapYardStand.name = "scrapyardstand";
  185. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/stand", scrapYardStand);
  186. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  187. {
  188. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  189. };
  190. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  191. {
  192. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  193. };
  194. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstand")).Register("scrapyardstand");
  195. tile.OnInteract += OnInteractStand;
  196. }
  197. Logger.Log("Created Prefabes");
  198. // Setting up default shop platforms
  199. new ShopPlatform("Default Objects", ShopPlatformSpacingType.Near).Register();
  200. new ShopPlatform("Default Blocks", ShopPlatformSpacingType.Near).Register();
  201. new ShopPlatform("Default Walls", ShopPlatformSpacingType.Near).Register();
  202. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2000, 100));
  203. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2001, 100));
  204. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2002, 500));
  205. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2400, 7000));
  206. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2100, 10));
  207. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2101, 10));
  208. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2102, 10));
  209. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2103, 10));
  210. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2104, 10));
  211. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2105, 10));
  212. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2106, 10));
  213. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2107, 10));
  214. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2108, 10));
  215. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2403, 10));
  216. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(ItemRegistry.GetItemIDByRegistryName("Gadget Core:Crafter Block"), 10));
  217. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2401, 1000));
  218. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2402, 1000));
  219. ShopPlatform.DefaultWalls.AddShopPlatformEntry(new ShopPlatformEntry(2200, 50));
  220. Logger.Log("Added Default Objects to the Planet");
  221. SceneManager.sceneLoaded += OnSceneLoaded;
  222. Logger.Log("Added Scene Hook");
  223. }
  224. internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  225. {
  226. if (scene.buildIndex == 1)
  227. {
  228. try
  229. {
  230. var storageMenu = GameObject.Find("Ship").transform.Find("SHIPPLACES").transform.Find("mech").localPosition = new Vector3(-130f, -5.23f, 0.6f);
  231. Core.logger.Log("Moved the Mech");
  232. }
  233. catch (System.Exception e) { Core.logger.LogError(e.Message); }
  234. }
  235. }
  236. public static readonly FieldInfo canInteractField = typeof(PlayerScript).GetField("canInteract", BindingFlags.NonPublic | BindingFlags.Instance);
  237. public static readonly FieldInfo interactingField = typeof(PlayerScript).GetField("interacting", BindingFlags.NonPublic | BindingFlags.Instance);
  238. private IEnumerator OnInteractStand()
  239. {
  240. Core.logger.Log("Start Interaction Stand");
  241. try
  242. {
  243. PlayerScript.curInteractObj.SendMessage("Request");
  244. Core.logger.Log("Requested Item");
  245. canInteractField.SetValue(InstanceTracker.PlayerScript, true);
  246. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  247. InstanceTracker.PlayerScript.w.SetActive(true);
  248. Core.logger.Log("Finished Interaction Stand");
  249. }
  250. catch(System.Exception e)
  251. {
  252. Core.logger.Log(e);
  253. }
  254. yield break;
  255. }
  256. public static readonly Material merchantPortraitMaterial = new Material(Shader.Find("Unlit/Transparent"))
  257. {
  258. mainTexture = GadgetCoreAPI.LoadTexture2D("merchantPortrait.png"),
  259. mainTextureScale = new Vector2(0.5f, 1)
  260. };
  261. public static readonly FieldInfo curAField = typeof(GameScript).GetField("curA", BindingFlags.NonPublic | BindingFlags.Instance);
  262. public static readonly FieldInfo curBField = typeof(GameScript).GetField("curB", BindingFlags.NonPublic | BindingFlags.Instance);
  263. public static readonly FieldInfo curCField = typeof(GameScript).GetField("curC", BindingFlags.NonPublic | BindingFlags.Instance);
  264. private IEnumerator OnInteractMerchant()
  265. {
  266. InstanceTracker.GameScript.hoverItem.SetActive(false);
  267. if (GameScript.inventoryOpen)
  268. {
  269. InstanceTracker.GameScript.inventoryMain.SetActive(false);
  270. GameScript.inventoryOpen = false;
  271. }
  272. InstanceTracker.GameScript.talkingPortrait.GetComponent<Renderer>().material = merchantPortraitMaterial;
  273. InstanceTracker.GameScript.txtTalkingName[0].text = "Ito";
  274. InstanceTracker.GameScript.txtTalkingName[1].text = InstanceTracker.GameScript.txtTalkingName[0].text;
  275. InstanceTracker.GameScript.StartCoroutine(InstanceTracker.GameScript.EnterTalking2());
  276. InstanceTracker.GameScript.menuTalking.SendMessage("Set", "Haz a look at my warez!!!");
  277. canInteractField.SetValue(InstanceTracker.PlayerScript, false);
  278. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  279. curAField.SetValue(InstanceTracker.GameScript, -1);
  280. curBField.SetValue(InstanceTracker.GameScript, -1);
  281. curCField.SetValue(InstanceTracker.GameScript, -1);
  282. yield break;
  283. }
  284. }
  285. }