You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
12 KiB

  1. using Networking.Server;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [CreateAssetMenu(menuName = "Major Project/Map Generation/Map Manager")]
  5. public class MapManager : ScriptableObject
  6. {
  7. public ClientList clients;
  8. public GameObject spawn;
  9. public GameObject spawn4; //The section to use as a spawn-point for games with 2-5 players
  10. public GameObject spawn8; //The section to use a spawn-point for games with 5-8 players
  11. //In 5-player games, we choose between them at random
  12. public List<MapSection> sections; //The list of sections to choose from after starting
  13. //Split up the inspector lists to make them easier to manage. They'll be combined on initialisation
  14. public List<SectionList> sectionLists;
  15. public int minConns = 2; //The minimum number of valid connections between two map sections for them to be allowed to link up
  16. public List<MapSection> activeSections; //The list of sections that have been placed on the map (and not removed)
  17. MapSection lastSection; //Which map-section was most recently added?
  18. public float startX; //The x-position of the current start of the track
  19. float startXinit = -16.0f;
  20. float endX; //The x-position of the current end of the track
  21. int totalSections; //How many sections have been added? Including ones that have been deleted
  22. int initialPlayerCount;
  23. int diffStart = 0; //Initial difficulty rating
  24. int diffCap = 5; //The highest the difficulty rating can go
  25. int difficulty; //Current difficulty rating
  26. int widthMin; //The minimum widthIn that we want for a new map section
  27. int widthMax; //The maximum widthIn that we want for a new map section
  28. int widthMinMin = 3; //The minimum to which widthMin can be reduced
  29. int widthMaxMin = 5; //The minimum to which widthMax can be reduced
  30. public void init()
  31. {
  32. sections = new List<MapSection>();
  33. startX = startXinit;
  34. endX = startX;
  35. totalSections = 0;
  36. foreach (SectionList sectionList in sectionLists)
  37. {
  38. foreach (MapSection section in sectionList.sectionList)
  39. {
  40. sections.Add(section);
  41. }
  42. }
  43. initialPlayerCount = clients.ConnectedClients.Count;
  44. activeSections = new List<MapSection>();
  45. if (initialPlayerCount < 5)
  46. {
  47. addSection(spawn4.GetComponent<MapSection>());
  48. }
  49. else if (initialPlayerCount > 5)
  50. {
  51. addSection(spawn8.GetComponent<MapSection>());
  52. }
  53. else
  54. {
  55. if (Random.Range(0.0f, 1.0f) < 0.5f)
  56. {
  57. addSection(spawn4.GetComponent<MapSection>());
  58. }
  59. else
  60. {
  61. addSection(spawn8.GetComponent<MapSection>());
  62. }
  63. }
  64. widthMin = activeSections[0].widthOut - 2;
  65. widthMax = activeSections[0].widthOut + 2;
  66. difficulty = diffStart;
  67. switch (initialPlayerCount)
  68. {
  69. case 2:
  70. foreach (GameObject spawnBlock in lastSection.spawns2)
  71. {
  72. spawnBlock.GetComponent<Block>().isSpawnable = true;
  73. }
  74. break;
  75. case 3:
  76. foreach (GameObject spawnBlock in lastSection.spawns3)
  77. {
  78. spawnBlock.GetComponent<Block>().isSpawnable = true;
  79. }
  80. break;
  81. case 4:
  82. foreach (GameObject spawnBlock in lastSection.spawns4)
  83. {
  84. spawnBlock.GetComponent<Block>().isSpawnable = true;
  85. }
  86. break;
  87. case 5:
  88. foreach (GameObject spawnBlock in lastSection.spawns5)
  89. {
  90. spawnBlock.GetComponent<Block>().isSpawnable = true;
  91. }
  92. break;
  93. case 6:
  94. foreach (GameObject spawnBlock in lastSection.spawns6)
  95. {
  96. spawnBlock.GetComponent<Block>().isSpawnable = true;
  97. }
  98. break;
  99. case 7:
  100. foreach (GameObject spawnBlock in lastSection.spawns7)
  101. {
  102. spawnBlock.GetComponent<Block>().isSpawnable = true;
  103. }
  104. break;
  105. case 8:
  106. foreach (GameObject spawnBlock in lastSection.spawns8)
  107. {
  108. spawnBlock.GetComponent<Block>().isSpawnable = true;
  109. }
  110. break;
  111. default:
  112. foreach (GameObject spawnBlock in lastSection.spawns1)
  113. {
  114. spawnBlock.GetComponent<Block>().isSpawnable = true;
  115. }
  116. break;
  117. }
  118. checkForward();
  119. }
  120. void chooseNextSection()
  121. {
  122. //First, we determine which sections are valid
  123. List<MapSection> validSections = new List<MapSection>();
  124. updateCriteria(); //We update the section selection criteria for the current gamestate
  125. int count = -1;
  126. foreach (MapSection section in sections)
  127. {
  128. count++;
  129. if (section == null)
  130. {
  131. continue;
  132. }
  133. if (section.weight > 0 && checkSegments(section))
  134. {
  135. //If a segment is a valid continuation of the current most-recent segment, add it to the list
  136. //Sections with higher weights get more entries => higher chance of being picked
  137. for (int i = 0; i < section.weight; i++)
  138. {
  139. validSections.Add(section);
  140. }
  141. }
  142. }
  143. //Having generated our list, we choose a random segment from it
  144. MapSection nextSection = validSections[(int)Random.Range(0.0f, (float)validSections.Count)];
  145. addSection(nextSection);
  146. }
  147. void addSection(MapSection section)
  148. {
  149. //Instantiate new section at x = endX
  150. Vector3 pos = new Vector3(endX, 0.0f, 0.0f);
  151. GameObject newSection = (GameObject)Instantiate(section.gameObject, pos, Quaternion.identity);
  152. MapSection newSectionScript = newSection.GetComponent<MapSection>();
  153. newSectionScript.InitSection(activeSections.Count);
  154. newSection.name = newSectionScript.name;
  155. activeSections.Add(newSectionScript);
  156. lastSection = newSectionScript;
  157. endX += newSectionScript.length;
  158. totalSections++;
  159. }
  160. bool checkSegments(MapSection second)
  161. {
  162. return checkSegments(this.lastSection, second);
  163. }
  164. bool checkSegments(MapSection first, MapSection second)
  165. {
  166. int connections = 0;
  167. //No more than one link section in a row
  168. if (first.length == 1 && second.length == 1)
  169. {
  170. return false;
  171. }
  172. if (second.difficultyMax < difficulty || second.difficultyMin > difficulty) //Check that we're in the right difficulty range for this section
  173. {
  174. return false;
  175. }
  176. if (second.widthIn < widthMin || second.widthIn > widthMax) //And that it's in the right width range
  177. {
  178. return false;
  179. }
  180. foreach (GameObject exit in first.exits)
  181. {
  182. foreach (GameObject entry in second.entrances)
  183. {
  184. if (checkConnection(exit, entry))
  185. {
  186. connections++;
  187. }
  188. }
  189. }
  190. return (connections >= minConns);
  191. }
  192. bool checkConnection(GameObject exit, GameObject entry)
  193. {
  194. //If the squares being checked don't line up, the connection is invalid
  195. if (exit.transform.localPosition.z != entry.transform.localPosition.z)
  196. {
  197. return false;
  198. }
  199. //If both components require jumping (pits or water), the connection is invalid
  200. //It's technically possible to cross two water blocks, but we don't count that
  201. if (requiresJump(exit) && requiresJump(entry))
  202. {
  203. return false;
  204. }
  205. //Since we currently don't let people jump over walls, if either block is a wall, the connection is invalid
  206. if (isWall(exit) || isWall(entry))
  207. {
  208. return false;
  209. }
  210. //If we've passed all these tests, the connection is valid!
  211. return true;
  212. }
  213. bool requiresJump(GameObject block)
  214. {
  215. if (block.GetComponent<Block>() == null) //The object must be a pit trap
  216. {
  217. return true;
  218. }
  219. return block.GetComponent<Block>().isWater; //If it's not a pit, then whether it requires jumping depends on whether it's water or not
  220. }
  221. bool isWall(GameObject block)
  222. {
  223. if (block.GetComponent<Block>() == null)
  224. {
  225. return true;
  226. }
  227. return !(block.GetComponent<Block>().is_Walkable);
  228. }
  229. //Check whether it's time to extend the track forward
  230. void checkForward()
  231. {
  232. //We check if the end of the last section of track is in sight
  233. Vector3 trackEnd = new Vector3(endX, 0.0f); //Get the middle of the end of the last track section
  234. //If it is, then we add a new section
  235. if (checkView(trackEnd))
  236. {
  237. chooseNextSection();
  238. checkForward();
  239. }
  240. }
  241. //Check whether it's time to delete the oldest section of active track
  242. void checkBack()
  243. {
  244. //We check if the end of the first section of track is still in sight
  245. Vector3 firstSectionEnd = new Vector3(startX + activeSections[0].length, 0.0f); //Get the middle of the end of the first track section
  246. //If it's not, then we remove it
  247. if (!(checkView(firstSectionEnd)))
  248. {
  249. spawn.GetComponent<blockSpawn>().updatePositions((int)startX + activeSections[0].length);
  250. startX += activeSections[0].length;
  251. spawn.GetComponent<blockSpawn>().updatePositions((int)startX);
  252. activeSections[0].destroySection();
  253. activeSections.RemoveAt(0);
  254. }
  255. }
  256. //Check whether a point is in sight or not
  257. bool checkView(Vector3 point)
  258. {
  259. Vector3 screenPoint = Camera.main.WorldToViewportPoint(point); //Map it into viewport space
  260. //The camera's field of view is represented by 0 > (x, y) < 1, with z being the distance from the camera
  261. return (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1);
  262. }
  263. //Checks in both directions for sections needing to be added or removed
  264. public void checkTrack()
  265. {
  266. checkForward();
  267. checkBack();
  268. }
  269. //Updates minimum and maximum difficulty, width, etc, based on current gamestate
  270. public void updateCriteria()
  271. {
  272. //Start with base values
  273. difficulty = diffStart;
  274. //By default, we can add a section 1 tile wider or narrower on either side than the last section
  275. widthMin = lastSection.widthOut - 2;
  276. widthMax = lastSection.widthOut + 2;
  277. /* Calculate min & max difficulties & width modifications
  278. * We recalculate from scratch each time (that is,
  279. * each time a section is added) so as to avoid having
  280. * to track which one-off increase has been applied
  281. * and which hasn't
  282. */
  283. //As the number of players shrinks, we ramp up the difficulty and contract the track
  284. if (initialPlayerCount > 0)
  285. {
  286. if (clients.ConnectedClients.Count <= (float)(0.5f * initialPlayerCount))
  287. {
  288. difficulty++;
  289. widthMin -= 2;
  290. widthMax -= 2;
  291. }
  292. if (clients.ConnectedClients.Count <= (float)(0.33f * initialPlayerCount))
  293. {
  294. difficulty += 2;
  295. widthMin -= 2;
  296. widthMax -= 2;
  297. }
  298. }
  299. //Ramp up the difficulty as the track extends
  300. difficulty += ((int)endX - (int)startX) / 7;
  301. //Apply caps
  302. if (difficulty > diffCap)
  303. {
  304. difficulty = diffCap;
  305. }
  306. if (widthMin < widthMinMin)
  307. {
  308. widthMin = widthMinMin;
  309. }
  310. if (widthMax < widthMaxMin)
  311. {
  312. widthMax = widthMaxMin;
  313. }
  314. }
  315. // Update is called once per frame
  316. void Update()
  317. {
  318. checkTrack();
  319. }
  320. }