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.

180 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 +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. if(possibleSpawnLocations.Count > 0)
  58. {
  59. while (spawnNumber > 0)
  60. {
  61. if(possibleSpawnLocations.Count > 0){
  62. int choice = Random.Range(0, possibleSpawnLocations.Count - 1);
  63. Debug.Log("count inside " + possibleSpawnLocations.Count);
  64. bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position);
  65. Debug.Log("bool spawned = " + spawned);
  66. if (spawned == true)
  67. {
  68. possibleSpawnLocations.RemoveAt(choice);
  69. spawnNumber--;
  70. }else{
  71. continue;
  72. }
  73. }
  74. else
  75. {
  76. Debug.Log("spawn else");
  77. goto escape;
  78. }
  79. Debug.Log("spawnNumber " + spawnNumber);
  80. }
  81. }
  82. escape:
  83. spawnNumber = 2;
  84. }
  85. private bool checkLocation(Vector3 spawnposition)
  86. {
  87. Debug.Log("Check location function");
  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. Debug.Log("Check duplicate function");
  104. if (spawnedLocations.Count > 0)
  105. {
  106. for (int k = 0; k < spawnedLocations.Count; k++)
  107. {
  108. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  109. {
  110. return true;
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116. private void spawnBlock(Vector3 spawnposition)
  117. {
  118. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  119. GameObject block = Instantiate(prefab);
  120. spawnposition.y = 1.0f;
  121. block.transform.position = spawnposition;
  122. spawnedLocations.Add(spawnposition);
  123. }
  124. public void assignLogicBlock(GameObject block, float value)
  125. {
  126. if(value > 1)
  127. {
  128. listtoUse = WeakLogicList;
  129. }
  130. else if (value <= 1 && value > (-4))
  131. {
  132. listtoUse = NormalLogicList;
  133. }
  134. else if (value <= (-5))
  135. {
  136. listtoUse = StrongLogicList;
  137. }
  138. //max behind camera is -6/-7
  139. //max lives = 3
  140. // ~ anyone ahead of the average will have a minimum of 4(full lives) or 2(with 1 life left)
  141. // ~ anyone behind the average will have a minimum of -3-4(full lives) or -5/-6 (with one life left)
  142. int number = Random.Range(0, listtoUse.Length-1);
  143. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = listtoUse[number].element;
  144. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = 1;
  145. }
  146. public void updatePositions(float max)
  147. {
  148. for(int i = SpawnBlocks.Count -1; i>=0; i--)
  149. {
  150. if(SpawnBlocks[i].transform.position.x <= (int)max)
  151. {
  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. }