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.

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