Exemplo n.º 1
0
    public TowerInfo BuildTower(PlayerAgent player, HexCoord coord, int index = -1)
    {
        if (index < 0)
        {
            index = defaultTower;
        }

        if (index < 0 || index >= towerList.Length)
        {
            return(null);
        }

        int price = towerList[index].price;

        bool isDefaultTower = !playerTowers.ContainsKey(player.SlotId) && index == defaultTower;

        if (isDefaultTower)
        {
            price = 0;
        }

        if (player.Resource < price)
        {
            return(null);
        }

        var vision = GetHexagonsInVision(player);

        vision.UnionWith(player.CampVision);
        if (!vision.Contains(coord))
        {
            return(null);
        }

        if (mapTowers.ContainsKey(coord))
        {
            return(null);
        }

        var tower = Instantiate(towerList[index]);

        NetworkServer.Spawn(tower.gameObject);

        if (!playerTowers.ContainsKey(player.SlotId))
        {
            playerTowers.Add(player.SlotId, new List <TowerInfo>());
        }

        playerTowers[player.SlotId].Add(tower);

        mapTowers.Add(coord, tower);

        tower.labelColor   = player.PlayerColor;
        tower.coord        = coord;
        tower.playerSlotId = player.SlotId;

        player.AddResource(-price);
        UpdateAfterBuildingChanged(player.SlotId);

        if (!isDefaultTower)
        {
            player.RpcAddLog("You built a " + tower.type.ToString() + ", resource decreased by " + price);
        }

        return(tower);
    }