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.

282 lines
9.2 KiB

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