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.

197 lines
6.3 KiB

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