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.

111 lines
3.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using Networking.Server;
  7. using Networking;
  8. using UnityEngine.SceneManagement;
  9. public class blockSpawn : MonoBehaviour
  10. {
  11. [SerializeField]
  12. public Inventory.Data[] spawnLogicList;
  13. public List<ClientData> ConnectedClients;
  14. public ClientList clientDataList;
  15. public Block[] SpawnBlocks;
  16. bool spawned = false;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. ConnectedClients = clientDataList.ConnectedClients;
  21. SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
  22. }
  23. public void getPlayerScores()
  24. {
  25. ConnectedClients.Sort((b, a) => a.SceneScore.CompareTo(b.SceneScore));
  26. }
  27. public void checkValid(Vector3 spawnposition)
  28. {
  29. for(int i = 0; i < SpawnBlocks.Length; i++)
  30. {
  31. if(spawned == false){
  32. if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z)
  33. {
  34. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  35. GameObject block = Instantiate(prefab);
  36. int number = (int)Random.Range(1.0f, spawnLogicList.Length);
  37. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element;
  38. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = spawnLogicList[number].Count;
  39. block.transform.position = spawnposition;
  40. spawned = true;
  41. break;
  42. }
  43. }
  44. }
  45. if(spawned == false){
  46. spawnposition.z -= 1;
  47. checkValid(spawnposition);
  48. }
  49. }
  50. public void getPlayerLocations()
  51. {
  52. Vector3 spawnposition = new Vector3(0, 1, 0);
  53. Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z);
  54. Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z);
  55. //x
  56. if (playerOne.x > playerTwo.x)
  57. {
  58. if ((playerOne.x - playerTwo.x) <= 1)
  59. {
  60. spawnposition.x = playerOne.x;
  61. }
  62. else
  63. {
  64. spawnposition.x = (playerOne.x - playerTwo.x) + playerOne.x;
  65. }
  66. }
  67. else
  68. {
  69. if ((playerTwo.x - playerOne.x) <= 1)
  70. {
  71. spawnposition.x = playerTwo.x;
  72. }
  73. else
  74. {
  75. spawnposition.x = (playerTwo.x - playerOne.x) + playerTwo.x;
  76. }
  77. }
  78. //z
  79. if (playerOne.y > playerTwo.y)
  80. {
  81. if ((playerOne.y - playerTwo.y) <= 1)
  82. {
  83. spawnposition.z = playerOne.y;
  84. }
  85. else
  86. {
  87. spawnposition.z = (playerOne.y - playerTwo.y) + playerOne.y;
  88. }
  89. }
  90. else
  91. {
  92. if ((playerTwo.y - playerOne.y) <= 1)
  93. {
  94. spawnposition.z = playerTwo.y;
  95. }
  96. else
  97. {
  98. spawnposition.z = (playerTwo.y - playerOne.y) + playerTwo.y;
  99. }
  100. }
  101. checkValid(spawnposition);
  102. }
  103. }