// 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();
                    Application.LoadLevel(Application.loadedLevel);
                    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>
        /// Raises the new game state event.
        /// </summary>
        /// <param name="newState">New state of the game.</param>
        /// <param name="changes">Changes received from the room</param>
        public void OnNewGameState(
            GameManager.GameStateData newState,
            List<ItemState> changes)
        {
            if (newState != null)
            {
                InitGame(newState);

                // record the game state if we are starting out or on a new
                // level.
                if (newState.CurrentPhase == GameManager.GameStateData.Phase.Connecting ||
                    newState.CurrentPhase == GameManager.GameStateData.Phase.Initializing)
                {
                    owner.GameState = newState;
                }
            }

            if (changes != null)
            {
                ProcessChanges(changes);
            }
        }
 public LocalGameManager(GameManager owner)
 {
     this.owner = owner;
 }
        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;
                        }
                    }
                }
            }
        }
 public RemoteGameManager(GameManager owner)
 {
     this.owner = owner;
 }
        // Awake is always called before any Start functions
        internal void Awake()
        {
            // Check if instance already exists
            if (instance == null)
            {
                // if not, set instance to this
                instance = this;
            }
            else if (instance != this)
            {
                // If instance already exists and it's not this:
                // Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
                Destroy(gameObject);
            }

            // Sets this to not be destroyed when reloading scene
            DontDestroyOnLoad(gameObject);

            // Get a component reference to the attached BoardManager script
            levelmanager = GetComponent<LevelManager>();
        }