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.

182 lines
5.8 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 + 4;
  39. Debug.Log("count before update " + SpawnBlocks.Count);
  40. updatePlayerPositions();
  41. Debug.Log("count mid update " + SpawnBlocks.Count);
  42. updatePositions(min_x - 1);
  43. Debug.Log("count after update " + SpawnBlocks.Count);
  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("possibleSpawnLocations count " + 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. bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position);
  65. if (spawned == true)
  66. {
  67. possibleSpawnLocations.RemoveAt(choice);
  68. triesCount = 0;
  69. spawnNumber--;
  70. }else{
  71. triesCount++;
  72. continue;
  73. }
  74. }
  75. else
  76. {
  77. goto escape;
  78. }
  79. //Debug.Log("spawnNumber " + spawnNumber);
  80. }
  81. //Debug.Log("SpawnTries: " + triesCount);
  82. }
  83. escape:
  84. spawnNumber = 2;
  85. }
  86. private bool checkLocation(Vector3 spawnposition)
  87. {
  88. bool duplicate = checkDuplicatePosition(spawnposition);
  89. if (duplicate == false)
  90. {
  91. spawnBlock(spawnposition);
  92. //Debug.Log("spawned at" + spawnposition);
  93. return true;
  94. }
  95. else
  96. {
  97. //Debug.Log("Fail to spawn at " + spawnposition);
  98. return false;
  99. }
  100. }
  101. private bool checkDuplicatePosition(Vector3 spawnposition)
  102. {
  103. if (spawnedLocations.Count > 0)
  104. {
  105. for (int k = 0; k < spawnedLocations.Count; k++)
  106. {
  107. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  108. {
  109. return true;
  110. }
  111. }
  112. }
  113. return false;
  114. }
  115. private void spawnBlock(Vector3 spawnposition)
  116. {
  117. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  118. GameObject block = Instantiate(prefab);
  119. spawnposition.y = 1.0f;
  120. block.transform.position = spawnposition;
  121. spawnedLocations.Add(spawnposition);
  122. }
  123. public void assignLogicBlock(GameObject block, float value)
  124. {
  125. if(value > 1)
  126. {
  127. listtoUse = WeakLogicList;
  128. }
  129. else if (value <= 1 && value > (-4))
  130. {
  131. listtoUse = NormalLogicList;
  132. }
  133. else if (value <= (-5))
  134. {
  135. listtoUse = StrongLogicList;
  136. }
  137. //max behind camera is -6/-7
  138. //max lives = 3
  139. // ~ anyone ahead of the average will have a minimum of 4(full lives) or 2(with 1 life left)
  140. // ~ anyone behind the average will have a minimum of -3-4(full lives) or -5/-6 (with one life left)
  141. int number = Random.Range(0, listtoUse.Length-1);
  142. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = listtoUse[number].element;
  143. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = 1;
  144. }
  145. public void updatePositions(int min)
  146. {
  147. for(int i = SpawnBlocks.Count -1; i>=0; i--)
  148. {
  149. if(SpawnBlocks[i].transform.position.x <= min)
  150. {
  151. Debug.Log("block to remove position x: " + SpawnBlocks[i].transform.position.x);
  152. SpawnBlocks.Remove(SpawnBlocks[i]);
  153. }
  154. }
  155. }
  156. public void updatePlayerPositions()
  157. {
  158. for (int i = SpawnBlocks.Count - 1; i >= 0; i--)
  159. {
  160. if (SpawnBlocks[i].CurrentPlayer != null)
  161. {
  162. SpawnBlocks.Remove(SpawnBlocks[i]);
  163. }
  164. }
  165. }
  166. }