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; 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() { //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)); } //add two to each to set bounds int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x; int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x +2; //Debug.Log("Min x: " + min_x + " max x: " + max_x); //Check points within the bounds of players foreach(Block point in SpawnBlocks) { //Debug.Log("Position: " + point.transform.position.x + " and " + point.transform.position.z); if(point.transform.position.x >= min_x && point.transform.position.x <= max_x) { possibleSpawnLocations.Add(point); } } //pick a random value from those available, checks the location //then removes it to remove the possibility of duplicates //Debug.Log(possibleSpawnLocations.Count); if(possibleSpawnLocations.Count > 0) { while (spawnNumber > 0) { 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); spawnNumber--; } } } } spawnNumber = 2; } private bool checkLocation(Vector3 spawnposition) { bool duplicate = checkDuplicatePosition(spawnposition); if (duplicate == false) { spawnBlock(spawnposition); return true; } else { Debug.Log("Fail to spawn - duplicate positioning"); 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.5f; 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; } //max behind camera is -6/-7 //max lives = 3 // ~ anyone ahead of the average will have a minimum of 4(full lives) or 2(with 1 life left) // ~ anyone behind the average will have a minimum of -3-4(full lives) or -5/-6 (with one life left) int number = Random.Range(0, listtoUse.Length-1); block.GetComponent().Collectable.element = listtoUse[number].element; block.GetComponent().Collectable.Count = 1; } }