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.

149 lines
4.3 KiB

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using Networking.Server;
  5. public class blockSpawn : MonoBehaviour
  6. {
  7. [SerializeField]
  8. public Inventory.Data[] LogicList;
  9. List<ClientData> ConnectedClients;
  10. public ClientList clientDataList;
  11. public List<Block> SpawnBlocks;
  12. List<Vector3> spawnedLocations;
  13. public List<Block> possibleSpawnLocations;
  14. int spawnNumber = 2;
  15. public int min_x, max_x;
  16. void Awake()
  17. {
  18. ConnectedClients = clientDataList.ConnectedClients;
  19. spawnedLocations = new List<Vector3>();
  20. possibleSpawnLocations = new List<Block>();
  21. }
  22. public void wakeup()
  23. {
  24. SpawnBlocks.Clear();
  25. SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isCollectableSpawnable).ToList();
  26. }
  27. public void Spawn()
  28. {
  29. wakeup();
  30. //Sort play locations so leader is first
  31. if (ConnectedClients.Count > 1)
  32. {
  33. ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x));
  34. }
  35. min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x;
  36. max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 4;
  37. updatePlayerPositions();
  38. updatePositions(min_x - 1);
  39. //Check points within the bounds of players
  40. possibleSpawnLocations.Clear();
  41. foreach (Block point in SpawnBlocks)
  42. {
  43. if(point.transform.position.x >= min_x && point.transform.position.x <= max_x)
  44. {
  45. possibleSpawnLocations.Add(point);
  46. }
  47. }
  48. int triesCount = 0;
  49. if(possibleSpawnLocations.Count > 0)
  50. {
  51. while (spawnNumber > 0 && triesCount < 25)
  52. {
  53. if(possibleSpawnLocations.Count > 0){
  54. int choice = Random.Range(0, possibleSpawnLocations.Count - 1);
  55. bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position);
  56. if (spawned == true)
  57. {
  58. possibleSpawnLocations.RemoveAt(choice);
  59. triesCount = 0;
  60. spawnNumber--;
  61. }else{
  62. triesCount++;
  63. continue;
  64. }
  65. }
  66. else
  67. {
  68. goto escape;
  69. }
  70. }
  71. }
  72. escape:
  73. spawnNumber = 2;
  74. }
  75. private bool checkLocation(Vector3 spawnposition)
  76. {
  77. bool duplicate = checkDuplicatePosition(spawnposition);
  78. if (duplicate == false)
  79. {
  80. spawnBlock(spawnposition);
  81. return true;
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. private bool checkDuplicatePosition(Vector3 spawnposition)
  89. {
  90. if (spawnedLocations.Count > 0)
  91. {
  92. for (int k = 0; k < spawnedLocations.Count; k++)
  93. {
  94. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  95. {
  96. return true;
  97. }
  98. }
  99. }
  100. return false;
  101. }
  102. private void spawnBlock(Vector3 spawnposition)
  103. {
  104. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  105. GameObject block = Instantiate(prefab);
  106. spawnposition.y = 1.0f;
  107. block.transform.position = spawnposition;
  108. spawnedLocations.Add(spawnposition);
  109. }
  110. public void assignLogicBlock(GameObject block, float value)
  111. {
  112. int number = Random.Range(0, LogicList.Length-1);
  113. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = LogicList[number].element;
  114. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = 1;
  115. }
  116. public void updatePositions(int min)
  117. {
  118. for(int i = SpawnBlocks.Count -1; i>=0; i--)
  119. {
  120. if(SpawnBlocks[i].transform.position.x < min)
  121. {
  122. SpawnBlocks.Remove(SpawnBlocks[i]);
  123. }
  124. }
  125. }
  126. public void updatePlayerPositions()
  127. {
  128. for (int i = SpawnBlocks.Count - 1; i >= 0; i--)
  129. {
  130. if (SpawnBlocks[i].CurrentPlayer != null)
  131. {
  132. SpawnBlocks.Remove(SpawnBlocks[i]);
  133. }
  134. }
  135. }
  136. }