Ejemplo n.º 1
0
 public async Task moveTo(IGame game, IEnumerable <Card> cards, Pile targetPile, int position)
 {
     foreach (var card in cards.Reverse())
     {
         await moveTo(game, card, this, targetPile, position);
     }
 }
Ejemplo n.º 2
0
 public MoveCardEventArg(Pile from, Pile to, Card card, int position)
 {
     this.from     = from;
     this.to       = to;
     this.card     = card;
     this.position = position;
 }
Ejemplo n.º 3
0
 public void addPile(Pile pile)
 {
     pileList.Add(pile);
     pile.owner = null;
     foreach (Card card in pile)
     {
         card.owner = null;
     }
 }
Ejemplo n.º 4
0
 public static Task moveTo(IGame game, Card card, Pile from, Pile to, int position)
 {
     if (game != null)
     {
         return(game.triggers.doEvent(new MoveCardEventArg(from, to, card, position), arg =>
         {
             from = arg.from;
             to = arg.to;
             card = arg.card;
             position = arg.position;
             if (from != null)
             {
                 if (from.cardList.Remove(card))
                 {
                     try
                     {
                         foreach (var effect in card.define.getEffects())
                         {
                             if (effect is IPassiveEffect passiveEffect)
                             {
                                 if (passiveEffect.piles.Contains(from.name) && (to == null || !passiveEffect.piles.Contains(to.name)))
                                 {
                                     passiveEffect.onDisable(game, card, null);
                                 }
                             }
                             else if (effect is GeneratedEffect generatedEffect)
                             {
                                 if (generatedEffect.pileList.Contains(from.name) && (to == null || !generatedEffect.pileList.Contains(to.name)))
                                 {
                                     generatedEffect.onDisable(game, card, null);
                                 }
                             }
                         }
                     }
                     catch (Exception e)
                     {
                         game.logger.logError("将" + card + "从" + from + "移动到" + to + "时禁用被动效果引发异常:" + e);
                     }
                     if (to != null)
                     {
                         if (position < 0)
                         {
                             position = 0;
                         }
                         if (position < to.cardList.Count)
                         {
                             to.cardList.Insert(position, card);
                         }
                         else
                         {
                             to.cardList.Add(card);
                         }
                         card.pile = to;
                         card.owner = to.owner;
                         try
                         {
                             foreach (var effect in card.define.getEffects())
                             {
                                 if (effect is IPassiveEffect passiveEffect)
                                 {
                                     if ((from == null || !passiveEffect.piles.Contains(from.name)) && passiveEffect.piles.Contains(to.name))
                                     {
                                         passiveEffect.onEnable(game, card, null);
                                     }
                                 }
                                 else if (effect is GeneratedEffect generatedEffect)
                                 {
                                     if ((from == null || !generatedEffect.pileList.Contains(from.name)) && generatedEffect.pileList.Contains(to.name))
                                     {
                                         generatedEffect.onEnable(game, card, null);
                                     }
                                 }
                             }
                         }
                         catch (Exception e)
                         {
                             game.logger.logError("将" + card + "从" + from + "移动到" + to + "时激活被动效果引发异常:" + e);
                         }
                     }
                     else
                     {
                         card.pile = null;
                         card.owner = null;
                     }
                 }
             }
             else
             {
                 if (to != null)
                 {
                     if (position < 0)
                     {
                         position = 0;
                     }
                     if (position < to.cardList.Count)
                     {
                         to.cardList.Insert(position, card);
                     }
                     else
                     {
                         to.cardList.Add(card);
                     }
                     card.pile = to;
                     card.owner = to.owner;
                     try
                     {
                         foreach (var effect in card.define.getEffects())
                         {
                             if (effect is IPassiveEffect passiveEffect)
                             {
                                 if (passiveEffect.piles.Contains(to.name))
                                 {
                                     passiveEffect.onEnable(game, card, null);
                                 }
                             }
                             else if (effect is GeneratedEffect generatedEffect)
                             {
                                 if (generatedEffect.pileList.Contains(to.name))
                                 {
                                     generatedEffect.onEnable(game, card, null);
                                 }
                             }
                         }
                     }
                     catch (Exception e)
                     {
                         game.logger.logError("将" + card + "从" + from + "中移除时激活被动效果引发异常:" + e);
                     }
                 }
                 else
                 {
                     card.pile = null;
                     card.owner = null;
                 }
             }
             return Task.CompletedTask;
         }));
     }
     else
     {
         if (from != null)
         {
             if (from.cardList.Remove(card))
             {
                 if (to != null)
                 {
                     if (position < 0)
                     {
                         position = 0;
                     }
                     if (position < to.cardList.Count)
                     {
                         to.cardList.Insert(position, card);
                     }
                     else
                     {
                         to.cardList.Add(card);
                     }
                     card.pile  = to;
                     card.owner = to.owner;
                 }
                 else
                 {
                     card.pile  = null;
                     card.owner = null;
                 }
             }
         }
         else
         {
             if (to != null)
             {
                 if (position < 0)
                 {
                     position = 0;
                 }
                 if (position < to.cardList.Count)
                 {
                     to.cardList.Insert(position, card);
                 }
                 else
                 {
                     to.cardList.Add(card);
                 }
                 card.pile  = to;
                 card.owner = to.owner;
             }
             else
             {
                 card.pile  = null;
                 card.owner = null;
             }
         }
         return(Task.CompletedTask);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 将位于该牌堆中的一张牌移动到其他的牌堆中。
 /// </summary>
 /// <param name="card"></param>
 /// <param name="to"></param>
 /// <param name="position"></param>
 public Task moveTo(IGame game, Card card, Pile to, int position)
 {
     return(moveTo(game, card, this, to, position));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 将牌堆中的一些牌与目标牌堆中随机的一些牌相替换。
 /// </summary>
 /// <param name="engine">用于提供随机功能的引擎</param>
 /// <param name="originalCards">要进行替换的卡牌</param>
 /// <param name="pile">目标牌堆</param>
 /// <returns>返回替换原有卡牌的卡牌数组,顺序与替换的顺序相同</returns>
 public async Task <Card[]> replaceByRandom(CardEngine engine, Card[] originalCards, Pile pile)
 {
     Card[] replaced = new Card[originalCards.Length];
     for (int i = 0; i < originalCards.Length; i++)
     {
         replaced[i] = pile.getCardByRandom(engine);
         await replace(engine, originalCards[i], replaced[i]);
     }
     return(replaced);
     //int[] indexArray = new int[originalCards.Length];
     //for (int i = 0; i < originalCards.Length; i++)
     //{
     //    //记录当前牌堆中的空位
     //    Card card = originalCards[i];
     //    indexArray[i] = indexOf(card);
     //    if (indexArray[i] < 0)
     //        throw new IndexOutOfRangeException(this + "中不存在" + card + "," + this + ":" + string.Join(",", cardList));
     //    //把牌放回去
     //    pile.cardList.Insert(engine.randomInt(0, pile.cardList.Count), card);
     //    foreach (IPassiveEffect effect in card.define.effects.OfType<IPassiveEffect>())
     //    {
     //        if (effect.piles.Contains(pile.name))
     //            effect.onEnable(engine, card);
     //    }
     //}
     //for (int i = 0; i < indexArray.Length; i++)
     //{
     //    //将牌堆中的随机卡片填入空位
     //    int targetIndex = engine.randomInt(0, pile.count - 1);
     //    cardList[indexArray[i]] = pile.cardList[targetIndex];
     //    Card card = cardList[indexArray[i]];
     //    foreach (IPassiveEffect effect in card.define.effects.OfType<IPassiveEffect>())
     //    {
     //        if (effect.piles.Contains(name))
     //            effect.onEnable(engine, card);
     //    }
     //    //并将其从牌堆中移除
     //    pile.cardList.RemoveAt(targetIndex);
     //    foreach (IPassiveEffect effect in card.define.effects.OfType<IPassiveEffect>())
     //    {
     //        if (effect.piles.Contains(pile.name))
     //            effect.onDisable(engine, card);
     //    }
     //}
     //return indexArray.Select(i => cardList[i]).ToArray();
 }
Ejemplo n.º 7
0
 public Task moveTo(IGame game, Card card, Pile targetPile)
 {
     return(moveTo(game, card, this, targetPile, targetPile.count));
 }