예제 #1
0
        public override void OnServerAddPlayer(NetworkConnection connection)
        {
            if (_polePositionManager.MaxNumPlayers == _polePositionManager.Players.Count ||
                _polePositionManager.InRace)
            {
                connection.Disconnect();
            }
            else
            {
                Transform  startPos = GetStartPosition();
                GameObject player   = startPos != null
                    ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                    : Instantiate(playerPrefab);

                PlayerInfo playerInfo = player.GetComponent <PlayerInfo>();
                playerInfo.ID              = connection.connectionId;
                playerInfo.Name            = string.Format("Player {0}", connection.connectionId);
                playerInfo.Color           = ColorPicker.GetRandomColor();
                playerInfo.CurrentPosition = startPositionIndex;
                playerInfo.CurrentLap      = 0;
                playerInfo.NumberOfLaps    = _polePositionManager.NumberOfLaps;

                NetworkServer.AddPlayerForConnection(connection, player);

                _polePositionManager.AddPlayer(playerInfo);
            }
        }
예제 #2
0
 /// <summary>
 /// Called on every NetworkBehaviour when it is activated on a client.
 /// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
 /// </summary>
 public override void OnStartClient()
 {
     base.OnStartClient();
     m_PlayerInfo.ID         = m_ID;
     m_PlayerInfo.CurrentLap = 0;
     m_PolePositionManager.AddPlayer(m_PlayerInfo);
 }
예제 #3
0
    /// <summary>
    /// This is invoked for NetworkBehaviour objects when they become active on the server.
    /// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
    /// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
    /// </summary>
    ///

    /* OnStartServer(): Este método es un override del OnStartServer() de NetworkBehaviour.
     * Si es serverOnly añade el player a la lista de m_players de PolePositionManager. También se actualiza el valor de m_ID a connectionToClient.connectionId - 1 pues el serverOnly se cuenta a sí mismo como connectionToClient.connectionId
     * Si no es serverOnly se activa el botón para que el host pueda modificar el número de vueltas
     * */
    public override void OnStartServer()
    {
        base.OnStartServer();
        if (isServerOnly)
        {
            if (!m_PolePositionManager.started && !m_PolePositionManager.full)
            {
                m_PolePositionManager.AddPlayer(m_PlayerInfo);
            }
            m_ID = connectionToClient.connectionId - 1;
        }
        else
        {
            m_ID = connectionToClient.connectionId;
            m_UIManager.buttonLaps.gameObject.SetActive(true);
        }
    }
예제 #4
0
 /// <summary>
 /// Called when the local player object has been set up.
 /// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.</para>
 /// </summary>
 public override void OnStartLocalPlayer()
 {
     playerName              = m_NetworkManager.name;
     m_PlayerInfo.ID         = m_ID;
     m_PlayerInfo.CurrentLap = 0;
     m_PlayerInfo.Color      = m_UIManager.color;
     m_PolePositionManager.AddPlayer(m_PlayerInfo);
 }
예제 #5
0
    /// <summary>
    /// This is invoked for NetworkBehaviour objects when they become active on the server.
    /// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
    /// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
    /// </summary>
    public override void OnStartServer()
    {
        base.OnStartServer();
        // Mutex para los jugadores que estén intentando entrar al mismo tiempo que un jugador trata de ponerse en listo
        m_PolePositionManager.mutex.WaitOne();
        m_ID = connectionToClient.connectionId;
        if (!m_PolePositionManager.gameStarted)
        {
            // Se busca una posición disponible para colocar al jugador, y si se encuentra se inicializa y se añade a la lista
            for (int i = 0; i < m_PolePositionManager.playersConnected.Length; i++)
            {
                if (!m_PolePositionManager.playersConnected[i])
                {
                    m_PlayerInfo.ID         = i;
                    m_PlayerInfo.CurrentLap = 0;
                    if (isClient)
                    {
                        m_UIManager.readyButton.onClick.AddListener(() => PlayerReady());
                        m_UIManager.changeColorButton.onClick.AddListener(() => ChangeColor());
                        if (isLocalPlayer)
                        {
                            m_UIManager.ActivateSelectHUD();
                        }
                    }

                    this.gameObject.transform.position = NetworkManager.startPositions[i].position;
                    this.gameObject.transform.rotation = NetworkManager.startPositions[i].rotation;
                    m_PolePositionManager.AddPlayer(m_PlayerInfo);

                    m_PolePositionManager.mutex.ReleaseMutex();
                    return;
                }
            }
            // Si llega aquí es porque están todos los huecos ocupados
            Debug.Log("No pueden entrar más jugadores a la partida");
        }
        else
        {
            Debug.Log("Partida Empezada");
        }
        m_PolePositionManager.mutex.ReleaseMutex();
    }
예제 #6
0
 /// <summary>
 /// Called on every NetworkBehaviour when it is activated on a client.
 /// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
 /// </summary>
 public override void OnStartClient()
 {
     base.OnStartClient();
     GetRefs();
     m_PolePositionManager.AddPlayer(m_PlayerInfo);
     if (isLocalPlayer)
     {
         //Al iniciarse un cliente, si es localPlayer, debe mandarse el nombre introducido al servidor
         m_PlayerInfo.CmdChangeName(m_UIManager.chooseNameHUD.playerName);
     }
 }
예제 #7
0
    /// <summary>
    /// Called on every NetworkBehaviour when it is activated on a client.
    /// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
    /// </summary>
    public override void OnStartClient()
    {
        base.OnStartClient();
        m_PlayerInfo.ID         = m_ID;
        m_PlayerInfo.CurrentLap = 0;
        m_PlayerInfo.Name       = m_Name;
        m_PlayerInfo.Color      = m_Color;

        GetComponentInChildren <Renderer>().materials[1].color = m_Color;
        m_PolePositionManager.AddPlayer(m_PlayerInfo);
    }
예제 #8
0
 void CmdAddPlayer(string name)
 {
     m_PlayerInfo.Name = name;
     m_PolePositionManager.AddPlayer(m_PlayerInfo);
 }