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.

280 lines
9.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Map : MonoBehaviour
  5. {
  6. GameObject LocalPlayer;
  7. List<GameObject> Players = new List<GameObject>();
  8. public GameObject[] Tiles;
  9. public List<GameObject> Landmarks;
  10. public int TileSize = 100;
  11. public Vector2 MapDimensions;
  12. //Minimum and maximum values for the map
  13. public float minX;
  14. public float maxX;
  15. public float minZ;
  16. public float maxZ;
  17. #region StartupFunctions
  18. private void Start()
  19. {
  20. LocalPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;
  21. Players.Add(LocalPlayer);
  22. foreach (GameObject curPlayer in Multiplayer.PlayersManager.Instance.RemotePlayers.Values)
  23. {
  24. Players.Add(curPlayer);
  25. }
  26. GenerateTiles();
  27. CreateDummys();
  28. }
  29. //Create dummy players for the world wrapping
  30. void CreateDummys()
  31. {
  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. public void GenerateTiles()
  44. {
  45. float HalfSize = TileSize / 2;
  46. Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));
  47. for (int x = 0; x < MapDimensions.x; x++)
  48. {
  49. for (int z = 0; z < MapDimensions.y; z++)
  50. {
  51. int randIndex = Random.Range(0, Tiles.Length);
  52. float RandomRotation = Random.Range(0, 4) * 90;
  53. Vector3 position = startPoint + new Vector3(TileSize * x + HalfSize, 0.0f, TileSize * z + HalfSize);
  54. Debug.Log(position);
  55. GameObject Tile = Instantiate(Tiles[randIndex]);
  56. Tile.transform.position = position;
  57. Tile.transform.Rotate(Vector3.up, RandomRotation);
  58. Tile.name = "Tile [" + x + "," + z + "]";
  59. if (x == 0)
  60. {
  61. Vector3 newPos = position;
  62. newPos.x += TileSize * MapDimensions.x;
  63. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  64. MirrorTile.transform.position = newPos;
  65. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  66. MirrorTile.name = Tile.name + "(Mirror)";
  67. }
  68. if (z == 0)
  69. {
  70. Vector3 newPos = position;
  71. newPos.z += TileSize * MapDimensions.y;
  72. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  73. MirrorTile.transform.position = newPos;
  74. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  75. MirrorTile.name = Tile.name + "(Mirror)";
  76. }
  77. if (x == 0 && z == 0)
  78. {
  79. Vector3 newPos = position;
  80. newPos.x += TileSize * MapDimensions.x;
  81. newPos.z += TileSize * MapDimensions.y;
  82. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  83. MirrorTile.transform.position = newPos;
  84. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  85. MirrorTile.name = Tile.name + "(Mirror)";
  86. }
  87. if (x == MapDimensions.x - 1)
  88. {
  89. Vector3 newPos = position;
  90. newPos.x -= TileSize * MapDimensions.x;
  91. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  92. MirrorTile.transform.position = newPos;
  93. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  94. MirrorTile.name = Tile.name + "(Mirror)";
  95. }
  96. if (z == MapDimensions.y - 1)
  97. {
  98. Vector3 newPos = position;
  99. newPos.z -= TileSize * MapDimensions.y;
  100. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  101. MirrorTile.transform.position = newPos;
  102. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  103. MirrorTile.name = Tile.name + "(Mirror)";
  104. }
  105. if (x == MapDimensions.x - 1 && z == MapDimensions.y - 1)
  106. {
  107. Vector3 newPos = position;
  108. newPos.x -= TileSize * MapDimensions.x;
  109. newPos.z -= TileSize * MapDimensions.y;
  110. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  111. MirrorTile.transform.position = newPos;
  112. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  113. MirrorTile.name = Tile.name + "(Mirror)";
  114. }
  115. if (x == 0 && z == MapDimensions.y - 1)
  116. {
  117. Vector3 newPos = position;
  118. newPos.x += TileSize * MapDimensions.x;
  119. newPos.z -= TileSize * MapDimensions.y;
  120. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  121. MirrorTile.transform.position = newPos;
  122. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  123. MirrorTile.name = Tile.name + "(Mirror)";
  124. }
  125. if (x == MapDimensions.x - 1 && z == 0)
  126. {
  127. Vector3 newPos = position;
  128. newPos.x -= TileSize * MapDimensions.x;
  129. newPos.z += TileSize * MapDimensions.y;
  130. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  131. MirrorTile.transform.position = newPos;
  132. MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
  133. MirrorTile.name = Tile.name + "(Mirror)";
  134. }
  135. }
  136. }
  137. }
  138. //Populate the tile with landmarks
  139. void PopulateTile(Tile tile)
  140. {
  141. foreach (GameObject LP in tile.LandmarkPoints)
  142. {
  143. if (Landmarks.Count > 0)
  144. {
  145. int rand = Random.Range(0, Landmarks.Count);
  146. Instantiate(Landmarks[rand], LP.transform.position, Quaternion.identity, tile.transform.parent);
  147. }
  148. }
  149. }
  150. #endregion
  151. void Update()
  152. {
  153. Teleport();
  154. }
  155. void Teleport()
  156. {
  157. Transform PT = LocalPlayer.transform;
  158. if (PT.position.x > maxX)
  159. {
  160. PT.position = new Vector3(minX, PT.position.y, PT.position.z);
  161. }
  162. else if (PT.position.x < minX)
  163. {
  164. PT.position = new Vector3(maxX, PT.position.y, PT.position.z);
  165. }
  166. if (PT.position.z > maxZ)
  167. {
  168. PT.position = new Vector3(PT.position.x, PT.position.y, minZ);
  169. }
  170. else if (PT.position.z < minZ)
  171. {
  172. PT.position = new Vector3(PT.position.x, PT.position.y, maxZ);
  173. }
  174. }
  175. }
  176. //EXTRA STUFF, PLEASE IGNORE
  177. /*
  178. * //IN UPDATE
  179. HandleCameraMovement(TopCam, true);
  180. HandleCameraMovement(BotCam, true);
  181. HandleCameraMovement(LeftCam, false);
  182. HandleCameraMovement(RightCam, false);
  183. HandleCamSizing();
  184. */
  185. /*
  186. /// <summary>
  187. ///
  188. /// </summary>
  189. /// <param name="Cam"> The camera to move</param>
  190. /// <param name="Xmove">Do we move in the X axis or the Z?</param>
  191. void HandleCameraMovement(Camera Cam, bool Xmove)
  192. {
  193. if (Xmove == true)
  194. {
  195. Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
  196. }
  197. else
  198. {
  199. Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
  200. }
  201. }
  202. void HandleCamSizing()
  203. {
  204. Transform PT = LocalPlayer.transform;
  205. if (PT.position.x > maxX - camWidth)
  206. {
  207. float dif = ((maxX - PT.position.x) / camWidth) / 2;
  208. SetCam(dif, 0, RightCam);
  209. }
  210. else if (PT.position.x < minX + camWidth)
  211. {
  212. float dif = ((minX - PT.position.x) / camWidth) / 2;
  213. SetCam(dif, 0, LeftCam);
  214. }
  215. else
  216. {
  217. SetCam(1, 1, RightCam);
  218. SetCam(1, 1, LeftCam);
  219. }
  220. if (PT.position.z > maxZ - camHeight)
  221. {
  222. float dif = ((maxZ - PT.position.z) / camHeight) / 2;
  223. SetCam(0, dif, TopCam);
  224. }
  225. else if (PT.position.x < + camHeight)
  226. {
  227. float dif = ((minZ - PT.position.z) / camHeight) / 2;
  228. SetCam(0, dif, BotCam);
  229. }
  230. else
  231. {
  232. SetCam(1, 1, RightCam);
  233. SetCam(1, 1, LeftCam);
  234. }
  235. }
  236. void SetCam(float x, float y, Camera cam)
  237. {
  238. cam.rect = new Rect(x, y, 1, 1);
  239. }
  240. */