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.

253 lines
8.3 KiB

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