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
3.5 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. //needs to be changed, can get caught in an infinite loop in negatives
  48. spawnposition.z -= 1;
  49. checkValid(spawnposition);
  50. }
  51. }
  52. public void getPlayerLocations()
  53. {
  54. Vector3 spawnposition = new Vector3(0, 1, 0);
  55. Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z);
  56. Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z);
  57. //x
  58. if (playerOne.x > playerTwo.x)
  59. {
  60. if ((playerOne.x - playerTwo.x) <= 1)
  61. {
  62. spawnposition.x = playerOne.x;
  63. }
  64. else
  65. {
  66. spawnposition.x = ((playerOne.x - playerTwo.x)/2) + playerOne.x;
  67. }
  68. }
  69. else
  70. {
  71. if ((playerTwo.x - playerOne.x) <= 1)
  72. {
  73. spawnposition.x = playerTwo.x;
  74. }
  75. else
  76. {
  77. spawnposition.x = ((playerTwo.x - playerOne.x)/2) + playerTwo.x;
  78. }
  79. }
  80. //z
  81. if (playerOne.y > playerTwo.y)
  82. {
  83. if ((playerOne.y - playerTwo.y) <= 1)
  84. {
  85. spawnposition.z = playerOne.y;
  86. }
  87. else
  88. {
  89. spawnposition.z = ((playerOne.y - playerTwo.y)/2) + playerOne.y;
  90. }
  91. }
  92. else
  93. {
  94. if ((playerTwo.y - playerOne.y) <= 1)
  95. {
  96. spawnposition.z = playerTwo.y;
  97. }
  98. else
  99. {
  100. spawnposition.z = ((playerTwo.y - playerOne.y)/2) + playerTwo.y;
  101. }
  102. }
  103. checkValid(spawnposition);
  104. }
  105. }