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.

114 lines
4.0 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[] spawnLogicList;
  9. List<ClientData> ConnectedClients;
  10. public ClientList clientDataList;
  11. public Block[] SpawnBlocks;
  12. List<Vector3> spawnedLocations;
  13. int min_z = -2, max_z = 2;
  14. Vector3 spawnposition = new Vector3(0, 0, 0);
  15. void Awake()
  16. {
  17. ConnectedClients = clientDataList.ConnectedClients;
  18. spawnedLocations = new List<Vector3>();
  19. }
  20. public void wakeup()
  21. {
  22. SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
  23. }
  24. public void Spawn()
  25. {
  26. /*
  27. Get all player locations, get leader(biggest x position value) and min value
  28. +2 to each value, set as min and man in random.range()
  29. add locations to a list to ensure no duplicates
  30. check not over pit or hole
  31. */
  32. ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x));
  33. int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x + 2;
  34. int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 2;
  35. for(int i = 0; i < 2; i++)
  36. {
  37. spawnposition = new Vector3(Random.Range(min_x, max_x), -0.5f, Random.Range(min_z, max_z));
  38. checkLocation(spawnposition);
  39. }
  40. }
  41. private void checkLocation(Vector3 spawnposition)
  42. {
  43. bool duplicate = checkDuplicatePosition(spawnposition);
  44. if (duplicate == false)
  45. {
  46. bool valid = checkValid(spawnposition);
  47. if (valid == true)
  48. {
  49. spawnBlock(spawnposition);
  50. }
  51. else
  52. {
  53. //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition));
  54. //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z));
  55. Debug.Log("Fail one");
  56. }
  57. }
  58. else
  59. {
  60. //this needs to be changed
  61. //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition));
  62. //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z));
  63. Debug.Log("Fail two");
  64. }
  65. }
  66. private bool checkDuplicatePosition(Vector3 spawnposition)
  67. {
  68. if (spawnedLocations.Count > 0)
  69. {
  70. for (int k = 0; k < spawnedLocations.Count; k++)
  71. {
  72. if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z)
  73. {
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. private bool checkValid(Vector3 spawnposition)
  81. {
  82. //is over a walkable block
  83. for (int i = 0; i < SpawnBlocks.Length; i++)
  84. {
  85. if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z)
  86. {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. private void spawnBlock(Vector3 spawnposition)
  93. {
  94. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  95. GameObject block = Instantiate(prefab);
  96. spawnposition.y += 1.5f;
  97. int number = (int)Random.Range(1.0f, spawnLogicList.Length);
  98. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element;
  99. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = spawnLogicList[number].Count;
  100. block.transform.position = spawnposition;
  101. Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
  102. spawnedLocations.Add(spawnposition);
  103. }
  104. }