/** * Copyright 2019 Heroic Labs and contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using DemoGame.Scripts.Gameplay.Decks; using UnityEngine; using UnityEngine.UI; namespace DemoGame.Scripts.Gameplay.Cards { /// /// Side panel in displaying informations of /// supplied . /// public class CardInfoSidePanel : MonoBehaviour { #region Fields /// /// Textfield containing the name of . /// [SerializeField] private Text _cardName = null; /// /// Textfield containing the level of . /// [SerializeField] private Text _cardLevel = null; /// /// Textfield containing the description of . /// [SerializeField] private Text _cardDescription = null; /// /// Textfield containing the cost of . /// [SerializeField] private Text _cardCost = null; /// /// Image showing the visual representation of . /// [SerializeField] private Image _cardImage = null; /// /// Button responsible for adding selected card to the deck. /// [SerializeField] private Button _useButton = null; /// /// Button responsible for upgrading selected card to the next level. /// [SerializeField] private Button _upgradeButton = null; /// /// Card the info of which is currently being displayed by this . /// private Card _displayedCard; /// /// The deck reference owned by local user. /// private Deck _deck; #endregion #region Methods /// /// Sets the deck reference. /// public void SetDeck(Deck deck) { this._deck = deck; } /// /// Sets the reference. /// Supplied must be one of cards not used in the deck. /// public void SetUnusedCard(Card card, Action onSwapBegin, Action onMerge) { if (card != null) { SetCardInfo(card); } else { // Cannot unselect card return; } // Reset the listener of the _useButton _useButton.interactable = true; _useButton.onClick.RemoveAllListeners(); _useButton.onClick.AddListener(() => onSwapBegin(card)); // Search for another copy of selected card Card other = GetCardToMerge(card, _deck); if (other == null) { // This is the only copy of this card; this card cannot be upgraded _upgradeButton.interactable = false; } else { // A copy of selected card has been found; upgrade is possible // Reset the listener of the _upgradeButton _upgradeButton.interactable = true; _upgradeButton.onClick.RemoveAllListeners(); // Upgrading removes one copy of upgraded card // Determine which card should be removed and which upgraded Card removed = other.isUsed == false ? other : card; Card merged = removed != other ? other : card; _upgradeButton.onClick.AddListener(() => onMerge(merged, removed)); } } /// /// Sets the reference. /// Supplied must be one of cards already used in the deck. /// public void SetUsedCard(Card card, Action onMerge, bool sufficientFunds) { if (card != null) { SetCardInfo(card); } else { // Cannot unselect card return; } // Cards already in the deck cannot be added to deck again _useButton.interactable = false; // Search for another copy of selected card Card mergeCard = GetCardToMerge(card, _deck); if (mergeCard == null) { // This is the only copy of this card; this card cannot be upgraded _upgradeButton.interactable = false; } else { // A copy of selected card has been found; upgrade is possible // Reset the listener of the _upgradeButton // User have to have enough gold to upgrade _upgradeButton.interactable = sufficientFunds; _upgradeButton.onClick.RemoveAllListeners(); // Because currently displayed card is in the deck and users deck must // contain 6 cards at all times, the other copy of selected card will be removed _upgradeButton.onClick.AddListener(() => onMerge(card, mergeCard)); } } /// /// Sets the UI to display celected info. /// private void SetCardInfo(Card card) { _displayedCard = card; _cardLevel.text = $"lvl {card.level.ToString()}"; _cardDescription.text = card.GetCardInfo().Description; _cardCost.text = card.GetCardInfo().Cost.ToString(); _cardName.text = card.GetCardInfo().Name; _cardImage.sprite = card.GetCardInfo().Sprite; } /// /// Searches the for a copy of supplied card. /// Two cards are copies of each other if both have the same /// and and are not the same card. /// private Card GetCardToMerge(Card card, Deck deck) { if (card == null) { return null; } foreach (Card other in deck.unusedCards) { if (other == card) { continue; } if (other.IsCopy(card) == false) { continue; } return other; } if (card.isUsed == true) { return null; } else { foreach (Card other in deck.usedCards) { if (other == card) { continue; } if (other.IsCopy(card) == false) { continue; } return other; } } return null; } #endregion } }