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.

292 lines
9.7 KiB

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