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.

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