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.

139 lines
4.6 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. void Awake()
  19. {
  20. ConnectedClients = clientDataList.ConnectedClients;
  21. spawnedLocations = new List<Vector3>();
  22. possibleSpawnLocations = new List<Block>();
  23. }
  24. public void wakeup()
  25. {
  26. SpawnBlocks.Clear();
  27. SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.isCollectableSpawnable).ToList();
  28. }
  29. public void Spawn()
  30. {
  31. //Sort play locations so leader is first
  32. if (ConnectedClients.Count > 1)
  33. {
  34. ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x));
  35. }
  36. //add two to each to set bounds
  37. int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x;
  38. int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x +2;
  39. //Debug.Log("Min x: " + min_x + " max x: " + max_x);
  40. //Check points within the bounds of players
  41. foreach(Block point in SpawnBlocks)
  42. {
  43. //Debug.Log("Position: " + point.transform.position.x + " and " + point.transform.position.z);
  44. if(point.transform.position.x >= min_x && point.transform.position.x <= max_x)
  45. {
  46. possibleSpawnLocations.Add(point);
  47. }
  48. }
  49. //pick a random value from those available, checks the location
  50. //then removes it to remove the possibility of duplicates
  51. //Debug.Log(possibleSpawnLocations.Count);
  52. if(possibleSpawnLocations.Count > 0)
  53. {
  54. while (spawnNumber > 0)
  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. spawnNumber--;
  63. }
  64. }
  65. }
  66. }
  67. spawnNumber = 2;
  68. }
  69. private bool checkLocation(Vector3 spawnposition)
  70. {
  71. bool duplicate = checkDuplicatePosition(spawnposition);
  72. if (duplicate == false)
  73. {
  74. spawnBlock(spawnposition);
  75. return true;
  76. }
  77. else
  78. {
  79. Debug.Log("Fail to spawn - duplicate positioning");
  80. return false;
  81. }
  82. }
  83. private bool checkDuplicatePosition(Vector3 spawnposition)
  84. {
  85. if (spawnedLocations.Count > 0)
  86. {
  87. for (int k = 0; k < spawnedLocations.Count; k++)
  88. {
  89. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  90. {
  91. return true;
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. private void spawnBlock(Vector3 spawnposition)
  98. {
  99. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  100. GameObject block = Instantiate(prefab);
  101. spawnposition.y += 1.5f;
  102. block.transform.position = spawnposition;
  103. spawnedLocations.Add(spawnposition);
  104. }
  105. public void assignLogicBlock(GameObject block, float value)
  106. {
  107. if(value > 1)
  108. {
  109. listtoUse = WeakLogicList;
  110. }
  111. else if (value <= 1 && value > (-4))
  112. {
  113. listtoUse = NormalLogicList;
  114. }
  115. else if (value <= (-5))
  116. {
  117. listtoUse = StrongLogicList;
  118. }
  119. //max behind camera is -6/-7
  120. //max lives = 3
  121. // ~ anyone ahead of the average will have a minimum of 4(full lives) or 2(with 1 life left)
  122. // ~ anyone behind the average will have a minimum of -3-4(full lives) or -5/-6 (with one life left)
  123. int number = Random.Range(0, listtoUse.Length-1);
  124. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = listtoUse[number].element;
  125. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = 1;
  126. }
  127. }