Beispiel #1
0
        /// <summary>
        /// Creates the room.
        /// </summary>
        /// <param name="roomName">Room name to use</param>
        /// <param name="joinDuringGame">If set to <c>true</c> players can join
        /// after the game has started.</param>
        /// <param name="autoconnect">If set to <c>true</c> players are automatically
        /// connected to the room, otherwise they need to be explicitly accepted.</param>
        internal void CreateRoom(
            string roomName,
            bool joinDuringGame,
            bool autoconnect)
        {
            NearbyRoom room = NearbyRoom.CreateRoom(roomName);

            room.AutoJoin   = autoconnect;
            room.AlwaysOpen = joinDuringGame;

            // Store off the room in with the players. The game manager
            // will pick up the room and start processing messages.
            GameManager.Instance.Room = room;

            // if you can join in progress and it is autoconnect, setup the
            // room and start it, no need to wait.
            if (joinDuringGame && autoconnect)
            {
                Debug.Log("Starting Game!");

                PlayerInfo.AddPendingPlayer(
                    new NearbyPlayer(localPlayerName),
                    localAvatarIndex);

                GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerLocal);
            }
            else
            {
                statusMsg = "Waiting for players...";
                GameManager.Instance.Room.WaitForPlayers(OnPlayerFound);
                Debug.Log(statusMsg);
                ShowLobbyPanel();
            }
        }
        /// <summary>
        /// New player is connecting to the game
        /// </summary>
        /// <param name="player">Player.</param>
        /// <param name="data">Data.</param>
        internal void OnPlayerConnecting(string endpointId, byte[] data)
        {
            PlayerInfo p = PlayerInfo.AddPendingPlayer(endpointId, null, data);

            GameObject obj = owner.LevelManager.CreatePlayer(
                p.AvatarIndex,
                p.DeviceId);

            PlayerController ctl = obj.GetComponent <PlayerController>();

            if (ctl != null)
            {
                ctl.Player            = p;
                ctl.BroadcastMovement = true;

                // force the  movement message so the remote player moves to the
                // assigned, random position.
                ItemState state = new ItemState();
                state.Enabled     = obj.activeSelf;
                state.Name        = obj.name;
                state.Position    = obj.transform.position;
                state.PrefabIndex = p.AvatarIndex;
                state.Rotation    = obj.transform.rotation;
                state.TileSetName = ItemState.PlayerTileSet;
                owner.LevelData.Add(state);
            }

            owner.CreatePlayerScorePanel(p);
        }
Beispiel #3
0
 /// <summary>
 /// Starts the discovery of rooms.
 /// </summary>
 internal void StartDiscovery()
 {
     statusMsg = "Looking for Games...";
     PlayerInfo.AddPendingPlayer(
         new NearbyPlayer(localPlayerName), localAvatarIndex);
     NearbyRoom.FindRooms(OnRoomFound);
     ShowLobbyPanel();
 }
Beispiel #4
0
        /// <summary>
        /// Called when a player is requesting to join a room.
        /// </summary>
        /// <param name="player">Player.</param>
        /// <param name="data">Data.</param>
        internal void OnPlayerFound(string endpointId, byte[] data)
        {
            PlayerInfo player = PlayerInfo.AddPendingPlayer(endpointId, null, data);
            GameObject obj    = Instantiate(itemChoicePrefab) as GameObject;

            obj.transform.SetParent(lobbyListArea.transform, false);
            obj.GetComponentInChildren <Text> ().text = player.Player.Name;
            Toggle t = obj.GetComponentInChildren <Toggle>();

            t.gameObject.name = player.DeviceId;
            t.isOn            = true;
        }
Beispiel #5
0
        // Initializes the game for each level.
        internal void InitGame(GameManager.GameStateData data)
        {
            if (data != null)
            {
                Debug.Log("Initializing using " + data.CurrentPhase + ": " +
                          data.SerialNumber);

                // if we are moving to the next level of the game, reload!
                if (owner.Level != data.Level)
                {
                    Debug.Log("Going from level " + owner.Level + " to " +
                              data.Level);
                    owner.GameState = data;
                    owner.LevelManager.DestroyBoard();
                    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
                    return;
                }

                // Call the SetupScene function of the BoardManager script,
                // pass it current level number.
                owner.LevelManager.SetupScene(data.Level, data.LevelData);

                // associate each player with the player state received.
                foreach (PlayerInfo.PlayerData p in data.Players)
                {
                    PlayerInfo player = PlayerInfo.GetPlayer(p.DeviceId);
                    if (player != null)
                    {
                        player.DataState.AvatarIndex = p.AvatarIndex;
                        player.DataState.Name        = p.Name;
                        player.Score = p.score;
                    }
                    else
                    {
                        // the endpoint is not known for players that are not local
                        // so make one up that is unique.
                        player = PlayerInfo.AddPendingPlayer(
                            new NearbyPlayer(
                                p.DeviceId,
                                "unknown" + p.DeviceId,
                                p.Name),
                            p.AvatarIndex);
                    }

                    owner.CreatePlayerScorePanel(player);
                }
            }
        }
        /// <summary>
        /// Starts single player game play.
        /// </summary>
        public void Play()
        {
            // read player preferences for the name and avatar.
            string defaultPlayerName = PlayerPrefs.GetString(Multiplayer.PlayerNameKey);

            if (defaultPlayerName == null)
            {
                defaultPlayerName = "Me";
            }

            int avatarIndex = PlayerPrefs.GetInt(Multiplayer.AvatarIndexKey, 0);

            PlayerInfo.AddPendingPlayer(
                new NearbyPlayer(defaultPlayerName), avatarIndex);

            GameManager.Instance.StartPlaying(GameManager.GameType.SinglePlayer);
        }