コード例 #1
0
    /// <summary>
    /// Creates a new Unit from a prefab. This includes adding it to the game logic, and creating a visual
    /// representation. The prefab provided should have a UnitAvatar script attatched, as well as any
    /// requirements of that script. This operation will fail if the given position is already occupied.
    /// </summary>
    /// <param name="unitPrefab">the prefab of the unit to create</param>
    /// <param name="pos">the TileVector position to spawn it</param>
    /// <param name="dir">the Direction for it to be facing</param>
    /// <param name="owner">the Player owner of the Unit</param>
    /// <param name="mirrored">whether or not the Unit is mirrored</param>
    /// <returns>true if the operation was successful; false if not</returns>
    public bool MakeUnit(GameObject unitPrefab, Player owner, TileVector pos, CardinalDirection dir, bool mirrored)
    {
        if (_gameOver || _playerUnitPlaced[owner])
        {
            return(false);                                                      // stop players placing more than one unit
        }
        var avatar = Instantiate(unitPrefab).GetComponent <UnitAvatar>();
        var unit   = avatar.CreateUnit(owner, pos, dir, mirrored);

        if (!_worldController.AddUnit(unit))         // oops! bad unit placement, so delete the unit as if nothing happened
        {
            Destroy(avatar.gameObject);
            return(false);
        }
        else            // successful placement
        {
            avatar.SetUnit(unit);
            _playerUnitPlaced[owner] = true;                    // set player as placed a unit

            _allAvatars.Add(avatar);

            avatar.EnqueueAnimation(new DeployAnimation(avatar));

            var allPlaced = true;                                       // check if all players have placed a unit
            foreach (var placed in _playerUnitPlaced.Values)
            {
                if (!placed)
                {
                    allPlaced = false;
                    break;
                }
            }
            if (allPlaced)                                                      // reset players' placement status and run game
            {
                DoTurn();
            }
            return(true);
        }
    }