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.

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