예제 #1
0
    public void Init(Game game, BattleHistory battleHistory)
    {
        this.game = game;

        // Copy the history, except for the moves; they'll be applied later.
        this.history = battleHistory;
        List <object> moveHistory = battleHistory.moves;

        this.history.moves = new List <object>();

        Scenario  scenario = this.history.scenario;
        BattleMap map      = this.history.startingMap;

        this.mapDisplay.Init(this, map.size);

        // Build misc objects.

        {
            // Let us know when anything on the UI is clicked (and not handled by a button or something).

            EventTrigger eventTrigger = this.uiRefs.canvas.gameObject.AddComponent <EventTrigger>();

            var entry = new EventTrigger.Entry();
            entry.eventID = EventTriggerType.PointerClick;
            entry.callback.AddListener((data) => {
                var button = ((PointerEventData)data).button;
                if (button == PointerEventData.InputButton.Left)
                {
                    this.MouseEvent(null, MapDisplay.MouseEventType.Click);
                }
                else if (button == PointerEventData.InputButton.Right)
                {
                    this.MouseEvent(null, MapDisplay.MouseEventType.RightClick);
                }
            });
            eventTrigger.triggers.Add(entry);
        }

        // Build map.

        this.mapSize = map.size;
        this.tiles   = new BattleTile[this.mapSize.x * this.mapSize.y];
        TileData baseTileData = GameData.GetTile(map.baseTileName);
        TileData tileData     = baseTileData;

        this.pathNetwork = new PathNetwork();
        for (int y = 0; y < map.size.y; ++y)
        {
            for (int x = 0; x < map.size.x; ++x)
            {
                var pos = new Vector2i(x, y);
                tileData = baseTileData;
                foreach (var o in map.tileOverrides)
                {
                    if (o.pos == pos)
                    {
                        tileData = GameData.GetTile(o.name);
                        break;
                    }
                }

                MapTile    mapTile = this.mapDisplay.GetTile(pos);
                BattleTile newTile = mapTile.gameObject.AddComponent <BattleTile>();
                newTile.Init(this, x, y, tileData);
                this.tiles[x + y * this.mapSize.x] = newTile;
            }
        }

        foreach (BattleTile tile in this.tiles)
        {
            for (int n = 0; n < 6; ++n)
            {
                BattleTile neighbor = tile.GetNeighbor(n);
                if (neighbor)
                {
                    this.pathNetwork.ConnectNodes(tile, neighbor);
                }
            }

            if (tile.data.allowsMovement == false)
            {
                this.pathNetwork.SetNodeEnabled(tile, false);
            }
        }

        // Create teams and place mechs.

        var playerSpawns = new Stack <Vector2i>();
        var enemySpawns  = new Stack <Vector2i>();

        foreach (var e in map.entities)
        {
            if (e.name == "PlayerMechSpawn")
            {
                playerSpawns.Push(e.pos);
            }
            else if (e.name == "EnemyMechSpawn")
            {
                enemySpawns.Push(e.pos);
            }
        }

        this.teams = new List <BattleTeam>();
        foreach (Scenario.Team teamData in scenario.teams)
        {
            BattleTeam team = new BattleTeam();
            this.teams.Add(team);
            team.mechs    = new List <BattleMech>();
            team.isPlayer = teamData.isPlayer;
            if (team.isPlayer == false)
            {
                team.ai = new BattleTeamAi(team);
            }
            team.visibleTiles = new bool[this.tiles.Length];

            foreach (Scenario.Mech m in teamData.mechs)
            {
                MechData mechData = GameData.GetMech(m.mechName);

                Vector2i   spawnPos = team.isPlayer ? playerSpawns.Pop() : enemySpawns.Pop();
                BattleTile tile     = this.GetTile(spawnPos);

                GameObject mechGO = new GameObject(string.Concat("Mech: ", mechData.name));
                mechGO.transform.parent = tile.transform;
                BattleMech mech = mechGO.AddComponent <BattleMech>();
                mech.Init(this, mechData);
                mech.PlaceAtMapTile(tile.mapTile);
                mech.SetDirection(m.direction);
                Assert.IsTrue(tile.mech == null);
                tile.mech = mech;
                mech.tile = tile;

                if (team.isPlayer == false)
                {
                    mech.ai = new BattleMechAi(mech);
                }

                this.pathNetwork.SetNodeEnabled(tile, false);

                team.mechs.Add(mech);
                mech.team = team;
            }
        }

        this.currentTeamIndex = this.history.scenario.startingTeamIndex;
        this.currentTeam      = this.teams[this.currentTeamIndex];

        // UI stuff.

        this.uiRefs.advanceAiButton.interactable = false;

        this.uiRefs.tileInfoBorder.SetActive(false);
        this.uiRefs.mechTab.SetActive(false);
        this.uiRefs.pilotTab.SetActive(false);
        this.uiRefs.tileTab.SetActive(false);
        this.uiRefs.actionsPanel.SetActive(false);

        Utility.AddButtonClickListener(this.uiRefs.finishTurnButton, this.UnitListButtonPressed);
        Utility.AddButtonClickListener(this.uiRefs.advanceAiButton, this.UnitListButtonPressed);

        Utility.AddButtonClickListener(this.uiRefs.mechTabButton, this.TileInfoTabButtonClicked);
        Utility.AddButtonClickListener(this.uiRefs.pilotTabButton, this.TileInfoTabButtonClicked);
        Utility.AddButtonClickListener(this.uiRefs.tileTabButton, this.TileInfoTabButtonClicked);

        Utility.AddButtonClickListener(this.uiRefs.moveButton, this.ActionButtonPressed);
        Utility.AddButtonClickListener(this.uiRefs.setTargetButton, this.ActionButtonPressed);
        Utility.AddButtonClickListener(this.uiRefs.fireNowButton, this.ActionButtonPressed);

        Utility.AddToggleListener(this.uiRefs.fireAutoToggle, this.ActionToggleChanged);

        Utility.AddButtonHoverListener(this.uiRefs.fireNowButton, this.ActionButtonHoverChanged);

        this.BringTileTabButtonToFront(this.uiRefs.mechTabButton);
        this.UpdateRightPanel();

        this.apTextOriginalColor = this.uiRefs.apText.color;

        // Execute any moves to start with.

        foreach (object o in moveHistory)
        {
            this.ExecuteMove(o);
        }

        this.currentTeamIndex = battleHistory.currentTeamIndex;
        this.currentTeam      = this.teams[this.currentTeamIndex];

        // Start first state.

        this.UpdateFogOfWar();

        if (this.currentTeam.isPlayer)
        {
            this.SetState(BattleState.SelectingAction);
        }
        else
        {
            this.SetState(BattleState.AiControl);
        }
    }