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.

187 lines
5.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Map : MonoBehaviour {
  5. public GameObject LocalPlayer;
  6. public GameObject[] Tiles;
  7. public List<GameObject> Landmarks;
  8. public GameObject[] MapGrid;
  9. public GameObject[] WorldWrapGrid;
  10. //Minimum and maximum values for the map
  11. public float minX;
  12. public float maxX;
  13. public float minZ;
  14. public float maxZ;
  15. /*
  16. public Camera TopCam;
  17. public Camera BotCam;
  18. public Camera LeftCam;
  19. public Camera RightCam;
  20. */
  21. #region StartupFunctions
  22. private void Start()
  23. {
  24. //GenerateMap();
  25. CreateDummys();
  26. }
  27. //Create dummy players for the world wrapping
  28. void CreateDummys()
  29. {
  30. List<GameObject> Players = new List<GameObject>();
  31. foreach (GameObject Player in Players)
  32. {
  33. Transform PT = Player.transform;
  34. Transform model = PT.Find("Model");
  35. Instantiate(model, new Vector3(PT.position.x + minX, PT.position.y, PT.position.z), transform.rotation, PT);
  36. Instantiate(model, new Vector3(PT.position.x + maxX, PT.position.y, PT.position.z), transform.rotation, PT);
  37. Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + minZ), transform.rotation, PT);
  38. Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + maxZ), transform.rotation, PT);
  39. }
  40. }
  41. void GenerateMap()
  42. {
  43. GameObject[] gameTiles = new GameObject[4];
  44. for (int i = 0; i < 4; i++)
  45. {
  46. int rand = Random.Range(0, Tiles.Length);
  47. GameObject tile = Instantiate(Tiles[rand], MapGrid[i].transform.position, Quaternion.identity);
  48. PopulateTile(tile.GetComponent<Tile>());
  49. gameTiles[i] = tile;
  50. }
  51. //Create the fake tiles for the world wrapping (This is done super poorly and quickly, don't judge me :)
  52. Instantiate(gameTiles[0], WorldWrapGrid[6].transform);
  53. Instantiate(gameTiles[0], WorldWrapGrid[10].transform);
  54. Instantiate(gameTiles[0], WorldWrapGrid[12].transform);
  55. Instantiate(gameTiles[1], WorldWrapGrid[5].transform);
  56. Instantiate(gameTiles[1], WorldWrapGrid[9].transform);
  57. Instantiate(gameTiles[1], WorldWrapGrid[11].transform);
  58. Instantiate(gameTiles[2], WorldWrapGrid[2].transform);
  59. Instantiate(gameTiles[2], WorldWrapGrid[4].transform);
  60. Instantiate(gameTiles[2], WorldWrapGrid[8].transform);
  61. Instantiate(gameTiles[3], WorldWrapGrid[1].transform);
  62. Instantiate(gameTiles[3], WorldWrapGrid[3].transform);
  63. Instantiate(gameTiles[3], WorldWrapGrid[7].transform);
  64. }
  65. //Populate the tile with landmarks
  66. void PopulateTile(Tile tile)
  67. {
  68. foreach(GameObject LP in tile.LandmarkPoints)
  69. {
  70. if (Landmarks.Count > 0)
  71. {
  72. int rand = Random.Range(0, Landmarks.Count);
  73. Instantiate(Landmarks[rand], LP.transform.position, Quaternion.identity, tile.transform.parent);
  74. }
  75. }
  76. }
  77. #endregion
  78. void Update()
  79. {
  80. Teleport();
  81. }
  82. void Teleport()
  83. {
  84. Transform PT = LocalPlayer.transform;
  85. if (PT.position.x > maxX)
  86. {
  87. PT.position = new Vector3(minX, PT.position.y, PT.position.z);
  88. }
  89. else if (PT.position.x < minX)
  90. {
  91. PT.position = new Vector3(maxX, PT.position.y, PT.position.z);
  92. }
  93. if (PT.position.z > maxZ)
  94. {
  95. PT.position = new Vector3(PT.position.x, PT.position.y, minZ);
  96. }
  97. else if (PT.position.z < minZ)
  98. {
  99. PT.position = new Vector3(PT.position.x, PT.position.y, maxZ);
  100. }
  101. }
  102. }
  103. //EXTRA STUFF, PLEASE IGNORE
  104. /*
  105. * //IN UPDATE
  106. HandleCameraMovement(TopCam, true);
  107. HandleCameraMovement(BotCam, true);
  108. HandleCameraMovement(LeftCam, false);
  109. HandleCameraMovement(RightCam, false);
  110. HandleCamSizing();
  111. */
  112. /*
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. /// <param name="Cam"> The camera to move</param>
  117. /// <param name="Xmove">Do we move in the X axis or the Z?</param>
  118. void HandleCameraMovement(Camera Cam, bool Xmove)
  119. {
  120. if (Xmove == true)
  121. {
  122. Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
  123. }
  124. else
  125. {
  126. Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
  127. }
  128. }
  129. void HandleCamSizing()
  130. {
  131. Transform PT = LocalPlayer.transform;
  132. if (PT.position.x > maxX - camWidth)
  133. {
  134. float dif = ((maxX - PT.position.x) / camWidth) / 2;
  135. SetCam(dif, 0, RightCam);
  136. }
  137. else if (PT.position.x < minX + camWidth)
  138. {
  139. float dif = ((minX - PT.position.x) / camWidth) / 2;
  140. SetCam(dif, 0, LeftCam);
  141. }
  142. else
  143. {
  144. SetCam(1, 1, RightCam);
  145. SetCam(1, 1, LeftCam);
  146. }
  147. if (PT.position.z > maxZ - camHeight)
  148. {
  149. float dif = ((maxZ - PT.position.z) / camHeight) / 2;
  150. SetCam(0, dif, TopCam);
  151. }
  152. else if (PT.position.x < + camHeight)
  153. {
  154. float dif = ((minZ - PT.position.z) / camHeight) / 2;
  155. SetCam(0, dif, BotCam);
  156. }
  157. else
  158. {
  159. SetCam(1, 1, RightCam);
  160. SetCam(1, 1, LeftCam);
  161. }
  162. }
  163. void SetCam(float x, float y, Camera cam)
  164. {
  165. cam.rect = new Rect(x, y, 1, 1);
  166. }
  167. */