// called in server to start the game spawning the initial units private void SpawnUnits() { List <SpawnPoint> spawnPoints = JuloFind.allWithComponent <SpawnPoint>(); foreach (SpawnPoint sp in spawnPoints) { int role = sp.playerId; if (playerMap.ContainsKey(role)) { PlayerWrapper wrapper = playerMap[role]; DualGamePlayer player = wrapper.player; if (role >= 0 && role < numPlayers) { SpawnUnit(player, sp.unitId, sp.transform); } else { Debug.LogWarningFormat("Invalid role"); } } else { Debug.LogWarningFormat("Ignoring unit for player {0}", role); } } }
// up one place public void PlayerUp(DualGamePlayer player) { if (player != playerMap[player.role].player) { Debug.Log("Warning unexpected"); } int role = player.role; if (role == 0) { Debug.LogWarning("Cannot up"); } else if (role - 1 < currentMaxPlayers) { if (playerMap.ContainsKey(role - 1)) { SwitchRoles(role, role - 1); } else { ChangeRole(role, role - 1); } } else { bool done = false; for (int i = role - 1; i >= currentMaxPlayers; i--) { if (playerMap.ContainsKey(i)) { SwitchRoles(role, i); done = true; break; } } if (!done) { if (playerMap.ContainsKey(currentMaxPlayers - 1)) { SwitchRoles(role, currentMaxPlayers - 1); } else { ChangeRole(role, currentMaxPlayers - 1); } } } }
// called on server to add already created local players public void AddHostedPlayer(/*short playerControllerId, */ DualGamePlayer player) { if (state != DNMState.Off) { Debug.LogError("Invalid state"); return; } player.role = cachedLocalPlayers.Count; if (cachedLocalPlayers.Count == 0) { mainPlayer = player; } cachedLocalPlayers.Add(player); }
// down one place public void PlayerDown(DualGamePlayer player) { if (player != playerMap[player.role].player) { Debug.Log("Warning unexpected"); } int role = player.role; if (role + 1 < currentMaxPlayers) { if (playerMap.ContainsKey(role + 1)) { SwitchRoles(role, role + 1); } else { ChangeRole(role, role + 1); } } else { bool done = false; for (int i = role + 1; i <= maximumSpectatorRole; i++) { if (playerMap.ContainsKey(i)) { SwitchRoles(role, i); done = true; break; } } if (!done) { ChangeRole(role, maximumSpectatorRole + 1); maximumSpectatorRole++; } } }
public PlayerWrapper(int connectionId, short playerControllerId, DualGamePlayer player) { this.connectionId = connectionId; this.playerControllerId = playerControllerId; this.player = player; }
protected virtual void SpawnUnit(DualGamePlayer owner, int unitId, Transform location) { Debug.LogWarning("Should override this"); }
public virtual void OnRoleChanged(DualGamePlayer player, int oldRole) { }
public virtual void OnPlayerRemoved(DualGamePlayer player) { }
/*********** DUAL CLIENT METHODS ***********/ public virtual void OnPlayerAdded(DualGamePlayer player) { }
// called on server when a client requests to add a player for it // TODO should receive a message with initial name/color public override void OnServerAddPlayer(NetworkConnection connectionToClient, short playerControllerId, NetworkReader extraMessage) { string playerName = "Guest"; if (extraMessage != null) { var s = extraMessage.ReadMessage <NewPlayerMessage>(); playerName = s.playerName; } else { Debug.LogWarning("Mo message"); } if (state != DNMState.Host) { Debug.LogErrorFormat("Invalid state: {0}", state); return; } DualGamePlayer newPlayer = null; bool isLocal = (connectionToClient.connectionId == 0); if (isLocal) { // if the player is local it should be cached if (gameState != GameState.NoGame) { Debug.LogErrorFormat("Invalid state: {0}", gameState); } else if (playerControllerId < cachedLocalPlayers.Count) { newPlayer = cachedLocalPlayers[playerControllerId]; } else { JuloDebug.Err("Player " + playerControllerId + " should be cached"); //return; } } else { // if is remote should be created if (connectionToClient.playerControllers.Count > 0) { Debug.LogWarning("Already a player for that connection"); return; } bool isPlaying = gameState != GameState.NoGame; newPlayer = CreatePlayer(connectionToClient.connectionId, playerControllerId); newPlayer.playerName = playerName; int newRole = -1; if (isPlaying || joinAsSpectator) { // join as spectator newRole = maximumSpectatorRole + 1; } else { // try to join as player bool assigned = false; // try to enter as player for (int i = 0; !assigned && i < currentMaxPlayers; i++) { if (!playerMap.ContainsKey(i)) { newRole = i; assigned = true; } } if (!assigned) { newRole = maximumSpectatorRole + 1; } } maximumSpectatorRole = Math.Max(maximumSpectatorRole, newRole); newPlayer.role = newRole; } if (newPlayer != null) { PlayerWrapper wrapper = new PlayerWrapper(connectionToClient.connectionId, playerControllerId, newPlayer); //Debug.LogFormat("ADDING {0}: {1}-{2}", newRole, connectionToClient.connectionId, playerControllerId); playerMap.Add(newPlayer.role, wrapper); newPlayer.gameObject.SetActive(true); NetworkServer.AddPlayerForConnection(connectionToClient, newPlayer.gameObject, playerControllerId); } else { JuloDebug.Warn("Player could not be added: " + connectionToClient.connectionId + ":" + playerControllerId); } }