using System.Collections.Generic; using System.Linq; using UnityEngine; using Networking.Server; public class blockSpawn : MonoBehaviour { [SerializeField] public Inventory.Data[] spawnLogicList; public List ConnectedClients; public ClientList clientDataList; public Block[] SpawnBlocks; bool spawned = false; void Awake() { ConnectedClients = clientDataList.ConnectedClients; SpawnBlocks = FindObjectsOfType().Where(p => p.is_Walkable).ToArray(); } public void getPlayerScores() { ConnectedClients.Sort((b, a) => a.SceneScore.CompareTo(b.SceneScore)); } public void checkValid(Vector3 spawnposition) { for(int i = 0; i < SpawnBlocks.Length; i++) { if(spawned == false){ if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z) { GameObject prefab = Resources.Load("Logic Block") as GameObject; GameObject block = Instantiate(prefab); int number = (int)Random.Range(1.0f, spawnLogicList.Length); block.GetComponent().Collectable.element = spawnLogicList[number].element; block.GetComponent().Collectable.Count = spawnLogicList[number].Count; block.transform.position = spawnposition; Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position); spawned = true; break; } } } if (spawned == false){ //needs to be changed, can get caught in an infinite loop when z is 7/8 if (spawnposition.z >= 2 && spawnposition.z <= 8) { spawnposition.z += 1; checkValid(spawnposition); } else if (spawnposition.z >= 8 && spawnposition.z <= 12) { spawnposition.z -= 1; checkValid(spawnposition); } } } public void getPlayerLocations() { Vector3 spawnposition = new Vector3(0, 1, 0); Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z); Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z); //x if (playerOne.x > playerTwo.x) { spawnposition.x = ((playerOne.x - playerTwo.x)/2) + playerTwo.x; } else { spawnposition.x = ((playerTwo.x - playerOne.x)/2) + playerOne.x; } //z if (playerOne.y > playerTwo.y) { spawnposition.z = ((playerOne.y - playerTwo.y)/2) + playerTwo.y; } else { spawnposition.z = ((playerTwo.y - playerOne.y)/2) + playerOne.y; } checkValid(spawnposition); } }