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.

371 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. int selectedIndex = Random.Range(0, validSections.Count);
  145. Debug.Log("<b>Validmap sections: </b>" + validSections.Count);
  146. Debug.Log("<b>Selected section: </b>" + selectedIndex);
  147. MapSection nextSection = validSections[selectedIndex];
  148. addSection(nextSection);
  149. }
  150. void addSection(MapSection section)
  151. {
  152. //Instantiate new section at x = endX
  153. Vector3 pos = new Vector3(endX, 0.0f, 0.0f);
  154. GameObject newSection = (GameObject)Instantiate(section.gameObject, pos, Quaternion.identity);
  155. MapSection newSectionScript = newSection.GetComponent<MapSection>();
  156. newSectionScript.InitSection(activeSections.Count);
  157. newSection.name = newSectionScript.name;
  158. activeSections.Add(newSectionScript);
  159. lastSection = newSectionScript;
  160. endX += newSectionScript.length;
  161. totalSections++;
  162. }
  163. bool checkSegments(MapSection second)
  164. {
  165. return checkSegments(this.lastSection, second);
  166. }
  167. bool checkSegments(MapSection first, MapSection second)
  168. {
  169. int connections = 0;
  170. //No more than one link section in a row
  171. if (first.length == 1 && second.length == 1)
  172. {
  173. return false;
  174. }
  175. if (second.difficultyMax < difficulty || second.difficultyMin > difficulty) //Check that we're in the right difficulty range for this section
  176. {
  177. return false;
  178. }
  179. if (second.widthIn < widthMin || second.widthIn > widthMax) //And that it's in the right width range
  180. {
  181. return false;
  182. }
  183. foreach (GameObject exit in first.exits)
  184. {
  185. foreach (GameObject entry in second.entrances)
  186. {
  187. if (checkConnection(exit, entry))
  188. {
  189. connections++;
  190. }
  191. }
  192. }
  193. return (connections >= minConns);
  194. }
  195. bool checkConnection(GameObject exit, GameObject entry)
  196. {
  197. //If the squares being checked don't line up, the connection is invalid
  198. if (exit.transform.localPosition.z != entry.transform.localPosition.z)
  199. {
  200. return false;
  201. }
  202. //If both components require jumping (pits or water), the connection is invalid
  203. //It's technically possible to cross two water blocks, but we don't count that
  204. if (requiresJump(exit) && requiresJump(entry))
  205. {
  206. return false;
  207. }
  208. //Since we currently don't let people jump over walls, if either block is a wall, the connection is invalid
  209. if (isWall(exit) || isWall(entry))
  210. {
  211. return false;
  212. }
  213. //If we've passed all these tests, the connection is valid!
  214. return true;
  215. }
  216. bool requiresJump(GameObject block)
  217. {
  218. if (block.GetComponent<Block>() == null) //The object must be a pit trap
  219. {
  220. return true;
  221. }
  222. return block.GetComponent<Block>().isWater; //If it's not a pit, then whether it requires jumping depends on whether it's water or not
  223. }
  224. bool isWall(GameObject block)
  225. {
  226. if (block.GetComponent<Block>() == null)
  227. {
  228. return true;
  229. }
  230. return !(block.GetComponent<Block>().is_Walkable);
  231. }
  232. //Check whether it's time to extend the track forward
  233. void checkForward()
  234. {
  235. //We check if the end of the last section of track is in sight
  236. Vector3 trackEnd = new Vector3(endX, 0.0f); //Get the middle of the end of the last track section
  237. //If it is, then we add a new section
  238. if (checkView(trackEnd))
  239. {
  240. chooseNextSection();
  241. checkForward();
  242. }
  243. }
  244. //Check whether it's time to delete the oldest section of active track
  245. void checkBack()
  246. {
  247. //We check if the end of the first section of track is still in sight
  248. Vector3 firstSectionEnd = new Vector3(startX + activeSections[0].length, 0.0f); //Get the middle of the end of the first track section
  249. //If it's not, then we remove it
  250. if (!(checkView(firstSectionEnd)))
  251. {
  252. spawn.GetComponent<blockSpawn>().updatePositions((int)startX + activeSections[0].length);
  253. startX += activeSections[0].length;
  254. spawn.GetComponent<blockSpawn>().updatePositions((int)startX);
  255. activeSections[0].destroySection();
  256. activeSections.RemoveAt(0);
  257. }
  258. }
  259. //Check whether a point is in sight or not
  260. bool checkView(Vector3 point)
  261. {
  262. Vector3 screenPoint = Camera.main.WorldToViewportPoint(point); //Map it into viewport space
  263. //The camera's field of view is represented by 0 > (x, y) < 1, with z being the distance from the camera
  264. return (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1);
  265. }
  266. //Checks in both directions for sections needing to be added or removed
  267. public void checkTrack()
  268. {
  269. checkForward();
  270. checkBack();
  271. }
  272. //Updates minimum and maximum difficulty, width, etc, based on current gamestate
  273. public void updateCriteria()
  274. {
  275. //Start with base values
  276. difficulty = diffStart;
  277. //By default, we can add a section 1 tile wider or narrower on either side than the last section
  278. widthMin = lastSection.widthOut - 2;
  279. widthMax = lastSection.widthOut + 2;
  280. /* Calculate min & max difficulties & width modifications
  281. * We recalculate from scratch each time (that is,
  282. * each time a section is added) so as to avoid having
  283. * to track which one-off increase has been applied
  284. * and which hasn't
  285. */
  286. //As the number of players shrinks, we ramp up the difficulty and contract the track
  287. if (initialPlayerCount > 0)
  288. {
  289. if (clients.ConnectedClients.Count <= (float)(0.5f * initialPlayerCount))
  290. {
  291. difficulty++;
  292. widthMin -= 2;
  293. widthMax -= 2;
  294. }
  295. if (clients.ConnectedClients.Count <= (float)(0.33f * initialPlayerCount))
  296. {
  297. difficulty += 2;
  298. widthMin -= 2;
  299. widthMax -= 2;
  300. }
  301. }
  302. //Ramp up the difficulty as the track extends
  303. difficulty += ((int)endX - (int)startX) / 7;
  304. //Apply caps
  305. if (difficulty > diffCap)
  306. {
  307. difficulty = diffCap;
  308. }
  309. if (widthMin < widthMinMin)
  310. {
  311. widthMin = widthMinMin;
  312. }
  313. if (widthMax < widthMaxMin)
  314. {
  315. widthMax = widthMaxMin;
  316. }
  317. }
  318. // Update is called once per frame
  319. void Update()
  320. {
  321. checkTrack();
  322. }
  323. }