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.

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