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.

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