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.

86 lines
3.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. public List<ClientData> ConnectedClients;
  10. public ClientList clientDataList;
  11. public Block[] SpawnBlocks;
  12. bool spawned = false;
  13. void Awake()
  14. {
  15. ConnectedClients = clientDataList.ConnectedClients;
  16. SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
  17. }
  18. public void getPlayerScores()
  19. {
  20. ConnectedClients.Sort((b, a) => a.SceneScore.CompareTo(b.SceneScore));
  21. }
  22. public void checkValid(Vector3 spawnposition)
  23. {
  24. for(int i = 0; i < SpawnBlocks.Length; i++)
  25. {
  26. if(spawned == false){
  27. if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z)
  28. {
  29. GameObject prefab = Resources.Load("Logic Block") as GameObject;
  30. GameObject block = Instantiate(prefab);
  31. int number = (int)Random.Range(1.0f, spawnLogicList.Length);
  32. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element;
  33. block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = spawnLogicList[number].Count;
  34. block.transform.position = spawnposition;
  35. Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
  36. spawned = true;
  37. break;
  38. }
  39. }
  40. }
  41. if (spawned == false){
  42. //needs to be changed, can get caught in an infinite loop when z is 7/8
  43. if (spawnposition.z >= 2 && spawnposition.z <= 8)
  44. {
  45. spawnposition.z += 1;
  46. checkValid(spawnposition);
  47. }
  48. else if (spawnposition.z >= 8 && spawnposition.z <= 12)
  49. {
  50. spawnposition.z -= 1;
  51. checkValid(spawnposition);
  52. }
  53. }
  54. }
  55. public void getPlayerLocations()
  56. {
  57. Vector3 spawnposition = new Vector3(0, 1, 0);
  58. Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z);
  59. Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z);
  60. //x
  61. if (playerOne.x > playerTwo.x)
  62. {
  63. spawnposition.x = ((playerOne.x - playerTwo.x)/2) + playerTwo.x;
  64. }
  65. else
  66. {
  67. spawnposition.x = ((playerTwo.x - playerOne.x)/2) + playerOne.x;
  68. }
  69. //z
  70. if (playerOne.y > playerTwo.y)
  71. {
  72. spawnposition.z = ((playerOne.y - playerTwo.y)/2) + playerTwo.y;
  73. }
  74. else
  75. {
  76. spawnposition.z = ((playerTwo.y - playerOne.y)/2) + playerOne.y;
  77. }
  78. checkValid(spawnposition);
  79. }
  80. }