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.

262 lines
8.6 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 { get { return (-MapDimensions.x * (TileSize / 2)); } }
  14. public float maxX { get { return (MapDimensions.x * (TileSize / 2)); } }
  15. public float minZ { get { return (-MapDimensions.y * (TileSize / 2)); } }
  16. public float maxZ { get { return (MapDimensions.y * (TileSize / 2)); } }
  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. Random.State state = Random.state;
  29. Random.InitState((int)System.DateTime.Now.Ticks);
  30. LocalPlayer.transform.position = new Vector3(Random.Range(minX, maxX), 5, Random.Range(minX, maxX));
  31. Random.state = state;
  32. }
  33. //Create dummy players for the world wrapping
  34. void CreateDummys()
  35. {
  36. foreach (GameObject Player in Players)
  37. {
  38. Player PS = Player.GetComponent<Player>();
  39. PS.CreateDummies(this);
  40. }
  41. }
  42. public void GenerateTiles()
  43. {
  44. float HalfSize = TileSize / 2;
  45. Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));
  46. GameObject Map = new GameObject("Map Tiles");
  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],Map.transform);
  56. Tile.transform.position = position;
  57. Tile.transform.Rotate(Vector3.up, RandomRotation);
  58. Tile.name = "Tile [" + x + "," + z + "]";
  59. PopulateTile(Tile.GetComponent<Tile>());
  60. if (x == 0)
  61. {
  62. Vector3 newPos = position;
  63. newPos.x += TileSize * MapDimensions.x;
  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 (z == 0)
  70. {
  71. Vector3 newPos = position;
  72. newPos.z += TileSize * MapDimensions.y;
  73. GameObject MirrorTile = Instantiate(Tile, Map.transform);
  74. MirrorTile.transform.position = newPos;
  75. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  84. MirrorTile.transform.position = newPos;
  85. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  93. MirrorTile.transform.position = newPos;
  94. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  102. MirrorTile.transform.position = newPos;
  103. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  112. MirrorTile.transform.position = newPos;
  113. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  122. MirrorTile.transform.position = newPos;
  123. MirrorTile.transform.rotation = Tile.transform.rotation;
  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(Tile, Map.transform);
  132. MirrorTile.transform.position = newPos;
  133. MirrorTile.transform.rotation = Tile.transform.rotation;
  134. MirrorTile.name = Tile.name + "(Mirror)";
  135. }
  136. }
  137. }
  138. StaticBatchingUtility.Combine(Map);
  139. }
  140. //Populate the tile with landmarks
  141. void PopulateTile(Tile tile)
  142. {
  143. foreach (Transform LP in tile.LandMarkLocations)
  144. {
  145. if (Landmarks.Count > 0)
  146. {
  147. int rand = Random.Range(0, Landmarks.Count);
  148. Instantiate(Landmarks[rand], LP.transform.position, Landmarks[rand].transform.rotation, tile.transform);
  149. Landmarks.Remove(Landmarks[rand]);
  150. }
  151. }
  152. }
  153. #endregion
  154. }
  155. //EXTRA STUFF, PLEASE IGNORE
  156. /*
  157. * //IN UPDATE
  158. HandleCameraMovement(TopCam, true);
  159. HandleCameraMovement(BotCam, true);
  160. HandleCameraMovement(LeftCam, false);
  161. HandleCameraMovement(RightCam, false);
  162. HandleCamSizing();
  163. */
  164. /*
  165. /// <summary>
  166. ///
  167. /// </summary>
  168. /// <param name="Cam"> The camera to move</param>
  169. /// <param name="Xmove">Do we move in the X axis or the Z?</param>
  170. void HandleCameraMovement(Camera Cam, bool Xmove)
  171. {
  172. if (Xmove == true)
  173. {
  174. Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
  175. }
  176. else
  177. {
  178. Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
  179. }
  180. }
  181. void HandleCamSizing()
  182. {
  183. Transform PT = LocalPlayer.transform;
  184. if (PT.position.x > maxX - camWidth)
  185. {
  186. float dif = ((maxX - PT.position.x) / camWidth) / 2;
  187. SetCam(dif, 0, RightCam);
  188. }
  189. else if (PT.position.x < minX + camWidth)
  190. {
  191. float dif = ((minX - PT.position.x) / camWidth) / 2;
  192. SetCam(dif, 0, LeftCam);
  193. }
  194. else
  195. {
  196. SetCam(1, 1, RightCam);
  197. SetCam(1, 1, LeftCam);
  198. }
  199. if (PT.position.z > maxZ - camHeight)
  200. {
  201. float dif = ((maxZ - PT.position.z) / camHeight) / 2;
  202. SetCam(0, dif, TopCam);
  203. }
  204. else if (PT.position.x < + camHeight)
  205. {
  206. float dif = ((minZ - PT.position.z) / camHeight) / 2;
  207. SetCam(0, dif, BotCam);
  208. }
  209. else
  210. {
  211. SetCam(1, 1, RightCam);
  212. SetCam(1, 1, LeftCam);
  213. }
  214. }
  215. void SetCam(float x, float y, Camera cam)
  216. {
  217. cam.rect = new Rect(x, y, 1, 1);
  218. }
  219. */