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.

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