Exemplo n.º 1
0
        /// <summary>
        /// Initializes the Game with configured settings.
        /// </summary>
        private void Start()
        {
            tileFactory = TileFactory.instance;
            unitFactory = UnitFactory.instance;

            var selectedMap = SceneLoader.GetParam(SceneLoader.LOAD_MAP_PARAM);

            if (selectedMap != "")
            {
                mapData = MapLoader.instance.GetMapAsset(selectedMap);
            }
            else
            {
            }

            var gameTypeString = SceneLoader.GetParam(SceneLoader.GAME_TYPE_PARAM);

            if (gameTypeString == GameManager.SINGLEPLAYER_GAME_TYPE)
            {
                gameType = GameType.Singleplayer;
            }
            else if (gameTypeString == GameManager.MULTIPLAYER_GAME_TYPE)
            {
                gameType = GameType.Multiplayer;
            }
            else
            {
            }

            if (gameType == GameType.Singleplayer)
            {
                var singleGameTypeString = SceneLoader.GetParam(SceneLoader.SINGLEPLAYER_GAME_TYPE_PARAM);

                if (singleGameTypeString == GameManager.CAMPAIGN_GAME_TYPE)
                {
                    singleplayerGameType = SingleplayerGameType.Campaign;
                }
                else if (singleGameTypeString == GameManager.PRACTICE_GAME_TYPE)
                {
                    singleplayerGameType = SingleplayerGameType.Practice;
                }
                else
                {
                    throw new Exception("Singleplayer Game Type not set!");
                }
            }
            else if (gameType == GameType.Multiplayer)
            {
            }

            // Read map data from text file to get columns and rows
            var tileData = JsonUtility.FromJson <TileDataWrapper>(mapData.text);
            int cols     = tileData.Columns;
            int rows     = tileData.Rows;

            // Set up Players
            var players = new List <Player>();

            for (int playerNum = 1; playerNum <= numberOfPlayers; playerNum++)
            {
                Player newPlayer = null;

                if (gameType == GameType.Singleplayer)
                {
                    if (singleplayerGameType == SingleplayerGameType.Practice)
                    {
                        newPlayer = new Player($"Player {playerNum}", playerNum);
                    }
                    else if (singleplayerGameType == SingleplayerGameType.Campaign)
                    {
                        if (playerNum == 1)
                        {
                            newPlayer = CampaignManager.instance.CampaignPlayerData;
                            newPlayer.Units.Clear();
                        }
                        else
                        {
                            newPlayer = new Player($"Enemy Faction", playerNum);
                        }
                    }
                    else
                    {
                        throw new Exception($"Error setting up Player {playerNum}");
                    }
                }

                if (gameType == GameType.Multiplayer)
                {
                    // TODO: Get logic to set up Players with unique names from server?
                }

                players.Add(newPlayer);
            }

            // If playing a Singleplayer game, the user always controls the first Player (Player 1)
            if (gameType == GameType.Singleplayer)
            {
                ControllingPlayer = players.First();
            }

            // If playing a Multiplayer game, the server decides which player the user controls
            if (gameType == GameType.Multiplayer)
            {
                // TODO: Set up logic to find out which player the human in controlling
            }

            model.ConstructModel(cols, rows, players);

            var newObjectViews = new Dictionary <Vector2Int, ObjectView>();

            Tuple <Unit, GameObject> newUnitTuple = null;

            var boardTileData = tileData.tileData;
            var boardUnitData = boardTileData.Where(data => data.Player != 0).Select(data =>
                                                                                     new {
                Player       = data.Player,
                UnitPosition = new Vector3(data.Column, data.Row, 0f),
                UnitData     = data.UnitData
            }
                                                                                     );

            foreach (var boardTile in boardTileData)
            {
                // Initialize Tile Model/View
                var newTile = tileFactory.CreateTile(boardTile, view.tiles.transform);
                model.AddTile(newTile);
            }

            foreach (var boardUnit in boardUnitData)
            {
                var unitData     = boardUnit.UnitData;
                var unitPosition = boardUnit.UnitPosition;
                var unitPlayer   = boardUnit.Player;

                if (gameType == GameType.Singleplayer)
                {
                    if (singleplayerGameType == SingleplayerGameType.Campaign)
                    {
                        if (unitPlayer == 1)
                        {
                            const char DELIMITER       = '_';
                            const int  UNIT_NAME_INDEX = 3;

                            if (unitData == "FREE_ALLY_SQUARE")
                            {
                                // TODO: Make Free allies
                            }
                            else
                            {
                                var split_string = unitData.Split(DELIMITER);

                                if (split_string.Length == 4)
                                {
                                    // Named Unit
                                    var unitName     = split_string[UNIT_NAME_INDEX];
                                    var existingUnit =
                                        CampaignManager.instance.CampaignPlayerData.CampaignUnits.SingleOrDefault(
                                            campaignUnit => campaignUnit.Name == unitName);

                                    if (existingUnit != null)
                                    {
                                        var cloneUnit = existingUnit.Clone();
                                        newUnitTuple = unitFactory.ImportUnitForBoard(unitPosition, view.units.transform, cloneUnit, unitData);
                                    }
                                    else
                                    {
                                        newUnitTuple = unitFactory.CreateUnitForBoard(unitPosition, view.units.transform, unitData);
                                    }
                                }
                                else
                                {
                                    throw new NotImplementedException();
                                }
                            }
                        }
                        else
                        {
                            newUnitTuple = unitFactory.CreateUnitForBoard(boardUnit.UnitPosition, view.units.transform, unitData);
                            CampaignManager.instance.ModifyUnitStats(newUnitTuple.Item1);
                        }
                    }
                    else if (singleplayerGameType == SingleplayerGameType.Practice)
                    {
                        newUnitTuple = unitFactory.CreateUnitForBoard(boardUnit.UnitPosition, view.units.transform, unitData);
                    }
                    else
                    {
                        throw new Exception("Singleplayer Game Type not set");
                    }
                }
                else if (gameType == GameType.Multiplayer)
                {
                }
                else
                {
                    throw new Exception("Game Type not set");
                }

                var newUnitModel  = newUnitTuple.Item1;
                var newUnitObject = newUnitTuple.Item2;

                var newUnitView = new UnitView(newUnitObject);
                var unitPos     = unitPosition.ToVector2Int();
                newObjectViews.Add(unitPos, newUnitView);
                model.AddUnit(newUnitModel, unitPlayer, unitPos.x, unitPos.y);
            }

            // Invoke model to set up tile neighbors
            model.AddNeighbors();

            viewModel.ConstructViewModel();

            view.ConstructView(cols, rows, newObjectViews);

            view.mCamera.MoveToPosition(model.GridForUnit(ControllingPlayer.Units[0]).ToVector3());

            var text = SceneLoader.GetParam(SceneLoader.LOAD_OPENING_DIALOGUE_PARAM);

            StartGame();
            if (text != "")
            {
                dialogueManager.LoadDialogue(text);
            }
        }