/// <summary> /// Instantiate the base components of a game. /// </summary> /// <param name="port">The port to open the GameNetworkServer on.</param> protected GameBase(NetworkBase network) { gameStatesToCommit = new List<GameData>(); gameStatesToSend = new List<GameData>(); players = new Player[MAX_PLAYERS]; // Defaults to null elements (unlike C, you don't have to set the elements) GameState = new State(); Network = network; GameState.registerStatePhaseChange(this.OnStatePhaseChange); GameState.registerTransformChange(this.OnTransformChange); GameState.RegisterEntityStateChange(this.OnEntityStateChange); return; }
/// <summary> /// A Player has joined, set them up in the system. /// </summary> /// <param name="gameData">The game data related to the character joining the session.</param> protected override void OnPlayerConnect(GameData gameData) { GameData response; Player player = Game.ConnectionIDToPlayer(gameData.ConnectionInfo); switch ((GameData.ConnectionDetails)gameData.EventDetail) { case GameData.ConnectionDetails.IdReqest: if (player == null) // If this is a new player, assign them a new ID, otherwise just resend the old id { player = new Player(gameData.ConnectionInfo, Game); } response = new GameData(GameData.GameDataType.Connect, player.PlayerID, (int)GameData.ConnectionDetails.IdReqest); //TODO: Ask Ben Cassel, why is this broadcast? Game.Network.BroadCastGameData(response); for (int i = 0; i < Game.players.Length; ++i) { if (Game.players[i] != null) { response = new GameData(GameData.GameDataType.Connect, Game.players[i].PlayerID, (int)GameData.ConnectionDetails.Connected); Game.Network.BroadCastGameData(response); } } break; case GameData.ConnectionDetails.Disconnected: case GameData.ConnectionDetails.Dropped: if (player != null) // This is a known player based on their connection, so we can drop them { Game.UnregisterPlayer(player); response = new GameData(GameData.GameDataType.Connect,player.PlayerID,(int)GameData.ConnectionDetails.Disconnected); Game.Network.BroadCastGameData(response); } break; default: throw new ArgumentException(); } return; }
/// <summary> /// Register a new player with the game client. /// </summary> /// <param name="player">The player to register with the game client.</param> /// <returns>Returns the ID of the player to register.</returns> public override int RegisterPlayer(Player player) { players[player.PlayerID] = player; return player.PlayerID; }
/// <summary> /// Unregister a player from the game server. /// </summary> /// <param name="player">The player to remove from the game client.</param> public override void UnregisterPlayer(Player player) { availableIDs.Push(player.PlayerID); PlayerToConnectionIDMap.Remove(player); if (player.ConnectionID != null) // Don't unregister the local player as he's not in this list already { ConnectionIDToPlayerMap.Remove(player.ConnectionID); } base.UnregisterPlayer(player); return; }
/// <summary> /// Register a new player with the game server. /// </summary> /// <param name="player">The player to register with the game server.</param> /// <returns>Returns the ID of the player to register.</returns> public override int RegisterPlayer(Player player) { PlayerToConnectionIDMap.Add(player, player.ConnectionID); if (player.ConnectionID != null) // Check if we're registering the local player { ConnectionIDToPlayerMap.Add(player.ConnectionID, player); } int playerID = availableIDs.Pop(); players[playerID] = player; return playerID; }
/// <summary> /// Retrieve a connection ID from a player. /// </summary> /// <param name="player">The player to fetch a connection ID for.</param> /// <returns>A connection ID mapped to the provided player. If the player does not exist, returns null.</returns> public ConnectionID PlayerToConnectionID(Player player) { if (PlayerToConnectionIDMap.ContainsKey(player)) { return PlayerToConnectionIDMap[player]; } else { return null; } }
/// <summary> /// Unregister a player from the game. /// </summary> /// <param name="player">The player to unregister.</param> public virtual void UnregisterPlayer(Player player) { if (player != null) // Don't remove already-removed players { players[player.PlayerID] = null; } return; }
/// <summary> /// Register a player with the game and return their ID. /// </summary> /// <param name="player">The player to register.</param> /// <returns>An integer representing the new ID of the registered player.</returns> public abstract int RegisterPlayer(Player player);