Believe it or not, we never implemented an in-game way of showing which team won when the game is over. This has created some confusion at the QA labs because when the game finishes, nothing happens. You can imagine that people get confused when it appears there are no more enemies left, but they can still run around.
I have spent this week working on getting a win/lose screen to show up when the game is over. To accomplish this we need to:
- Check to see if there are lives remaining on only 1 team
- Check to see if there are existing players
- Ensure that the remaining players are all on the same team.
It’s pretty easy doing the first step, I send an RPC to the server, and the server checks the remaining lives array. Checking the players is not easy though. When the server creates a player we store the data in a structure that holds a players team color, and the players NetworkPlayer. This last part is key for us because we can then do a search for all game objects that are networked players and see if their NetworkView has this stored player. We can filter for the team because the structure contains the team color.
Having this information on hand, I was able to check for all existing players and if they were found, update a counter for each team to count all the players on the screen for each team. I then checked these final counts, and if there is only one that has a count greater than zero, then that team wins.
To each NetworkView connected to the server, I send out the team that won, and each client then handles which image to show on the screen.
I did encounter a problem with tracking the players. I check if the game is done every time a player is supposed to be removed from the game (because they died, or were vacuumed up). This is achieved by sending an RPC to the server that tells it a player died and it should check for gameover. The problem is that the RPC gets sent before the player is actually removed. To counter this problem, I create a co-routine with a delay of 2 seconds before sending the RPC this way I can ensure that the player is removed before the server checks for gameover.