refactor: Simplify GetStartPosition (#828)

* Simplify GetStartPosition

- Early out if startPositions.Count = 0
- Reduces the normal returns to single LOC each
- Only resets startPositionIndex if it reaches int.MaxValue
- Uses modulo and increment-after against count to pick round robin index
- Eliminates unnecessary last return that could never be reached

* Per Paul's request
This commit is contained in:
MrGadget 2019-04-18 16:08:44 -04:00 committed by Paul Pacheco
parent ec5b735f4a
commit bcd3424c0e

View File

@ -691,24 +691,17 @@ public Transform GetStartPosition()
// first remove any dead transforms // first remove any dead transforms
startPositions.RemoveAll(t => t == null); startPositions.RemoveAll(t => t == null);
if (playerSpawnMethod == PlayerSpawnMethod.Random && startPositions.Count > 0) if (startPositions.Count == 0)
{
// try to spawn at a random start location
int index = UnityEngine.Random.Range(0, startPositions.Count);
return startPositions[index];
}
if (playerSpawnMethod == PlayerSpawnMethod.RoundRobin && startPositions.Count > 0)
{
if (startPositionIndex >= startPositions.Count)
{
startPositionIndex = 0;
}
Transform startPos = startPositions[startPositionIndex];
startPositionIndex += 1;
return startPos;
}
return null; return null;
if (playerSpawnMethod == PlayerSpawnMethod.Random)
return startPositions[UnityEngine.Random.Range(0, startPositions.Count)];
else
{
Transform startPosition = startPositions[startPositionIndex];
startPositionIndex = (startPositionIndex + 1) % startPositions.Count;
return startPosition;
}
} }
public virtual void OnServerRemovePlayer(NetworkConnection conn, NetworkIdentity player) public virtual void OnServerRemovePlayer(NetworkConnection conn, NetworkIdentity player)