/// <summary>
        /// Process a round in a battle with two given BattleDeck objects.
        /// Round Winner calculation uses Discards and when clause introduced in C#7.0 and switch expression introduced in C#8.0.
        /// Ref when clause: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch#the-case-statement-and-the-when-clause
        /// Ref Discards: https://docs.microsoft.com/en-us/dotnet/csharp/discards
        /// </summary>
        /// <param name="battleDeckP1"></param>
        /// <param name="battleDeckP2"></param>
        private void ProcessRound(string player1Name, string player2Name, ref BattleDeck battleDeckP1, ref BattleDeck battleDeckP2, ref BattleLog log)
        {
            Card cardP1 = battleDeckP1.GetRandomCard();
            Card cardP2 = battleDeckP2.GetRandomCard();

            //generate the accuracy of the next attack (max 1.7, min 0.3
            _accuracy = new Random().NextDouble() * (1.7 - 0.3) + 0.3;

            BattleResult roundWinner = cardP1 switch
            {
                ISpell _ when cardP2 is ISpell => FightSpellVsSpell(cardP1, cardP2),
                IMonster _ when cardP2 is ISpell => FightMonsterVsSpell(cardP1, cardP2),
                ISpell _ when cardP2 is IMonster => FightMonsterVsSpell(cardP2, cardP1, true),
                _ => FightMonsterVsMonster(cardP1, cardP2)
            };

            if (roundWinner == BattleResult.Player1Wins)
            {
                battleDeckP2.RemoveCard(cardP2);
                battleDeckP1.AddCard(cardP2);
                log.Rounds.Add(
                    $"{cardP1.Name} ({cardP1.GetType().Name}, {player1Name}) won with damage {cardP1.Damage} and element {Enum.GetName(typeof(ElementType), cardP1.Element)} " +
                    $"against {cardP2.Name} ({cardP2.GetType().Name}, {player2Name}) with damage {cardP2.Damage} and element {Enum.GetName(typeof(ElementType), cardP2.Element)}. " +
                    $"The accuracy of the attack was {Math.Round(_accuracy, 3)}");
            }
            else
            {
                battleDeckP1.RemoveCard(cardP1);
                battleDeckP2.AddCard(cardP1);
                log.Rounds.Add(
                    $"{cardP1.Name} ({cardP1.GetType().Name}, {player1Name}) lost with damage {cardP1.Damage} and element {Enum.GetName(typeof(ElementType), cardP1.Element)} " +
                    $"against {cardP2.Name} ({cardP2.GetType().Name}, {player2Name}) with damage {cardP2.Damage} and element {Enum.GetName(typeof(ElementType), cardP2.Element)}. " +
                    $"The accuracy of the attack was {Math.Round(_accuracy, 3)}");
            }
        }
Example #2
0
        public static BattleDeck GenerateBattleDeck(User user)
        {
            DummyCard[] dummyDeck = CardsUsersDatabaseHandler.GetDummyDeck(user);
            BattleDeck  deck      = new BattleDeck();

            foreach (DummyCard card in dummyDeck)
            {
                deck.AddCard(DummyCardConverter.Convert(card));
            }

            return(deck);
        }
    //[SerializeField] GameObject UIcardTemplate;
    //List<Card> cards = new List<Card>();
    //List<GameObject> UIcards = new List<GameObject>();

    //public void AddCard(Card card)
    //{
    //    cards.Add(card);
    //}
    public void DrawCard(int targetIndex)
    {
        BattleDeck deck = MainManager.instance.GetBattleDeck();
        Card       card = deck.DrawCard();

        if (card is null)
        {
            Debug.LogError("empty battle deck!!"); cards.RemoveAt(targetIndex);
        }
        else
        {
            cards[targetIndex] = card;
        }
    }
    //public void ClearUICards()
    //{
    //    foreach (var item in UIcards)
    //    {
    //        Destroy(item);
    //    }
    //}

    //public void RefreshUI()
    //{
    //    float width = 80;
    //    float gap = 10;

    //    ClearUICards();

    //    float fullSize = width * size + gap * (size - 1);
    //    float start = - fullSize / 2.0f;
    //    for (int i = 0; i < size; i++)
    //    {
    //        Debug.Log("generate UIcard");
    //        GameObject newUICard = GameObject.Instantiate(UIcardTemplate, this.transform);
    //        newUICard.GetComponent<UICard>().Init(cards[i]);
    //        newUICard.GetComponent<RectTransform>().anchoredPosition = new Vector2(start + i * (width + gap) + width / 2.0f, 0);
    //        UIcards.Add(newUICard);
    //    }
    //}

    // Start is called before the first frame update
    void Start()
    {
        size = PlayerPrefs.GetInt("FieldSize");
        BattleDeck deck = MainManager.instance.GetBattleDeck();

        deck.Init();
        deck.Shuffle();
        for (int i = 0; i < size; i++)
        {
            Card card = deck.DrawCard();
            if (card is null)
            {
                Debug.LogError("empty battle deck!!");
            }
            else
            {
                cards.Add(card);
            }
        }
        RefreshUI();
    }