ShipGenerator.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using GadgetCore.API;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace Subworlds
  7. {
  8. internal enum ShipGeneratorObjectPoolType
  9. {
  10. SmallObject = 0,
  11. LargeObject = 1,
  12. FlyingEnemy = 2,
  13. BrokenItemMod = 3,
  14. BrokenChest = 4,
  15. OldChest = 5,
  16. BrokenLaser = 6
  17. }
  18. public class ShipGenerator
  19. {
  20. private static readonly FieldInfo chunksField = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.Instance);
  21. private static readonly Material materialaBackLight = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Ship/parallax.png") };
  22. private static readonly Material materialaParalexStars = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Ship/bgStars.png") };
  23. private static readonly Material materialaParalexNone = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Ship/bgNone.png") };
  24. public static readonly FieldInfo networkStuffField = typeof(Chunk).GetField("networkStuff", BindingFlags.NonPublic | BindingFlags.Instance);
  25. public static readonly FieldInfo tempField = typeof(Chunk).GetField("temp", BindingFlags.NonPublic | BindingFlags.Instance);
  26. public static void GenarateTown(SpawnerScript s, int[] param)
  27. {
  28. GameScript.isTown = true;
  29. if (GameScript.districtLevel % 2 != 0)
  30. {
  31. GameScript.districtLevel++;
  32. }
  33. s.backLights.SetActive(true);
  34. s.mainLight.SetActive(false);
  35. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  36. //s.mainLight.GetComponentInChildren<Light>().color = new Color(0.05f, 0.05f, 0.05f, 0.1f);
  37. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  38. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  39. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  40. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  41. var chunks = (GameObject[])chunksField.GetValue(s);
  42. var num = 0;
  43. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/ShuttleMain"), new Vector3((float)(218 + 0 * 128 + 4 * 8), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  44. num++;
  45. if (Network.isServer)
  46. {
  47. chunks[num] = new GameObject("chunk");
  48. var chunk = chunks[num].AddComponent<Chunk>();
  49. PopulateTown(chunk);
  50. num++;
  51. }
  52. }
  53. public static void Genarate(SpawnerScript s, int[] param)
  54. {
  55. try
  56. {
  57. GameScript.isTown = false;
  58. spawnLocations.Clear();
  59. s.backLights.SetActive(true);
  60. s.mainLight.SetActive(false);
  61. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  62. //s.mainLight.GetComponentInChildren<Light>().color = new Color(0.05f, 0.05f, 0.05f, 0.1f);
  63. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  64. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  65. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  66. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  67. var chunks = (GameObject[])chunksField.GetValue(s);
  68. var num = 0;
  69. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/ShipAEntrance"), new Vector3((float)(218 + 0 * 128), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  70. num++;
  71. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/ShipAMain"), new Vector3((float)(218 + 0.75 * 128), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  72. num++;
  73. spawnLocations.Add(new int[] { 7, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  74. spawnLocations.Add(new int[] { 10, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  75. spawnLocations.Add(new int[] { 16, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  76. spawnLocations.Add(new int[] { 17, 4, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  77. spawnLocations.Add(new int[] { 32, 0, (int)ShipGeneratorObjectPoolType.BrokenLaser });
  78. Add4(4, 0);
  79. Add4(4, 5);
  80. Add4Floor(4, -5);
  81. Add4(10, 0);
  82. Add4(10, 5);
  83. Add4Floor(10, -5);
  84. Add4(20, 0);
  85. Add4(20, 5);
  86. Add4Floor(20, -5);
  87. Add4(26, 0);
  88. Add4(26, 5);
  89. Add4Floor(26, -5);
  90. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/ShipAMain"), new Vector3((float)(218 + 1.75 * 128), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  91. num++;
  92. spawnLocations.Add(new int[] { 7 + 32, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  93. spawnLocations.Add(new int[] { 10 + 32, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  94. spawnLocations.Add(new int[] { 16 + 32, 7, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  95. spawnLocations.Add(new int[] { 17 + 32, 4, (int)ShipGeneratorObjectPoolType.FlyingEnemy });
  96. spawnLocations.Add(new int[] { 32 + 32, 0, (int)ShipGeneratorObjectPoolType.BrokenLaser });
  97. Add4(4 + 32, 0);
  98. Add4(4 + 32, 5);
  99. Add4Floor(4 + 32, -5);
  100. Add4(10 + 32, 0);
  101. Add4(10 + 32, 5);
  102. Add4Floor(10 + 32, -5);
  103. Add4(20 + 32, 0);
  104. Add4(20 + 32, 5);
  105. Add4Floor(20 + 32, -5);
  106. Add4(26 + 32, 0);
  107. Add4(26 + 32, 5);
  108. Add4Floor(26 + 32, -5);
  109. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/ShipAExit"), new Vector3((float)(218 + 2.5 * 128), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  110. num++;
  111. if (Network.isServer)
  112. {
  113. chunks[num] = new GameObject("chunk");
  114. var chunk = chunks[num].AddComponent<Chunk>();
  115. PopulateWorld(chunk);
  116. num++;
  117. }
  118. }
  119. catch (System.Exception e) { Core.logger.LogConsole(e); }
  120. }
  121. private static void Add4(int x, int y)
  122. {
  123. var r = random.Next(8);
  124. switch (r)
  125. {
  126. case 0:
  127. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  128. break;
  129. case 1:
  130. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  131. break;
  132. case 2:
  133. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  134. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  135. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  136. break;
  137. case 3:
  138. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  139. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  140. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  141. break;
  142. case 4:
  143. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  144. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  145. break;
  146. case 5:
  147. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  148. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  149. break;
  150. case 6:
  151. if (random.Next(2) == 0)
  152. {
  153. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.BrokenItemMod });
  154. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.BrokenChest });
  155. }
  156. else
  157. {
  158. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.BrokenChest });
  159. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.BrokenItemMod });
  160. }
  161. break;
  162. case 7:
  163. if (random.Next(2) == 0)
  164. {
  165. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  166. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.OldChest });
  167. }
  168. else
  169. {
  170. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.OldChest });
  171. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.SmallObject });
  172. }
  173. break;
  174. }
  175. }
  176. private static void Add4Floor(int x, int y)
  177. {
  178. if (random.Next(2) == 0)
  179. Add4(x, y);
  180. else
  181. {
  182. var r = random.Next(5);
  183. switch (r)
  184. {
  185. case 0:
  186. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  187. break;
  188. case 1:
  189. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  190. break;
  191. case 2:
  192. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  193. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  194. break;
  195. case 3:
  196. spawnLocations.Add(new int[] { x + 1, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  197. spawnLocations.Add(new int[] { x + 3, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  198. break;
  199. case 4:
  200. spawnLocations.Add(new int[] { x + 0, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  201. spawnLocations.Add(new int[] { x + 2, y, (int)ShipGeneratorObjectPoolType.LargeObject });
  202. break;
  203. }
  204. }
  205. }
  206. private static List<int[]> spawnLocations = new List<int[]>();
  207. private static System.Random random = new System.Random();
  208. public static void PopulateTown(Chunk chunk)
  209. {
  210. var networkStuff = new GameObject[400];
  211. networkStuffField.SetValue(chunk, networkStuff);
  212. try
  213. {
  214. int temp = (int)tempField.GetValue(chunk);
  215. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npcStorage"), new Vector3(248f + 4 * 0, -6.118f, 0.3f), Quaternion.identity, 0);
  216. temp++;
  217. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc2103"), new Vector3(248f + 4 * 1, -6.118f, 0.3f), Quaternion.identity, 0);
  218. temp++;
  219. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc/npcQuest"), new Vector3(248f + 4 * 3, -6.118f, 0.3f), Quaternion.identity, 0);
  220. temp++;
  221. }
  222. catch (Exception e) { Core.logger.LogError(e); }
  223. GameScript.endPortal[0] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f + 4 * 5, -6.118f, 0f), Quaternion.identity, 0);
  224. GameScript.endPortalUA[0] = GameScript.endPortal[0].transform.GetChild(0).gameObject;
  225. GameScript.endPortal[0].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  226. GameScript.endPortalUA[0].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.lastBiome == -1 ? Core.shipPlanet.GetID() : Core.lastBiome, 0, 0 });
  227. GameScript.endPortal[3] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f - 4 * 2, -6.118f, 0f), Quaternion.identity, 0);
  228. GameScript.endPortalUA[3] = GameScript.endPortal[3].transform.GetChild(0).gameObject;
  229. GameScript.endPortal[3].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  230. GameScript.endPortalUA[3].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 98, 0, 3 });
  231. }
  232. public static void PopulateWorld(Chunk chunk)
  233. {
  234. var networkStuff = new GameObject[400];
  235. networkStuffField.SetValue(chunk, networkStuff);
  236. try
  237. {
  238. int temp = (int)tempField.GetValue(chunk);
  239. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenTeleporter"), new Vector3(248f - 4 * 1, -4f + 0.118f, 0.3f), Quaternion.identity, 0);
  240. temp++;
  241. foreach (var e in spawnLocations)
  242. {
  243. switch ((ShipGeneratorObjectPoolType)e[2])
  244. {
  245. case ShipGeneratorObjectPoolType.SmallObject:
  246. if (random.Next(30) == 0)
  247. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenItemMod"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  248. else if (random.Next(4) == 0)
  249. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenCrates1"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  250. else if (random.Next(3) == 0)
  251. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenCrates2"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  252. else if (random.Next(2) == 0)
  253. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenCrates3"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  254. else
  255. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenCrates4"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  256. break;
  257. case ShipGeneratorObjectPoolType.LargeObject:
  258. if (random.Next(6) == 0)
  259. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate1"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  260. else if (random.Next(5) == 0)
  261. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate2"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  262. else if (random.Next(4) == 0)
  263. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate3"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  264. else if (random.Next(3) == 0)
  265. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate4"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  266. else if (random.Next(2) == 0)
  267. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate5"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  268. else
  269. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenPirate6"), new Vector3(248f + 4 * e[0], -5.75f + 0.118f + 4 * e[1], 0.2f), Quaternion.identity, 0);
  270. break;
  271. case ShipGeneratorObjectPoolType.FlyingEnemy:
  272. Network.Instantiate(Resources.Load("e/Subworlds/BrokenSliver"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 1.1f + 4 * e[1], 0f), Quaternion.identity, 0);
  273. Network.Instantiate(Resources.Load("e/Subworlds/BrokenDroid"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 1.1f + 4 * e[1], 0f), Quaternion.identity, 0);
  274. break;
  275. case ShipGeneratorObjectPoolType.BrokenItemMod:
  276. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenItemMod"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  277. break;
  278. case ShipGeneratorObjectPoolType.BrokenChest:
  279. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/BrokenChest"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  280. break;
  281. case ShipGeneratorObjectPoolType.OldChest:
  282. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/OldChest"), new Vector3(248f + 4 * e[0], -8f + 0.118f * 4 + 4 * e[1], 0.2f), Quaternion.identity, 0);
  283. break;
  284. case ShipGeneratorObjectPoolType.BrokenLaser:
  285. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("haz/Subworlds/LaserHazard"), new Vector3(248f + 4 * (e[0] + 0.5f), 4 * (e[1]), 0.2f), Quaternion.identity, 0);
  286. break;
  287. }
  288. if (networkStuff[temp] != null)
  289. temp++;
  290. }
  291. }
  292. catch (Exception e) { Core.logger.LogError(e); }
  293. GameScript.endPortal[0] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f + 4 * 38 + 128, -6.118f, 0f), Quaternion.identity, 0);
  294. GameScript.endPortalUA[0] = GameScript.endPortal[0].transform.GetChild(0).gameObject;
  295. GameScript.endPortal[0].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  296. GameScript.endPortalUA[0].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.shipPlanet.GetID(), 0, 0 });
  297. }
  298. }
  299. }