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

@ -686,30 +686,23 @@ public virtual void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage e
NetworkServer.AddPlayerForConnection(conn, player); NetworkServer.AddPlayerForConnection(conn, player);
} }
public Transform GetStartPosition() 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)
{ return null;
// 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]; if (playerSpawnMethod == PlayerSpawnMethod.Random)
startPositionIndex += 1; return startPositions[UnityEngine.Random.Range(0, startPositions.Count)];
return startPos; else
} {
return null; 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)
{ {