using System.Collections.Generic; using System.Linq; using UnityEngine; using Networking.Server; public class blockSpawn : MonoBehaviour { [SerializeField] public Inventory.Data[] StrongLogicList; public Inventory.Data[] NormalLogicList; public Inventory.Data[] WeakLogicList; Inventory.Data[] listtoUse; List ConnectedClients; public ClientList clientDataList; public List SpawnBlocks; List spawnedLocations; public List possibleSpawnLocations; int spawnNumber = 2; public int min_x, max_x; void Awake() { ConnectedClients = clientDataList.ConnectedClients; spawnedLocations = new List(); possibleSpawnLocations = new List(); } public void wakeup() { SpawnBlocks.Clear(); SpawnBlocks = FindObjectsOfType().Where(p => p.isCollectableSpawnable).ToList(); } public void Spawn() { wakeup(); //Sort play locations so leader is first if (ConnectedClients.Count > 1) { ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x)); } min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x; max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 4; updatePlayerPositions(); updatePositions(min_x - 1); //Check points within the bounds of players possibleSpawnLocations.Clear(); foreach (Block point in SpawnBlocks) { if(point.transform.position.x >= min_x && point.transform.position.x <= max_x) { possibleSpawnLocations.Add(point); } } int triesCount = 0; if(possibleSpawnLocations.Count > 0) { while (spawnNumber > 0 && triesCount < 25) { if(possibleSpawnLocations.Count > 0){ int choice = Random.Range(0, possibleSpawnLocations.Count - 1); bool spawned = checkLocation(possibleSpawnLocations[choice].transform.position); if (spawned == true) { possibleSpawnLocations.RemoveAt(choice); triesCount = 0; spawnNumber--; }else{ triesCount++; continue; } } else { goto escape; } //Debug.Log("spawnNumber " + spawnNumber); } //Debug.Log("SpawnTries: " + triesCount); } escape: spawnNumber = 2; } private bool checkLocation(Vector3 spawnposition) { bool duplicate = checkDuplicatePosition(spawnposition); if (duplicate == false) { spawnBlock(spawnposition); return true; } else { return false; } } private bool checkDuplicatePosition(Vector3 spawnposition) { if (spawnedLocations.Count > 0) { for (int k = 0; k < spawnedLocations.Count; k++) { if (spawnedLocations[k].x == spawnposition.x && spawnedLocations[k].z == spawnposition.z) { return true; } } } return false; } private void spawnBlock(Vector3 spawnposition) { GameObject prefab = Resources.Load("Logic Block") as GameObject; GameObject block = Instantiate(prefab); spawnposition.y = 1.0f; block.transform.position = spawnposition; spawnedLocations.Add(spawnposition); } public void assignLogicBlock(GameObject block, float value) { if(value > 1) { listtoUse = WeakLogicList; } else if (value <= 1 && value > (-4)) { listtoUse = NormalLogicList; } else if (value <= (-5)) { listtoUse = StrongLogicList; } int number = Random.Range(0, listtoUse.Length-1); block.GetComponent().Collectable.element = listtoUse[number].element; block.GetComponent().Collectable.Count = 1; } public void updatePositions(int min) { for(int i = SpawnBlocks.Count -1; i>=0; i--) { if(SpawnBlocks[i].transform.position.x < min) { SpawnBlocks.Remove(SpawnBlocks[i]); } } } public void updatePlayerPositions() { for (int i = SpawnBlocks.Count - 1; i >= 0; i--) { if (SpawnBlocks[i].CurrentPlayer != null) { SpawnBlocks.Remove(SpawnBlocks[i]); } } } }