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.

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