using System; using System.Linq; using System.Collections.Generic; using Continentis.MainGame.Card; using Continentis.MainGame.Commands; using DG.Tweening; using SLSFramework.General; using UniRx; using UnityEngine; namespace Continentis.MainGame.Character { public partial class DeckSubmodule : SubmoduleBase { public Dictionary> piles; public DeckSubmodule(CharacterBase character) : base(character) { piles = new Dictionary>(); piles.Add("Storage", new List()); piles.Add("Hand", new List()); piles.Add("Draw", new List()); piles.Add("Discard", new List()); piles.Add("Exhaust", new List()); piles.Add("Grave", new List()); piles.Add("Pool", new List()); piles.Add("Intention", new List()); } } public partial class DeckSubmodule { public void SetUpHandCardViews() { DrawPile.ForEach(c=> c.GenerateHandCardView(CombatUIManager.Instance.combatMainPage.drawPile)); HandPile.ForEach(c=> { if(!c.cardLogic.playSubmodule.isDuringPlayEffect) c.GenerateHandCardView(CombatUIManager.Instance.combatMainPage.handPile); }); DiscardPile.ForEach(c=>c.GenerateHandCardView(CombatUIManager.Instance.combatMainPage.discardPile)); ExhaustPile.ForEach(c=>c.GenerateHandCardView(CombatUIManager.Instance.combatMainPage.exhaustPile)); } /// /// 抽取指定数量的卡牌,返回一个包含抽牌指令的指令组。 /// public CommandGroup DrawCards(int cardCount, float interval = 0.1f) { if (owner.statusSubmodule.HasStatus(StatusType.Heavy)) //沉重状态无法抽牌 { MainGameManager.GenerateInfoText("Heavy: Can not draw cards", owner.characterView); return new CommandGroup(ExecutionMode.Sequential); } if (cardCount > DrawPile.Count && DiscardPile.Count > 0) { Debug.Log("抽牌堆牌数不足,且弃牌堆有牌,正在洗牌..."); ReshuffleDeck(); } Debug.Log($"准备抽取 {cardCount} 张卡牌。"); CommandContext context = new CommandContext(); CommandGroup drawCardsGroup = new CommandGroup(ExecutionMode.Sequential, context, new Cmd_DrawCards(this, cardCount, interval), new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 抽牌后的处理 })); return drawCardsGroup; } /// /// 从指令组的上下文中获取抽到的卡牌列表。 /// public List GetDrawnCards(CommandGroup drawCardsGroup) { CommandContext context = drawCardsGroup.groupContext; return context.GetInfo>("DrawnCards"); } public void PlayCard(CardInstance card, List targetList) { card.cardLogic.Play(targetList, owner); } public CommandGroup DiscardCard(CardInstance card, float interval = 0.1f) { CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context, new Cmd_DiscardCards(card.deck, new List() { card }, interval), new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 弃牌后的处理 })); return discardCardGroup; } public CommandGroup DiscardCards(List cards, float interval = 0.1f) { Dictionary> groupedCards = cards.GroupBy(card => card.deck) .ToDictionary(group => group.Key, group => group.ToList()); CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context); foreach (var kvp in groupedCards) { discardCardGroup.AddCommand(new Cmd_DiscardCards(kvp.Key, kvp.Value, interval)); } discardCardGroup.AddCommand(new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 弃牌后的处理 })); return discardCardGroup; } public CommandGroup ExhaustCard(CardInstance card, float interval = 0.1f) { CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context, new Cmd_ExhaustCards(owner is PlayerHero, card.deck, new List() { card }, interval), new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 消耗牌后的处理 })); return discardCardGroup; } public CommandGroup ExhaustCards(List cards, float interval = 0.1f) { Dictionary> groupedCards = cards.GroupBy(card => card.deck) .ToDictionary(group => group.Key, group => group.ToList()); CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context); foreach (var kvp in groupedCards) { discardCardGroup.AddCommand(new Cmd_ExhaustCards(owner is PlayerHero, kvp.Key, kvp.Value, interval)); } discardCardGroup.AddCommand(new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 弃牌后的处理 })); return discardCardGroup; } public CommandGroup UsePowerCard(CardInstance card, float interval = 0.1f) { CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context, new Cmd_UsePowerCards(owner is PlayerHero, card.deck, new List() { card }, interval), new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 消耗牌后的处理 })); return discardCardGroup; } public CommandGroup UsePowerCards(List cards, float interval = 0.1f) { Dictionary> groupedCards = cards.GroupBy(card => card.deck) .ToDictionary(group => group.Key, group => group.ToList()); CommandContext context = new CommandContext(); CommandGroup discardCardGroup = new CommandGroup(ExecutionMode.Sequential, context); foreach (var kvp in groupedCards) { discardCardGroup.AddCommand(new Cmd_UsePowerCards(owner is PlayerHero, kvp.Key, kvp.Value, interval)); } discardCardGroup.AddCommand(new Cmd_Function(0, () => { //Debug.Log((context.sharedInfo["DrawnCards"] as List).Count); //TODO: 弃牌后的处理 })); return discardCardGroup; } public void ReshuffleDeck(float interval = 0.1f) { CommandQueueManager.Instance.AddCommand(new Cmd_ReshuffleDeck(this, interval)); } } public partial class DeckSubmodule { public KeyValuePair> GetCardLocation(CardInstance card, out int index) { index = -1; foreach (KeyValuePair> pile in piles) { index = pile.Value.IndexOf(card); if (index != -1) { return pile; } } throw new Exception("Card not found in any pile."); } public void TransferCard(string pileName, int index, CardInstance card) { List pile = Pile(pileName); pile.Transfer(card, index); card.cardLocation = new CardLocation(pileName, index); } public void TransferCard(string fromPileName, string toPileName, CardInstance card) { List fromPile = Pile(fromPileName); List toPile = Pile(toPileName); fromPile.Transfer(toPile, card); card.cardLocation = new CardLocation(toPileName, toPile.IndexOf(card)); } public void TransferCard(List fromPile, List toPile, CardInstance card) { fromPile.Transfer(toPile, card); card.cardLocation = new CardLocation(GetCardLocation(card, out _).Key, toPile.IndexOf(card)); } public List GetAllCards() { List allCards = new List(); foreach (KeyValuePair> pile in piles) { allCards.AddRange(pile.Value); } return allCards; } } public partial class DeckSubmodule { public List Pile(string pileName) { if (piles.TryGetValue(pileName, out List pile)) { return pile; } throw new KeyNotFoundException($"Pile '{pileName}' not found."); } public List StoragePile => Pile("Storage"); public List HandPile => Pile("Hand"); public List DrawPile => Pile("Draw"); public List DiscardPile => Pile("Discard"); public List ExhaustPile => Pile("Exhaust"); public List GravePile => Pile("Grave"); public List PoolPile => Pile("Pool"); public List IntentionPile => Pile("Intention"); } }