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

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 { get { return (-MapDimensions.x * (TileSize / 2)); } }
  15. public float maxX { get { return (MapDimensions.x * (TileSize / 2)); } }
  16. public float minZ { get { return (-MapDimensions.y * (TileSize / 2)); } }
  17. public float maxZ { get { return (MapDimensions.y * (TileSize / 2)); } }
  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. PS.CreateDummies(this);
  37. }
  38. }
  39. public void GenerateTiles()
  40. {
  41. float HalfSize = TileSize / 2;
  42. Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));
  43. for (int x = 0; x < MapDimensions.x; x++)
  44. {
  45. for (int z = 0; z < MapDimensions.y; z++)
  46. {
  47. int randIndex = Random.Range(0, Tiles.Length);
  48. float RandomRotation = Random.Range(0, 4) * 90;
  49. Vector3 position = startPoint + new Vector3(TileSize * x + HalfSize, 0.0f, TileSize * z + HalfSize);
  50. Debug.Log(position);
  51. GameObject Tile = Instantiate(Tiles[randIndex]);
  52. Tile.transform.position = position;
  53. Tile.transform.Rotate(Vector3.up, RandomRotation);
  54. Tile.name = "Tile [" + x + "," + z + "]";
  55. if (x == 0)
  56. {
  57. Vector3 newPos = position;
  58. newPos.x += TileSize * MapDimensions.x;
  59. GameObject MirrorTile = Instantiate(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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(Tiles[randIndex]);
  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 (GameObject LP in tile.LandmarkPoints)
  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. }
  144. }
  145. }
  146. #endregion
  147. }
  148. //EXTRA STUFF, PLEASE IGNORE
  149. /*
  150. * //IN UPDATE
  151. HandleCameraMovement(TopCam, true);
  152. HandleCameraMovement(BotCam, true);
  153. HandleCameraMovement(LeftCam, false);
  154. HandleCameraMovement(RightCam, false);
  155. HandleCamSizing();
  156. */
  157. /*
  158. /// <summary>
  159. ///
  160. /// </summary>
  161. /// <param name="Cam"> The camera to move</param>
  162. /// <param name="Xmove">Do we move in the X axis or the Z?</param>
  163. void HandleCameraMovement(Camera Cam, bool Xmove)
  164. {
  165. if (Xmove == true)
  166. {
  167. Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
  168. }
  169. else
  170. {
  171. Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
  172. }
  173. }
  174. void HandleCamSizing()
  175. {
  176. Transform PT = LocalPlayer.transform;
  177. if (PT.position.x > maxX - camWidth)
  178. {
  179. float dif = ((maxX - PT.position.x) / camWidth) / 2;
  180. SetCam(dif, 0, RightCam);
  181. }
  182. else if (PT.position.x < minX + camWidth)
  183. {
  184. float dif = ((minX - PT.position.x) / camWidth) / 2;
  185. SetCam(dif, 0, LeftCam);
  186. }
  187. else
  188. {
  189. SetCam(1, 1, RightCam);
  190. SetCam(1, 1, LeftCam);
  191. }
  192. if (PT.position.z > maxZ - camHeight)
  193. {
  194. float dif = ((maxZ - PT.position.z) / camHeight) / 2;
  195. SetCam(0, dif, TopCam);
  196. }
  197. else if (PT.position.x < + camHeight)
  198. {
  199. float dif = ((minZ - PT.position.z) / camHeight) / 2;
  200. SetCam(0, dif, BotCam);
  201. }
  202. else
  203. {
  204. SetCam(1, 1, RightCam);
  205. SetCam(1, 1, LeftCam);
  206. }
  207. }
  208. void SetCam(float x, float y, Camera cam)
  209. {
  210. cam.rect = new Rect(x, y, 1, 1);
  211. }
  212. */