MeteroidGenerator.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using GadgetCore.API;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using UnityEngine;
  8. using Random = System.Random;
  9. namespace Subworlds
  10. {
  11. public class MeteroidGenerator
  12. {
  13. private static readonly FieldInfo chunksField = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.Instance);
  14. private static readonly Material materialaBackLight = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/parallax.png") };
  15. private static readonly Material materialaParalexStars = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/bgStars.png") };
  16. private static readonly Material materialaParalexNone = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/bgNone.png") };
  17. public static readonly FieldInfo networkStuffField = typeof(Chunk).GetField("networkStuff", BindingFlags.NonPublic | BindingFlags.Instance);
  18. public static readonly FieldInfo tempField = typeof(Chunk).GetField("temp", BindingFlags.NonPublic | BindingFlags.Instance);
  19. private static int W = 128 + 64;
  20. private static int H = 128 - 32;
  21. private static float GetSizeForPixels(int px)
  22. {
  23. return 1f / 16f * px;
  24. }
  25. private static void MarkGeneration(int x, int y, int[][] arr)
  26. {
  27. if (x >= 0 && x < W && y >= 0 && y < H)
  28. arr[x][y] = 1;
  29. }
  30. private static bool CheckGeneration(int x, int y, int[][] arr)
  31. {
  32. if (x >= 0 && x < W && y >= 0 && y < H)
  33. if (arr[x][y] == 0)
  34. return true;
  35. else return false;
  36. return true;
  37. }
  38. public static void GenarateTown(SpawnerScript s, int[] param)
  39. {
  40. GameScript.isTown = true;
  41. if (GameScript.districtLevel % 2 != 0)
  42. {
  43. GameScript.districtLevel++;
  44. }
  45. s.backLights.SetActive(true);
  46. s.mainLight.SetActive(false);
  47. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  48. //s.mainLight.GetComponentInChildren<Light>().color = new Color(0.05f, 0.05f, 0.05f, 0.1f);
  49. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  50. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  51. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  52. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  53. var chunks = (GameObject[])chunksField.GetValue(s);
  54. var num = 0;
  55. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/MeteorBase"), new Vector3((float)(218 + 0 * 128 + 4 * 8), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  56. num++;
  57. if (Network.isServer)
  58. {
  59. chunks[num] = new GameObject("chunk");
  60. var chunk = chunks[num].AddComponent<Chunk>();
  61. PopulateTown(chunk);
  62. num++;
  63. }
  64. }
  65. public static void Genarate(SpawnerScript s, int[] param)
  66. {
  67. GameScript.isTown = false;
  68. s.backLights.SetActive(true);
  69. s.mainLight.SetActive(false);
  70. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  71. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  72. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  73. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  74. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  75. //s.backLights.SetActive(true);
  76. s.mainLight.SetActive(true);
  77. s.mainLight.GetComponentInChildren<Light>().color = new Color(1f, 1f, 1f, 1f);
  78. var chunks = (GameObject[])chunksField.GetValue(s);
  79. var num = 0;
  80. try
  81. {
  82. if (Network.isServer)
  83. {
  84. chunks[num] = new GameObject("chunk");
  85. var chunk = chunks[num].AddComponent<Chunk>();
  86. PopulateWorld(chunk);
  87. chunk.StartCoroutine(Meteors());
  88. num++;
  89. }
  90. }
  91. catch (System.Exception e) { Core.logger.LogConsole(e); }
  92. }
  93. private static List<int[]> spawnLocations = new List<int[]>();
  94. private static System.Random random = new System.Random();
  95. public static void PopulateTown(Chunk chunk)
  96. {
  97. var networkStuff = new GameObject[400];
  98. networkStuffField.SetValue(chunk, networkStuff);
  99. try
  100. {
  101. int temp = (int)tempField.GetValue(chunk);
  102. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npcStorage"), new Vector3(248f - 4 * 1, -6.118f, 0.3f), Quaternion.identity, 0);
  103. temp++;
  104. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc2103"), new Vector3(248f + 4 * 0, -6.118f, 0.3f), Quaternion.identity, 0);
  105. temp++;
  106. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc/npcQuest"), new Vector3(248f + 4 * 2, -6.118f, 0.3f), Quaternion.identity, 0);
  107. temp++;
  108. }
  109. catch (System.Exception e) { Core.logger.LogError(e); }
  110. GameScript.endPortal[0] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f + 4 * 4, -6.118f, 0f), Quaternion.identity, 0);
  111. GameScript.endPortalUA[0] = GameScript.endPortal[0].transform.GetChild(0).gameObject;
  112. GameScript.endPortal[0].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  113. GameScript.endPortalUA[0].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.lastBiome == -1 ? Core.meteroidPlanet.GetID() : Core.lastBiome, 0, 0 });
  114. GameScript.endPortal[3] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f - 4 * 3, -6.118f, 0f), Quaternion.identity, 0);
  115. GameScript.endPortalUA[3] = GameScript.endPortal[3].transform.GetChild(0).gameObject;
  116. GameScript.endPortal[3].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  117. GameScript.endPortalUA[3].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 98, 0, 3 });
  118. }
  119. public static void PopulateWorld(Chunk chunk)
  120. {
  121. var networkStuff = new GameObject[400];
  122. networkStuffField.SetValue(chunk, networkStuff);
  123. int temp = (int)tempField.GetValue(chunk);
  124. int[][] genSpots = new int[W][];
  125. for (int i = 0; i < W; i++)
  126. genSpots[i] = new int[H];
  127. {
  128. int x = 15;
  129. int y = 10;
  130. for (int xOff = -2; xOff < 3; xOff++)
  131. for (int yOff = -2; yOff < 6; yOff++)
  132. MarkGeneration(x + xOff, y + yOff, genSpots);
  133. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  134. temp++;
  135. }
  136. {
  137. int porterGenerated = 0;
  138. int overflow = 0;
  139. int x = random.Next(W / 2) + W / 2;
  140. int y = random.Next(H / 2) + H / 2;
  141. bool addLoopPortal = false;
  142. foreach (var gadget in GadgetCore.API.Gadgets.ListAllEnabledGadgets())
  143. if (gadget.Info?.ModName == "Loop Portal")
  144. {
  145. addLoopPortal = true;
  146. break;
  147. }
  148. while (overflow < 1000 && porterGenerated < 1)
  149. {
  150. overflow++;
  151. bool noGen = false;
  152. for (int xOff = -1; xOff < 2; xOff++)
  153. for (int yOff = -1; yOff < 4; yOff++)
  154. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  155. noGen = true;
  156. if (!noGen)
  157. {
  158. for (int xOff = -1; xOff < 2; xOff++)
  159. for (int yOff = -1; yOff < 4; yOff++)
  160. MarkGeneration(x + xOff, y + yOff, genSpots);
  161. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  162. temp++;
  163. GameScript.endPortal[porterGenerated] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.451f + 1, 0f), Quaternion.identity, 0);
  164. GameScript.endPortalUA[porterGenerated] = GameScript.endPortal[porterGenerated].transform.GetChild(0).gameObject;
  165. GameScript.endPortal[porterGenerated].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  166. GameScript.endPortalUA[porterGenerated].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { SpawnerScript.curBiome, 0, porterGenerated });
  167. porterGenerated++;
  168. }
  169. x = Mathf.Min(x + random.Next(5) - 2, W);
  170. y = Mathf.Min(y + random.Next(5) - 2, H);
  171. }
  172. //while (addLoopPortal && overflow < 5000)
  173. //{
  174. // overflow++;
  175. // bool noGen = false;
  176. // for (int xOff = -1; xOff < 2; xOff++)
  177. // for (int yOff = -1; yOff < 4; yOff++)
  178. // if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  179. // noGen = true;
  180. // if (!noGen)
  181. // {
  182. // for (int xOff = -1; xOff < 2; xOff++)
  183. // for (int yOff = -1; yOff < 4; yOff++)
  184. // MarkGeneration(x + xOff, y + yOff, genSpots);
  185. // networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  186. // temp++;
  187. // var i = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.451f + 1, 0f), Quaternion.identity, 0);
  188. // var iUA = i.transform.GetChild(0).gameObject;
  189. // i.GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  190. // iUA.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { SpawnerScript.curBiome, 0, 4 });
  191. // i.transform.parent = GameScript.endPortal[0].transform;
  192. // networkStuff[temp] = i;
  193. // temp++;
  194. // break;
  195. // }
  196. // x = Mathf.Min(x + random.Next(5) - 2, W);
  197. // y = Mathf.Min(y + random.Next(5) - 2, H);
  198. //}
  199. }
  200. {
  201. int r = 0;
  202. int overflow = 0;
  203. while (overflow < 15000 && r < 700)
  204. {
  205. overflow++;
  206. int i = random.Next(6);
  207. int x = random.Next(W);
  208. int y = random.Next(H);
  209. bool noGen = false;
  210. switch (i)
  211. {
  212. case 0:
  213. case 1:
  214. case 2:
  215. for (int xOff = -1; xOff < 2; xOff++)
  216. for (int yOff = -1; yOff < 2; yOff++)
  217. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  218. noGen = true;
  219. break;
  220. case 3:
  221. case 4:
  222. for (int xOff = -1; xOff < 2; xOff++)
  223. for (int yOff = -1; yOff < 2; yOff++)
  224. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  225. noGen = true;
  226. break;
  227. case 5:
  228. for (int xOff = -2; xOff < 3; xOff++)
  229. for (int yOff = -2; yOff < 3; yOff++)
  230. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  231. noGen = true;
  232. break;
  233. }
  234. if (!noGen)
  235. {
  236. r++;
  237. switch (i)
  238. {
  239. case 0:
  240. case 1:
  241. case 2:
  242. for (int xOff = -1; xOff < 2; xOff++)
  243. for (int yOff = -1; yOff < 2; yOff++)
  244. MarkGeneration(x + xOff, y + yOff, genSpots);
  245. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  246. temp++;
  247. var position = new Vector3((float)(242 + x * 4 - 60), y * 4 - 60 + GetSizeForPixels(54) + 1.049f, 0f);
  248. if (random.Next(2) == 0)
  249. {
  250. Network.Instantiate(Resources.Load("e/Subworlds/Jelly"), position, Quaternion.identity, 0);
  251. }
  252. else if (random.Next(2) == 0)
  253. {
  254. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/SpaceOre1"), position, Quaternion.identity, 0);
  255. temp++;
  256. }
  257. else
  258. {
  259. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/SpaceOre2"), position, Quaternion.identity, 0);
  260. temp++;
  261. }
  262. break;
  263. case 3:
  264. case 4:
  265. for (int xOff = -1; xOff < 2; xOff++)
  266. for (int yOff = -1; yOff < 2; yOff++)
  267. MarkGeneration(x + xOff, y + yOff, genSpots);
  268. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockSmall"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  269. temp++;
  270. break;
  271. case 5:
  272. for (int xOff = -2; xOff < 3; xOff++)
  273. for (int yOff = -2; yOff < 3; yOff++)
  274. MarkGeneration(x + xOff, y + yOff, genSpots);
  275. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockLarge"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  276. temp++;
  277. break;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. private static IEnumerator Meteors()
  284. {
  285. yield return new WaitForSeconds((float)UnityEngine.Random.Range(1, 5));
  286. if (Network.isServer && MenuScript.player != null)
  287. {
  288. while (true)
  289. {
  290. if (GameScript.isTown || !GameScript.inInstance)
  291. break;
  292. Vector3 spawnPos = new Vector3(MenuScript.player.transform.position.x + (float)UnityEngine.Random.Range(-20, 70), MenuScript.player.transform.position.y + 40f, 0f);
  293. GameObject p;
  294. if (UnityEngine.Random.Range(0, 3) == 0)
  295. p = (GameObject)Network.Instantiate(Resources.Load("proj/meteor"), spawnPos, Quaternion.identity, 0);
  296. else
  297. p = (GameObject)Network.Instantiate(Resources.Load("proj/meteor2"), spawnPos, Quaternion.identity, 0);
  298. Vector3 target = new Vector3(MenuScript.player.transform.position.x + (float)UnityEngine.Random.Range(-20, 70), MenuScript.player.transform.position.y, 0f);
  299. p.SendMessage("EnemySet", target, SendMessageOptions.DontRequireReceiver);
  300. yield return new WaitForSeconds(1f);
  301. }
  302. }
  303. yield break;
  304. }
  305. }
  306. }