コード例 #1
0
    /// <summary>
    /// Method makes turn transitions. It is called by player at the end of his turn.
    /// </summary>
    public void EndTurn()
    {
        if (Units.Select(u => u.PlayerNumber).Distinct().Count() == 1)
        {
            return;
        }
        CellGridState = new CellGridStateBase(this);

        // End turn for current unit and advance the init tracker
        InitTracker[0].OnTurnEnd();
        AdvanceTracker();
        // Set current player to next player in the initiative
        CurrentPlayerNumber = InitTracker[0].PlayerNumber;

        if (TurnEnded != null)
        {
            TurnEnded.Invoke(this, new EventArgs());
        }

        // Start next unit's turn
        InitTracker[0].OnTurnStart();
        Players.Find(p => p.PlayerNumber.Equals(CurrentPlayerNumber)).Play(this);
        // Select unit
        if (CurrentPlayerNumber == 0)
        {
            CellGridState = new CellGridStateUnitSelected(this, InitTracker[0]);
        }
    }
コード例 #2
0
    // Use this for initialization
    void Start()
    {
        // Create a list of all players (that control the units)
        Players = new List <Player>();
        for (int i = 0; i < PlayersParent.childCount; i++)
        {
            var player = PlayersParent.GetChild(i).GetComponent <Player>();
            if (player != null)
            {
                Players.Add(player);
            }
            else
            {
                Debug.LogError("Invalid object in Players Parent game object");
            }
        }
        NumberOfPlayers = Players.Count;

        // Create a list of all cells on the current scene
        // Individual cells are children of the parent Grid
        Cells = new List <Cell>();
        for (int i = 0; i < transform.childCount; i++)
        {
            var cell = transform.GetChild(i).gameObject.GetComponent <Cell>();

            if (cell != null)
            {
                Cells.Add(cell);
            }
            else
            {
                Debug.LogError("Invalid object in cells parent game object");
            }
        }

        // Entity generator
        var entityGenerator = GetComponent <IUnitGenerator>();
        var Entities        = new List <Entity>();

        // Setup Entities
        if (entityGenerator != null)
        {
            Entities = entityGenerator.SpawnEntities(Cells);

            Units     = new List <Unit>();
            Obstacles = new List <Obstacle>();
            foreach (Entity entity in Entities)
            {
                if (entity is Unit)
                {
                    Units.Add(entity as Unit);
                }
                else if (entity is Obstacle)
                {
                    Obstacles.Add(entity as Obstacle);
                }
            }

            // Set CurrentPlayerNumber to the owner of the unit with the highest dexterity score
            InitTracker         = Units.OrderByDescending(o => o.Dexterity).ToList();
            CurrentPlayerNumber = InitTracker[0].PlayerNumber;

            // Subscribe functions to entity events
            foreach (var unit in Units)
            {
                unit.EntityClicked += OnEntityClicked;
                unit.UnitDestroyed += OnUnitDestroyed;
            }
            foreach (var obs in Obstacles)
            {
                obs.EntityClicked += OnEntityClicked;
                obs.ObsDestroyed  += OnObsDestroyed;
            }
        }
        else
        {
            Debug.LogError("No IUnitGenerator script attached to cell grid");
        }

        // Subscribe click and highlighting events to Cell clicks
        foreach (var cell in Cells)
        {
            cell.CellClicked       += OnCellClicked;
            cell.CellHighlighted   += OnCellHighlighted;
            cell.CellDehighlighted += OnCellDehighlighted;
        }

        CellGridState = new CellGridStateBase(this);

        StartGame();
    }