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.3"; // 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. PlanetInfo scrapYardPlanet = new PlanetInfo(PlanetType.SPECIAL, "Scrap Yard", new GadgetCore.Util.Tuple<int, int>[] { new GadgetCore.Util.Tuple<int, int>(1, 100) });
  51. scrapYardPlanet.SetPortalInfo(texturePortalSign, texturePlanetPrevIcon, texturePlanetIcon);
  52. scrapYardPlanet.SetBackgroundInfo(textureParalex, textureParalex0, textureParalex1, textureParalex2, textureParalex3);
  53. scrapYardPlanet.SetTerrainInfo(textureEntrance, textureBG, textureMid0, textureMid1, textureBigSide, textureSmallSide);
  54. scrapYardPlanet.OnGenerateWorld += ScrapYardGenerator.GenarateTown;
  55. scrapYardPlanet.Register("Scrap Yard");
  56. scrapYardPlanet.PortalUses = -1;
  57. ItemInfo item = new ItemInfo(ItemType.GENERIC, "Scrap Trophy", "A rusty trophy.\nUsed for purchasing\nspecial items.", textureScrapTrophy);
  58. int itemID = ItemRegistry.Singleton.Register(item, "Scrap Trophy");
  59. if (itemID == -1)
  60. throw new System.Exception("Could not register item " + "Scrap Trophy");
  61. { // Scrap Yard Bulding
  62. var scrapYardShopBuilding = Object.Instantiate((GameObject)Resources.Load("prop/2501"));
  63. scrapYardShopBuilding.transform.localScale *= 8;
  64. Texture2D texture = GadgetCoreAPI.LoadTexture2D("shopStand.png");
  65. var renderer = scrapYardShopBuilding.GetComponentInChildren<Renderer>();
  66. renderer.material = new Material(Shader.Find("Unlit/Transparent"))
  67. {
  68. mainTexture = texture
  69. };
  70. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/scrapYardShopBuilding", scrapYardShopBuilding);
  71. }
  72. { // Scrap Yard Shop Owner
  73. var scrapYardMerchant = Object.Instantiate((GameObject)Resources.Load("npc/perceval"));
  74. Texture2D textureBody = GadgetCoreAPI.LoadTexture2D("merchantBody.png");
  75. Texture2D textureHead = GadgetCoreAPI.LoadTexture2D("merchantHead.png");
  76. var gameObjectBody = scrapYardMerchant.transform.Find("e").Find("perceval").FindChild("Plane_002");
  77. var rendererBody = gameObjectBody.gameObject.GetComponentInChildren<Renderer>();
  78. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  79. {
  80. mainTexture = textureBody
  81. };
  82. var gameObjectHead = scrapYardMerchant.transform.Find("e").Find("perceval").FindChild("Plane");
  83. var rendererHead = gameObjectHead.gameObject.GetComponentInChildren<Renderer>();
  84. rendererHead.material = new Material(Shader.Find("Unlit/Transparent"))
  85. {
  86. mainTexture = textureHead
  87. };
  88. //scrapYardMerchant.layer = 0;
  89. scrapYardMerchant.transform.Find("e").rotation = Quaternion.Euler(0f, 180f, 0f);
  90. scrapYardMerchant.name = "scrapyardmerchant";
  91. Component.DestroyImmediate(scrapYardMerchant.GetComponent<npcTurnScript>());
  92. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/scrapYardMerchant", scrapYardMerchant);
  93. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardmerchant", "Degug item.", null as Texture2D).Register();
  94. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardmerchant"), tileItem);
  95. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardmerchant");
  96. if (tileID == -1)
  97. throw new System.Exception("Could not register Tile " + "scrapyardmerchant");
  98. tile.OnInteract += OnInteractMerchant;
  99. }
  100. { // Part Semicolider Platform
  101. var asset = GadgetCoreAPI.LoadAssetBundle("platform");
  102. var platform = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/platform.prefab"));
  103. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/component/platform", platform);
  104. }
  105. { // Bottom Chunk
  106. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  107. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  108. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  109. {
  110. mainTexture = textureBG
  111. };
  112. for (int i = 0; i < 6; i++)
  113. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  114. for (int i = 0; i < 16; i++)
  115. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  116. gameObject.transform.GetChild(0).GetChild(7).localScale += new Vector3(1.5f, 0, 0);
  117. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(5).gameObject);
  118. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(1).gameObject);
  119. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  120. var platformComponent = Object.Instantiate((GameObject)Resources.Load("prop/ScrapYard/component/platform"));
  121. platformComponent.transform.SetParent(gameObject.transform);
  122. platformComponent.transform.localScale = new Vector3(0.5625f, 10, 1);
  123. platformComponent.transform.localPosition = new Vector3(0, 0.25f, 0);
  124. platformComponent.transform.localRotation = Quaternion.Euler(90, 0, 0);
  125. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkBase", gameObject);
  126. }
  127. { // Extension Chunk
  128. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  129. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  130. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  131. {
  132. mainTexture = textureBGExtension
  133. };
  134. for (int i = 0; i < 6; i++)
  135. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  136. for (int i = 0; i < 16; i++)
  137. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  138. for (int i = 0; i < 6; i++)
  139. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);
  140. gameObject.transform.GetChild(0).GetChild(0).localScale = new Vector3(0.4375f, 2, 1);
  141. gameObject.transform.GetChild(0).GetChild(0).localPosition = new Vector3(0.78125f, 0, 0);
  142. gameObject.transform.GetChild(0).GetChild(1).localScale = new Vector3(0.4375f, 2, 1);
  143. gameObject.transform.GetChild(0).GetChild(1).localPosition = new Vector3(-0.78125f, 0, 0);
  144. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  145. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkExtension", gameObject);
  146. }
  147. { // Top Chunk
  148. var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
  149. var rendererBody = gameObject.GetComponentInChildren<Renderer>();
  150. rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
  151. {
  152. mainTexture = textureBGTop
  153. };
  154. for (int i = 0; i < 6; i++)
  155. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
  156. for (int i = 0; i < 16; i++)
  157. GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);
  158. for (int i = 0; i < 5; i++)
  159. GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);
  160. gameObject.transform.GetChild(0).GetChild(0).localPosition = new Vector3(0.78125f, 0, 0);
  161. gameObject.transform.GetChild(0).GetChild(0).localScale = new Vector3(0.4375f, 2, 1);
  162. gameObject.transform.GetChild(0).GetChild(1).localPosition = new Vector3(-0.78125f, 0, 0);
  163. gameObject.transform.GetChild(0).GetChild(1).localScale = new Vector3(0.4375f, 2, 1);
  164. gameObject.transform.GetChild(0).GetChild(2).localPosition = new Vector3(0, 0.5f, 0);
  165. gameObject.transform.GetChild(0).GetChild(2).localScale = new Vector3(2f, 0.5f, 1);
  166. Component.DestroyImmediate(gameObject.GetComponent<Chunk>());
  167. GadgetCoreAPI.AddCustomResource("z/ScrapYard/chunkTop", gameObject);
  168. }
  169. { // Platform
  170. var gameObject = Object.Instantiate((GameObject)Resources.Load("prop/2501"));
  171. gameObject.transform.localScale = new Vector3(12, 2, 1);
  172. Texture2D texture = GadgetCoreAPI.LoadTexture2D("metalPlatform.png");
  173. var renderer = gameObject.GetComponentInChildren<Renderer>();
  174. renderer.material = new Material(Shader.Find("Unlit/Transparent"))
  175. {
  176. mainTexture = texture
  177. };
  178. var platformComponent = Object.Instantiate((GameObject)Resources.Load("prop/ScrapYard/component/platform"));
  179. platformComponent.transform.SetParent(gameObject.transform);
  180. platformComponent.transform.localScale = new Vector3(2, 2, 1);
  181. platformComponent.transform.localPosition = new Vector3(0, 0, 0);
  182. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/metalPlatform", gameObject);
  183. }
  184. { // Shop Stand
  185. var asset = GadgetCoreAPI.LoadAssetBundle("scrapyardstand");
  186. var scrapYardStand = UnityEngine.Object.Instantiate((GameObject)asset.LoadAsset("assets/resources/scrapyardstand.prefab"));
  187. scrapYardStand.SetActive(false);
  188. scrapYardStand.AddComponent<ScrapYardShopStandScript>();
  189. scrapYardStand.name = "scrapyardstand";
  190. GadgetCoreAPI.AddCustomResource("prop/ScrapYard/stand", scrapYardStand);
  191. scrapYardStand.transform.GetChild(0).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  192. {
  193. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStand.png")
  194. };
  195. scrapYardStand.transform.GetChild(2).GetComponent<Renderer>().material = new Material(Shader.Find("Unlit/Transparent"))
  196. {
  197. mainTexture = GadgetCoreAPI.LoadTexture2D("scrapYardStandInfo.png")
  198. };
  199. ItemInfo tileItem = new ItemInfo(ItemType.GENERIC, "scrapyardstand", "Degug item.", null as Texture2D).Register();
  200. TileInfo tile = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("scrapyardstand"), tileItem);
  201. int tileID = TileRegistry.Singleton.Register(tile, "scrapyardstand");
  202. if (tileID == -1)
  203. throw new System.Exception("Could not register Tile " + "scrapyardstand");
  204. tile.OnInteract += OnInteractStand;
  205. }
  206. // Setting up default shop platforms
  207. new ShopPlatform("Default Objects", ShopPlatformSpacingType.Near).Register();
  208. new ShopPlatform("Default Blocks", ShopPlatformSpacingType.Near).Register();
  209. new ShopPlatform("Default Walls", ShopPlatformSpacingType.Near).Register();
  210. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2000, 100));
  211. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2001, 100));
  212. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2002, 500));
  213. ShopPlatform.DefaultBlocks.AddShopPlatformEntry(new ShopPlatformEntry(2400, 7000));
  214. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2100, 10));
  215. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2101, 10));
  216. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2102, 10));
  217. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2103, 10));
  218. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2104, 10));
  219. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2105, 10));
  220. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2106, 10));
  221. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2107, 10));
  222. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2108, 10));
  223. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2403, 10));
  224. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(ItemRegistry.GetItemIDByRegistryName("Gadget Core:Crafter Block"), 10));
  225. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2401, 1000));
  226. ShopPlatform.DefaultObjects.AddShopPlatformEntry(new ShopPlatformEntry(2402, 1000));
  227. ShopPlatform.DefaultWalls.AddShopPlatformEntry(new ShopPlatformEntry(2200, 50));
  228. SceneManager.sceneLoaded += OnSceneLoaded;
  229. }
  230. internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  231. {
  232. if (scene.buildIndex == 1)
  233. {
  234. try
  235. {
  236. var storageMenu = GameObject.Find("Ship").transform.Find("SHIPPLACES").transform.Find("mech").localPosition = new Vector3(-130f, -5.23f, 0.6f);
  237. }
  238. catch (System.Exception e) { Core.logger.LogError(e.Message); }
  239. }
  240. }
  241. public static readonly FieldInfo canInteractField = typeof(PlayerScript).GetField("canInteract", BindingFlags.NonPublic | BindingFlags.Instance);
  242. public static readonly FieldInfo interactingField = typeof(PlayerScript).GetField("interacting", BindingFlags.NonPublic | BindingFlags.Instance);
  243. private IEnumerator OnInteractStand()
  244. {
  245. try
  246. {
  247. PlayerScript.curInteractObj.SendMessage("Request");
  248. canInteractField.SetValue(InstanceTracker.PlayerScript, true);
  249. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  250. InstanceTracker.PlayerScript.w.SetActive(true);
  251. }
  252. catch(System.Exception e)
  253. {
  254. Core.logger.Log(e);
  255. }
  256. yield break;
  257. }
  258. public static readonly Material merchantPortraitMaterial = new Material(Shader.Find("Unlit/Transparent"))
  259. {
  260. mainTexture = GadgetCoreAPI.LoadTexture2D("merchantPortrait.png"),
  261. mainTextureScale = new Vector2(0.5f, 1)
  262. };
  263. public static readonly FieldInfo curAField = typeof(GameScript).GetField("curA", BindingFlags.NonPublic | BindingFlags.Instance);
  264. public static readonly FieldInfo curBField = typeof(GameScript).GetField("curB", BindingFlags.NonPublic | BindingFlags.Instance);
  265. public static readonly FieldInfo curCField = typeof(GameScript).GetField("curC", BindingFlags.NonPublic | BindingFlags.Instance);
  266. private IEnumerator OnInteractMerchant()
  267. {
  268. InstanceTracker.GameScript.hoverItem.SetActive(false);
  269. if (GameScript.inventoryOpen)
  270. {
  271. InstanceTracker.GameScript.inventoryMain.SetActive(false);
  272. GameScript.inventoryOpen = false;
  273. }
  274. InstanceTracker.GameScript.talkingPortrait.GetComponent<Renderer>().material = merchantPortraitMaterial;
  275. InstanceTracker.GameScript.txtTalkingName[0].text = "Ito";
  276. InstanceTracker.GameScript.txtTalkingName[1].text = InstanceTracker.GameScript.txtTalkingName[0].text;
  277. InstanceTracker.GameScript.StartCoroutine(InstanceTracker.GameScript.EnterTalking2());
  278. InstanceTracker.GameScript.menuTalking.SendMessage("Set", "Haz a look at my warez!!!");
  279. canInteractField.SetValue(InstanceTracker.PlayerScript, false);
  280. interactingField.SetValue(InstanceTracker.PlayerScript, false);
  281. curAField.SetValue(InstanceTracker.GameScript, -1);
  282. curBField.SetValue(InstanceTracker.GameScript, -1);
  283. curCField.SetValue(InstanceTracker.GameScript, -1);
  284. yield break;
  285. }
  286. }
  287. }