Browse Source

Fixed respawning-related issues (I hope). Players should now respawn only on open squares and never on traps.

master
s3607057 (Angus Niven) 5 years ago
parent
commit
e08ae38e70
3 changed files with 55 additions and 74 deletions
  1. +9
    -64
      Assets/Scripts/Character.cs
  2. +45
    -9
      Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs
  3. +1
    -1
      Assets/Scripts/LevelBlocks/Block.cs

+ 9
- 64
Assets/Scripts/Character.cs View File

@ -218,80 +218,25 @@ public class Character : MonoBehaviour
public void respawnCharacter() 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 //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.respawnNeeded = false;
this._currentBlock = currentBlock;
this._currentBlock = respawnPosition;
respawnPosition.CurrentPlayer = this;
this.stuck = false;
Debug.Log("Moved " + this.name + " to " Debug.Log("Moved " + this.name + " to "
+ this.transform.position.x + ", " + this.transform.position.x + ", "
+ this.transform.position.y + ", " + this.transform.position.y + ", "
+ this.transform.position.z + ", " + this.transform.position.z + ", "
+ " respawnNeeded = " + respawnNeeded);
+ ", respawnNeeded = " + respawnNeeded
+ ", stuck = " + this.stuck);
} }
else else
{ {

+ 45
- 9
Assets/Scripts/GameMode/ColorGameMode/RacetrackGameMode.cs View File

@ -85,9 +85,11 @@ public class RacetrackGameMode : GameMode
//If there are any players left, it will be the average of their positions //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 //Otherwise, it will be as close to the centre of the screen as possible
Vector3 respawnLocation = new Vector3(0.0f, -0.5f, 0.0f); 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 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 //We list the positions of all the surviving players
List<Vector3> survivingPlayerLocations = new List<Vector3>(); List<Vector3> survivingPlayerLocations = new List<Vector3>();
foreach (PlayerData player in survivingPlayers) foreach (PlayerData player in survivingPlayers)
@ -101,19 +103,23 @@ public class RacetrackGameMode : GameMode
} }
respawnLocation = respawnLocation / survivingPlayers.Count; respawnLocation = respawnLocation / survivingPlayers.Count;
//Debug.Log("Averaged survivor locations, new respawn location: " + respawnLocation);
} }
else //Otherwise, we go for the middle of the screen 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 //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; respawnLocation.x = mapManager.startX;
Vector3 respawnLocationVP = Camera.main.WorldToViewportPoint(respawnLocation); Vector3 respawnLocationVP = Camera.main.WorldToViewportPoint(respawnLocation);
while (respawnLocationVP.x &gt; 0.5f || respawnLocationVP.y &gt; 0.5f)
while (respawnLocationVP.x &lt; 0.5f || respawnLocationVP.y &lt; 0.5f)
{ {
respawnLocation.x += 1.0f; respawnLocation.x += 1.0f;
respawnLocationVP = Camera.main.WorldToViewportPoint(respawnLocation); 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 //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<Block>(); respawnBlocks = new List<Block>();
radius++; radius++;
//Debug.Log("Finding respawn blocks at radius = " + radius);
for (int i = -1 * radius; i <= radius; i++) for (int i = -1 * radius; i <= radius; i++)
{ {
for (int j = -1 * radius; j <= radius; j++) for (int j = -1 * radius; j <= radius; j++)
{ {
Block currentBlock; 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 && 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); } 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) foreach (PlayerData player in respawningPlayers)
{ {
//We randomly pick a block for them to respawn on //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 respawnBlocks.Remove(respawnBlock); //Then we remove it from the list for the next player
} }

+ 1
- 1
Assets/Scripts/LevelBlocks/Block.cs View File

@ -41,7 +41,7 @@ public class Block : MonoBehaviour
/// <summary> /// <summary>
/// List of current players on this block /// List of current players on this block
/// </summary> /// </summary>
public Character CurrentPlayer { get; protected set; }
public Character CurrentPlayer { get; set; }
private Renderer renderer; private Renderer renderer;
#endregion Private Functions #endregion Private Functions

Loading…
Cancel
Save