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.

184 lines
5.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. Debug.Log("Count after wakeup function " + SpawnBlocks.Count);
  30. }
  31. public void Spawn()
  32. {
  33. wakeup();
  34. //Sort play locations so leader is first
  35. if (ConnectedClients.Count > 1)
  36. {
  37. ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x));
  38. }
  39. min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x;
  40. max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 4;
  41. Debug.Log("count before update " + SpawnBlocks.Count);
  42. updatePlayerPositions();
  43. Debug.Log("count mid update " + SpawnBlocks.Count);
  44. updatePositions(min_x - 1);
  45. Debug.Log("count after update " + SpawnBlocks.Count);
  46. //Check points within the bounds of players
  47. possibleSpawnLocations.Clear();
  48. foreach (Block point in SpawnBlocks)
  49. {
  50. //Debug.Log("Position: " + point.transform.position.x + " and " + point.transform.position.z);
  51. if(point.transform.position.x >= min_x && point.transform.position.x <= max_x)
  52. {
  53. possibleSpawnLocations.Add(point);
  54. }
  55. }
  56. //pick a random value from those available, checks the location
  57. //then removes it to remove the possibility of duplicates
  58. Debug.Log("possibleSpawnLocations count " + possibleSpawnLocations.Count);
  59. int triesCount = 0;
  60. if(possibleSpawnLocations.Count > 0)
  61. {
  62. while (spawnNumber > 0 && triesCount < 25)
  63. {
  64. if(possibleSpawnLocations.Count > 0){
  65. int choice = Random.Range(0, possibleSpawnLocations.Count - 1);
  66. bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position);
  67. if (spawned == true)
  68. {
  69. possibleSpawnLocations.RemoveAt(choice);
  70. triesCount = 0;
  71. spawnNumber--;
  72. }else{
  73. triesCount++;
  74. continue;
  75. }
  76. }
  77. else
  78. {
  79. goto escape;
  80. }
  81. //Debug.Log("spawnNumber " + spawnNumber);
  82. }
  83. //Debug.Log("SpawnTries: " + triesCount);
  84. }
  85. escape:
  86. spawnNumber = 2;
  87. }
  88. private bool checkLocation(Vector3 spawnposition)
  89. {
  90. bool duplicate = checkDuplicatePosition(spawnposition);
  91. if (duplicate == false)
  92. {
  93. spawnBlock(spawnposition);
  94. //Debug.Log("spawned at" + spawnposition);
  95. return true;
  96. }
  97. else
  98. {
  99. //Debug.Log("Fail to spawn at " + spawnposition);
  100. return false;
  101. }
  102. }
  103. private bool checkDuplicatePosition(Vector3 spawnposition)
  104. {
  105. if (spawnedLocations.Count > 0)
  106. {
  107. for (int k = 0; k < spawnedLocations.Count; k++)
  108. {
  109. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  110. {
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. private void spawnBlock(Vector3 spawnposition)
  118. {
  119. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  120. GameObject block = Instantiate(prefab);
  121. spawnposition.y = 1.0f;
  122. block.transform.position = spawnposition;
  123. spawnedLocations.Add(spawnposition);
  124. }
  125. public void assignLogicBlock(GameObject block, float value)
  126. {
  127. if(value > 1)
  128. {
  129. listtoUse = WeakLogicList;
  130. }
  131. else if (value <= 1 && value > (-4))
  132. {
  133. listtoUse = NormalLogicList;
  134. }
  135. else if (value <= (-5))
  136. {
  137. listtoUse = StrongLogicList;
  138. }
  139. //max behind camera is -6/-7
  140. //max lives = 3
  141. // ~ anyone ahead of the average will have a minimum of 4(full lives) or 2(with 1 life left)
  142. // ~ anyone behind the average will have a minimum of -3-4(full lives) or -5/-6 (with one life left)
  143. int number = Random.Range(0, listtoUse.Length-1);
  144. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = listtoUse[number].element;
  145. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = 1;
  146. }
  147. public void updatePositions(int min)
  148. {
  149. for(int i = SpawnBlocks.Count -1; i>=0; i--)
  150. {
  151. //Debug.Log(SpawnBlocks[i].transform.position.x);
  152. if(SpawnBlocks[i].transform.position.x < min)
  153. {
  154. SpawnBlocks.Remove(SpawnBlocks[i]);
  155. }
  156. }
  157. }
  158. public void updatePlayerPositions()
  159. {
  160. for (int i = SpawnBlocks.Count - 1; i >= 0; i--)
  161. {
  162. if (SpawnBlocks[i].CurrentPlayer != null)
  163. {
  164. SpawnBlocks.Remove(SpawnBlocks[i]);
  165. }
  166. }
  167. }
  168. }