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.

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