From e08ae38e70ddd47be8952de156db20cbf4b74c8b Mon Sep 17 00:00:00 2001 From: "s3607057 (Angus Niven)" Date: Thu, 17 Oct 2019 00:01:01 +1100 Subject: [PATCH] Fixed respawning-related issues (I hope). Players should now respawn only on open squares and never on traps. --- Assets/Scripts/Character.cs | 73 +++---------------- .../ColorGameMode/RacetrackGameMode.cs | 54 +++++++++++--- Assets/Scripts/LevelBlocks/Block.cs | 2 +- 3 files changed, 55 insertions(+), 74 deletions(-) diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs index 417d30f..328f995 100644 --- a/Assets/Scripts/Character.cs +++ b/Assets/Scripts/Character.cs @@ -218,80 +218,25 @@ public class Character : MonoBehaviour public void respawnCharacter() { - respawnCharacter(CurrentBlock.position); + respawnCharacter(CurrentBlock); } - public void respawnCharacter(Vector3 respawnPosition) + public void respawnCharacter(Block respawnPosition) { - /* Will introduce more complex criteria for choosing where to respawn the player in future - * For now: if the square one back (x =- 1) from the pit is walkable and unoccupied, then put them there - * Otherwise, try the next square back, etc, until we find an available one - */ - - Block currentBlock = null; - //We start from the position of our pit, at ground level - Vector3 currentPos = respawnPosition; - - //Debug.Log("Commencing respawn"); - - //Hardcoding the number of iterations for now for simplicity, should change this later - for (int i = 0; i < 100; i++) - { - //First we check one back - currentPos.x -= 1; - //Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z); - if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here? - { - //Debug.Log("Block exists"); - if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it? - { - //Debug.Log("Block is walkable"); - //Don't yet have a check for whether it's occupied - break; //If it is, we stop here - } - } - - //If the block one back isn't an option, we check to the left and right - currentPos.z += 1; - //Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z); - if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here? - { - //Debug.Log("Block exists"); - if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it? - { - //Debug.Log("Block is walkable"); - //Don't yet have a check for whether it's occupied - break; //If it is, we stop here - } - } - currentPos.z -= 2; - //Debug.Log("currentPos = " + currentPos.x + ", " + currentPos.y + ", " + currentPos.z); - if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) //Does a block exist here? - { - //Debug.Log("Block exists"); - if (currentBlock.isWalkable(~Ignore)) //If so, can we walk on it? - { - //Debug.Log("Block is walkable"); - //Don't yet have a check for whether it's occupied - break; //If it is, we stop here - } - } - - //If we've gotten this far and haven't found an available spot, we move back a row and try again - currentPos.z += 1; - } - //Having found our target block, we move the character there - if (currentBlock != null) + if (respawnPosition != null) { - this.transform.position = currentBlock.VisualPosition; + this.transform.position = respawnPosition.VisualPosition; this.respawnNeeded = false; - this._currentBlock = currentBlock; + this._currentBlock = respawnPosition; + respawnPosition.CurrentPlayer = this; + this.stuck = false; Debug.Log("Moved " + this.name + " to " + this.transform.position.x + ", " + this.transform.position.y + ", " + this.transform.position.z + ", " - + " respawnNeeded = " + respawnNeeded); + + ", respawnNeeded = " + respawnNeeded + + ", stuck = " + this.stuck); } else { diff --git a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs index 431d21a..1a376a7 100644 --- a/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs +++ b/Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs @@ -85,9 +85,11 @@ public class RacetrackGameMode : GameMode //If there are any players left, it will be the average of their positions //Otherwise, it will be as close to the centre of the screen as possible Vector3 respawnLocation = new Vector3(0.0f, -0.5f, 0.0f); + //Debug.Log("Initial respawn location: " + respawnLocation); if (survivingPlayers.Count > 0) //If any players are not currently waiting to respawn, we get the average of their locations { + //Debug.Log("Averaging survivor locations"); //We list the positions of all the surviving players List survivingPlayerLocations = new List(); foreach (PlayerData player in survivingPlayers) @@ -101,19 +103,23 @@ public class RacetrackGameMode : GameMode } respawnLocation = respawnLocation / survivingPlayers.Count; + //Debug.Log("Averaged survivor locations, new respawn location: " + respawnLocation); } else //Otherwise, we go for the middle of the screen { + //Debug.Log("Seeking the middle of the screen"); //We start in the middle of the track, at the back, and scroll forward until we're past the centre of the camera's view respawnLocation.x = mapManager.startX; Vector3 respawnLocationVP = Camera.main.WorldToViewportPoint(respawnLocation); - while (respawnLocationVP.x > 0.5f || respawnLocationVP.y > 0.5f) + while (respawnLocationVP.x < 0.5f || respawnLocationVP.y < 0.5f) { respawnLocation.x += 1.0f; respawnLocationVP = Camera.main.WorldToViewportPoint(respawnLocation); + //Debug.Log("respawnLocation = " + respawnLocation + ", viewport = " + respawnLocationVP); } + //Debug.Log("Found middle of camera view, new respawn location: " + respawnLocation); } //Now we randomly pick blocks in the vicinity of this point to respawn players on @@ -127,31 +133,61 @@ public class RacetrackGameMode : GameMode respawnBlocks = new List(); radius++; + //Debug.Log("Finding respawn blocks at radius = " + radius); + for (int i = -1 * radius; i <= radius; i++) { for (int j = -1 * radius; j <= radius; j++) { Block currentBlock; - Vector3 currentPos = new Vector3(respawnLocation.x + i, respawnLocation.y, respawnLocation.z); + Vector3 currentPos = new Vector3(respawnLocation.x + i, respawnLocation.y, respawnLocation.z + j); + + //Debug.Log("Checking position " + currentPos); - if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock) //Does a block exist here? - && currentBlock.is_Walkable //Are we allowed on this block? - && !(currentBlock.isWater) && !(currentBlock.isPit) //Don't respawn on top of instant traps + if (Block.isBlockAtPosition(currentPos, 1, ~Ignore, out currentBlock)) + { + /*Debug.Log("Found block at " + currentPos + + ". is_Walkable = " + currentBlock.is_Walkable + + ", currentBlock is Water = " + (currentBlock is Water) + + ", isPit = " + currentBlock.isPit + + ", no currentPlayer = " + (currentBlock.CurrentPlayer == null));*/ + + if (currentBlock.is_Walkable //Are we allowed on this block? + && !(currentBlock is Water) && !(currentBlock.isPit) //Don't respawn on top of instant traps && currentBlock.CurrentPlayer == null) //Block must be unoccupied + { + respawnBlocks.Add(currentBlock); + //Debug.Log("Added block at " + currentPos); + } + else + { + //Debug.Log("Block at " + currentPos + " invalid"); + } + } + else { - respawnBlocks.Add(currentBlock); + //Debug.Log("No block at " + currentPos); } } } } while (respawnBlocks.Count < respawningPlayers.Count); + //Debug.Log("Possible respawn spots: "); + + /*foreach(Block block in respawnBlocks) + { + Debug.Log(block.transform.position); + }*/ + foreach (PlayerData player in respawningPlayers) { //We randomly pick a block for them to respawn on - Block respawnBlock = respawnBlocks[(int)Random.Range(0.0f, (float)respawnBlocks.Count)]; - Vector3 respawnLocationPlayer = respawnBlock.transform.position; - player.character.respawnCharacter(respawnLocationPlayer); + int respawnIndex = (int)Random.Range(0.0f, (float)respawnBlocks.Count); + //Debug.Log("Respawn index = " + respawnIndex); + Block respawnBlock = respawnBlocks[respawnIndex]; + //Debug.Log("Respawn location = " + respawnBlock.transform.position); + player.character.respawnCharacter(respawnBlock); respawnBlocks.Remove(respawnBlock); //Then we remove it from the list for the next player } diff --git a/Assets/Scripts/LevelBlocks/Block.cs b/Assets/Scripts/LevelBlocks/Block.cs index 23806b3..5f7ed92 100644 --- a/Assets/Scripts/LevelBlocks/Block.cs +++ b/Assets/Scripts/LevelBlocks/Block.cs @@ -41,7 +41,7 @@ public class Block : MonoBehaviour /// /// List of current players on this block /// - public Character CurrentPlayer { get; protected set; } + public Character CurrentPlayer { get; set; } private Renderer renderer; #endregion Private Functions