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.

113 lines
3.4 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 Awake()
  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. Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
  41. spawned = true;
  42. break;
  43. }
  44. }
  45. }
  46. if(spawned == false){
  47. spawnposition.z -= 1;
  48. checkValid(spawnposition);
  49. }
  50. }
  51. public void getPlayerLocations()
  52. {
  53. Vector3 spawnposition = new Vector3(0, 1, 0);
  54. Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z);
  55. Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z);
  56. //x
  57. if (playerOne.x > playerTwo.x)
  58. {
  59. if ((playerOne.x - playerTwo.x) <= 1)
  60. {
  61. spawnposition.x = playerOne.x;
  62. }
  63. else
  64. {
  65. spawnposition.x = (playerOne.x - playerTwo.x) + playerOne.x;
  66. }
  67. }
  68. else
  69. {
  70. if ((playerTwo.x - playerOne.x) <= 1)
  71. {
  72. spawnposition.x = playerTwo.x;
  73. }
  74. else
  75. {
  76. spawnposition.x = (playerTwo.x - playerOne.x) + playerTwo.x;
  77. }
  78. }
  79. //z
  80. if (playerOne.y > playerTwo.y)
  81. {
  82. if ((playerOne.y - playerTwo.y) <= 1)
  83. {
  84. spawnposition.z = playerOne.y;
  85. }
  86. else
  87. {
  88. spawnposition.z = (playerOne.y - playerTwo.y) + playerOne.y;
  89. }
  90. }
  91. else
  92. {
  93. if ((playerTwo.y - playerOne.y) <= 1)
  94. {
  95. spawnposition.z = playerTwo.y;
  96. }
  97. else
  98. {
  99. spawnposition.z = (playerTwo.y - playerOne.y) + playerTwo.y;
  100. }
  101. }
  102. checkValid(spawnposition);
  103. }
  104. }