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.

190 lines
6.1 KiB

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