From 764cb2c53748a3186543774b18c14f6591ad7b2d Mon Sep 17 00:00:00 2001 From: ClairePeta Date: Wed, 2 Oct 2019 15:27:55 +1000 Subject: [PATCH] Block spawning update --- Assets/Scripts/Character.cs | 40 ++++++------- Assets/Scripts/blockSpawn.cs | 108 +++++++++++++++++++---------------- 2 files changed, 77 insertions(+), 71 deletions(-) diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index 49e9dcf..04082c8 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -135,8 +135,6 @@ public class Character : MonoBehaviour StopAnimation(animation); } - - public IEnumerator RotateInDirection(Direction direction,float angles, Animation animation, float time) { System.Func yFunction = null; @@ -153,7 +151,6 @@ public class Character : MonoBehaviour private IEnumerator LerpToBlock(Vector3 target, float time, System.Func heightOffset = null) { - Vector3 _startPos = transform.position; Vector3 _endPos = target; @@ -161,8 +158,6 @@ public class Character : MonoBehaviour float elapsedTime = 0; - - while (elapsedTime / time < 1) { @@ -187,7 +182,6 @@ public class Character : MonoBehaviour private IEnumerator Rotate(Direction direction, float angles, float time, System.Func heightOffset = null) { - int RotationDir = 0; switch (direction) { @@ -207,7 +201,6 @@ public class Character : MonoBehaviour lastRotation = angles * RotationDir; - float elapsedTime = 0; float anglePerSecond = (angles * RotationDir) / time; Vector3 _startPos = transform.position; @@ -229,7 +222,6 @@ public class Character : MonoBehaviour transform.position = _startPos + Vector3.up * heightOffset(1); } - public IEnumerator AnimateToPosition(Vector3 position, Animation animation, float time) { System.Func yFunction = null; @@ -288,8 +280,7 @@ public class Character : MonoBehaviour break; } } - - + public void respawnCharacter() { /* Will introduce more complex criteria for choosing where to respawn the player in future @@ -377,21 +368,28 @@ public class Character : MonoBehaviour void OnTriggerEnter(Collider other) { Collectable collectable = other.GetComponentInChildren(); + blockSpawn spawn = GetComponent(); if (collectable != null) { - collectable.OnCollect(this); - } - if (other.gameObject.name == "collect_sphere") - { - other.gameObject.SetActive(false); - //player.collected +=1; + //get position from average; + ClientList list = spawn.clientDataList; + float average = 0; + int livePlayerCount = 0; + foreach (ClientData data in list.ConnectedClients) + { + if(data.Lives > 0) + { + average += data.playerCharacter.transform.position.x; + livePlayerCount++; + } + } + average /= livePlayerCount; + float tosend = lives + (transform.position.x - average); - } - if (other.gameObject.name == "End Portal") - { - other.GetComponent().enabled = false; - SceneManager.LoadScene(nextScene); + spawn.assignLogicBlock(collectable.gameObject, tosend); + + collectable.OnCollect(this); } } diff --git a/Assets/Scripts/blockSpawn.cs b/Assets/Scripts/blockSpawn.cs index 8b71330..dda7e10 100644 --- a/Assets/Scripts/blockSpawn.cs +++ b/Assets/Scripts/blockSpawn.cs @@ -6,68 +6,74 @@ using Networking.Server; public class blockSpawn : MonoBehaviour { [SerializeField] - public Inventory.Data[] spawnLogicList; + public Inventory.Data[] StrongLogicList; + public Inventory.Data[] NormalLogicList; + public Inventory.Data[] WeakLogicList; + Inventory.Data[] listtoUse; + List ConnectedClients; public ClientList clientDataList; public Block[] SpawnBlocks; List spawnedLocations; - int min_z = -2, max_z = 2; - Vector3 spawnposition = new Vector3(0, 0, 0); + List possibleSpawnLocations; + int spawnNumber = 2; void Awake() { ConnectedClients = clientDataList.ConnectedClients; spawnedLocations = new List(); + possibleSpawnLocations = new List(); } public void wakeup() { - SpawnBlocks = FindObjectsOfType().Where(p => p.is_Walkable).ToArray(); + SpawnBlocks = FindObjectsOfType().Where(p => p.isCollectableSpawnable).ToArray(); } public void Spawn() { - /* - Get all player locations, get leader(biggest x position value) and min value - +2 to each value, set as min and man in random.range() - add locations to a list to ensure no duplicates - check not over pit or hole - */ - + //Sort play locations so leader is first ConnectedClients.Sort((b, a) => a.playerCharacter.transform.position.x.CompareTo(b.playerCharacter.transform.position.x)); - int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x + 2; + + //add two to each to set bounds + int min_x = (int)ConnectedClients[ConnectedClients.Count - 1].playerCharacter.transform.position.x + 3; int max_x = (int)ConnectedClients[0].playerCharacter.transform.position.x + 2; - for(int i = 0; i < 2; i++) + //Check points within the bounds of players + foreach(Block point in SpawnBlocks) { - spawnposition = new Vector3(Random.Range(min_x, max_x), -0.5f, Random.Range(min_z, max_z)); - checkLocation(spawnposition); + 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 + while(spawnNumber > 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 void checkLocation(Vector3 spawnposition) + private bool checkLocation(Vector3 spawnposition) { bool duplicate = checkDuplicatePosition(spawnposition); if (duplicate == false) { - bool valid = checkValid(spawnposition); - if (valid == true) - { - spawnBlock(spawnposition); - } - else - { - //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition)); - //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z)); - Debug.Log("Fail one"); - } + spawnBlock(spawnposition); + return true; } else { - //this needs to be changed - //Block clostest = Utility.minBy(SpawnBlocks, p => Vector3.Distance(p.transform.position, spawnposition)); - //checkLocation(new Vector3(spawnposition.x, spawnposition.y + 1.5f, spawnposition.z)); - Debug.Log("Fail two"); + Debug.Log("Fail to spawn - duplicate positioning"); + return false; } } @@ -86,30 +92,32 @@ public class blockSpawn : MonoBehaviour return false; } - private bool checkValid(Vector3 spawnposition) - { - //is over a walkable block - for (int i = 0; i < SpawnBlocks.Length; i++) - { - if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.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; - - 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); spawnedLocations.Add(spawnposition); } + + public void assignLogicBlock(GameObject block, float value) + { + if(value > 0) + { + listtoUse = WeakLogicList; + } + else if (value < 0 && value > (-10)) + { + listtoUse = NormalLogicList; + } + else if (value <= (-10)) + { + listtoUse = StrongLogicList; + } + + int number = (int)Random.Range(0, listtoUse.Length-1); + block.GetComponent().Collectable.element = listtoUse[number].element; + block.GetComponent().Collectable.Count = listtoUse[number].Count; + } } \ No newline at end of file