Example #1
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);
                }
            }
        }
        public void OnNewGameState(GameManager.GameStateData newState, List <ItemState> changes)
        {
            if (newState != null)
            {
                // we don't do anything with game state we receive since we
                // are the local game manager which is the source of truth for
                // the game state.
                Debug.Log("Got new game state: " + newState);
            }

            if (changes != null && changes.Count > 0)
            {
                Debug.Log("Got New changes from Remote! with " + changes.Count);

                // Only change the players and scores
                foreach (ItemState item in changes)
                {
                    Debug.Log("Processing  ----> " + item.Name);
                    GameObject obj = owner.LevelManager.Find(item.Name);
                    if (obj != null)
                    {
                        PlayerController ctl = obj.GetComponent <PlayerController>();

                        // only move players - enemies are managed locally.
                        if (ctl != null && !ctl.Player.IsLocal)
                        {
                            ctl.MoveTo(item.Position);
                        }
                    }
                    else if (item.Name.StartsWith(GameManager.ScoreChangedItemName))
                    {
                        PlayerInfo p = PlayerInfo.GetPlayer(item.TileSetName);
                        if (p != null)
                        {
                            p.Score = (p.Score > item.PrefabIndex) ? p.Score : item.PrefabIndex;
                        }
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Enters the room from the lobby.
        /// </summary>
        public void EnterRoom()
        {
            if (joining)
            {
                // get the room that we should join.
                Toggle[] rooms = lobbyListArea.GetComponentsInChildren <Toggle>();
                foreach (Toggle t in rooms)
                {
                    if (t.isOn)
                    {
                        GameManager.Instance.Room = NearbyRoom.LookupRoomByEndpoint(t.name);
                        break;
                    }
                }

                if (GameManager.Instance.Room != null)
                {
                    GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerRemote);
                }
                else
                {
                    statusMsg = "You must select a room to start!";
                }

                return;
            }
            else
            {
                if (!GameManager.Instance.Room.AutoJoin)
                {
                    // loop over players and remove unchecked players
                    Toggle[] players = lobbyListArea.GetComponentsInChildren <Toggle>();
                    Debug.Log("Checking " + players.Length + " players");
                    foreach (Toggle t in players)
                    {
                        if (!t.isOn)
                        {
                            Debug.Log("Removing " + t.gameObject.name);
                            PlayerInfo.RemovePendingPlayer(t.gameObject.name);
                        }
                        else
                        {
                            Debug.Log("Accepting " + t.gameObject.name);
                            PlayerInfo p = PlayerInfo.GetPlayer(t.gameObject.name);
                            if (p != null)
                            {
                                GameManager.Instance.Room.AcceptRequest(p.Player.EndpointId);
                            }
                            else
                            {
                                Debug.LogError("Cannot find NearbyPlayer for " +
                                               t.gameObject.name);
                            }
                        }
                    }
                }

                if (!GameManager.Instance.Room.AlwaysOpen)
                {
                    GameManager.Instance.Room.CloseRoom();
                }

                Debug.Log("Starting Game!");

                GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerLocal);
            }
        }
Example #4
0
        /// <summary>
        /// Processes the changes received from the room.
        /// </summary>
        /// <param name="changes">Changes in item state to proces.</param>
        internal void ProcessChanges(List <ItemState> changes)
        {
            Debug.Log("Processing " + changes.Count + " changes!");
            foreach (ItemState item in changes)
            {
                Debug.Log("---> " + item.Name);
                GameObject obj = owner.LevelManager.Find(item.Name);
                if (obj != null)
                {
                    // use the moving object so we get smooth movement.
                    MovingObject moving = obj.GetComponent <MovingObject>();

                    if (moving != null && !moving.BroadcastMovement)
                    {
                        moving.MoveTo(item.Position);
                    }
                    else
                    {
                        obj.transform.position = item.Position;
                    }

                    obj.transform.rotation = item.Rotation;

                    // handle enemies in a clever way to get the animation.
                    EnemyController enemy = obj.GetComponent <EnemyController>();
                    if (enemy != null && obj.activeSelf && obj.activeSelf != item.Enabled)
                    {
                        enemy.Explode();
                    }
                    else
                    {
                        obj.SetActive(item.Enabled);
                    }
                }
                else if (item.Name == GameManager.PlayerTurnFlagName)
                {
                    // toggle the turn flag.
                    if (item.Enabled)
                    {
                        foreach (PlayerInfo p in PlayerInfo.AllPlayers)
                        {
                            if (p.IsLocal)
                            {
                                p.Moved = false;
                            }
                        }
                    }
                }
                else if (item.Name.StartsWith(GameManager.ScoreChangedItemName))
                {
                    // update the scores
                    PlayerInfo p = PlayerInfo.GetPlayer(item.TileSetName);
                    if (p != null)
                    {
                        p.Score = (p.Score > item.PrefabIndex) ? p.Score : item.PrefabIndex;
                    }
                }
                else
                {
                    // create new objects that showed up
                    // since the scene was initialized.
                    Debug.Log("Creating " + item.Name);
                    GameObject newObj = owner.LevelManager.CreateItem(item);
                    if (newObj.GetComponent <PlayerController>() != null)
                    {
                        string     devId = newObj.name.Replace(ItemState.PlayerTileSet, string.Empty);
                        PlayerInfo p     = PlayerInfo.GetPlayer(devId);
                        if (p != null)
                        {
                            if (newObj.GetComponent <PlayerController>().Player == null)
                            {
                                newObj.GetComponent <PlayerController>().Player            = p;
                                newObj.GetComponent <PlayerController>().BroadcastMovement = p.IsLocal;
                            }
                        }
                    }
                }
            }
        }