예제 #1
0
        public void SetGameState(NWGameStateInfo _gameState)
        {
            if (_gameState == null)
            {
                Console.LogError(ConsoleFilter.GameController, "Update GameStateInfo is null");
                return;
            }

            if (Util.ArrayIsNullOrEmpty(_gameState.Teams))
            {
                Console.LogError(ConsoleFilter.GameController, "Update GameStateInfo  Teams is null");
                return;
            }
            foreach (NWTeamInfo team in _gameState.Teams)
            {
                if (!teams.ContainsKey(team.Id))
                {
                    Console.LogErrorFormat(ConsoleFilter.GameController, "Team {0} is not Containce", team.Id);
                    continue;
                }
                teams[team.Id].Resources = team.Resources;
                teams[team.Id].Units     = team.Units;
                teams[team.Id].Builds    = team.Builds;
                teams[team.Id].Turrets   = team.Turrets;
            }

            if (!Util.ArrayIsNullOrEmpty(_gameState.Units))
            {
                foreach (NWUnit serverUnit in _gameState.Units)
                {
                    Unit unit = null;
                    if (UnitController.TryGetUnit(serverUnit.Id, out unit) && unit != null)
                    {
                        unit.transform.position = Util.ConvertRTSStructToVector3(serverUnit.trasform);
                    }
                }
            }
            if (!Util.ArrayIsNullOrEmpty(_gameState.Builds))
            {
                foreach (NWBuild serverBuild in _gameState.Builds)
                {
                    BuildController.UpdateBuild(serverBuild);
                }
            }

            //add and Update Turrets
            int[]         turretsId = BuildController.GetAllTurretsId();
            HashSet <int> Turrets   = new HashSet <int>(turretsId);

            int[] serverTurretsId = Util.ArrayIsNullOrEmpty(_gameState.Turrets) ?
                                    new int[0] :
                                    Array.ConvertAll(_gameState.Turrets, x => x.Id);

            HashSet <int> ServerTurrets = new HashSet <int>(serverTurretsId);
            HashSet <int> removeTurrets = new HashSet <int>(Turrets);

            removeTurrets.ExceptWith(ServerTurrets);

            foreach (NWTurret nwTurret in _gameState.Turrets)
            {
                int     id     = nwTurret.Id;
                ITurret turret = null;
                if (BuildController.TryGetTurret(id, out turret) && turret != null)
                {
                    turret.UpdateInfo(nwTurret);
                }
                else
                {
                    BuildController.AddTurret(nwTurret);
                }
            }

            foreach (int turretId in removeTurrets)
            {
                BuildController.RemoveTurret(turretId);
            }
            commandQueue.Enqueue(() =>
            {
                UI.SetMainPlayerTeamInfo(teams[MainPlayerController.TeamId]);
            });
        }
예제 #2
0
        public void SetStartGameState(NWGameStateInfo _state)
        {
            if (_state == null)
            {
                Console.LogError(ConsoleFilter.GameController, "Start GameStateInfo is null");
                return;
            }

            if (Util.ArrayIsNullOrEmpty(_state.Teams))
            {
                Console.LogError(ConsoleFilter.GameController, "Start GameStateInfo  Teams is null");
                return;
            }
            foreach (NWTeamInfo team in _state.Teams)
            {
                if (teams.ContainsKey(team.Id))
                {
                    Console.LogErrorFormat(ConsoleFilter.GameController, "Dublicate Team {0}", team.Id);
                    continue;
                }
                teams.Add(team.Id, team);
            }


            if (!Util.ArrayIsNullOrEmpty(_state.Units))
            {
                foreach (NWUnit unit in _state.Units)
                {
                    UnitController.AddUnit(unit);
                }
            }

            if (!Util.ArrayIsNullOrEmpty(_state.Builds))
            {
                foreach (NWBuild build in _state.Builds)
                {
                    if (build == null)
                    {
                        Console.LogError(ConsoleFilter.MainController, "ServerBuild  is null");
                        continue;
                    }
                    BuildController.AddBuild(build);
                }
            }

            if (!Util.ArrayIsNullOrEmpty(_state.Turrets))
            {
                foreach (NWTurret turret in _state.Turrets)
                {
                    if (turret == null)
                    {
                        Console.LogError(ConsoleFilter.MainController, "ServerTurret is null");
                    }
                    BuildController.AddTurret(turret);
                }
            }

            if (gameControllers.SwichScreenController != null)
            {
                gameControllers.SwichScreenController.LoadScreenDone();
            }

            commandQueue.Enqueue(() =>
            {
                if (teams.ContainsKey(MainPlayerController.TeamId))
                {
                    UI.SetMainPlayerTeamInfo(teams[MainPlayerController.TeamId]);
                }
            });
        }