CEntity_EffectController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. public class CEntity_EffectController : MonoBehaviour
  7. {
  8. //Number of skills used this turn (referenced by use limit)
  9. List<ICardEffect> UseEffectsThisTurn = new List<ICardEffect>();
  10. #region CEntity_Effect
  11. public CEntity_Effect cEntity_Effect { get; set; }
  12. #endregion
  13. #region Get effect list
  14. public List<ICardEffect> GetCardEffects_ExceptAddedEffects(EffectTiming timing, CardSource card)
  15. {
  16. List<ICardEffect> GetCardEffects = new List<ICardEffect>();
  17. if (cEntity_Effect != null)
  18. {
  19. foreach (ICardEffect cardEffect in cEntity_Effect.GetCardEffects(timing, card))
  20. {
  21. GetCardEffects.Add(cardEffect);
  22. }
  23. }
  24. return GetCardEffects;
  25. }
  26. public List<ICardEffect> GetCardEffects(EffectTiming timing, CardSource card)
  27. {
  28. List<ICardEffect> GetCardEffects = new List<ICardEffect>();
  29. foreach (ICardEffect cardEffect in GetCardEffects_ExceptAddedEffects(timing, card))
  30. {
  31. GetCardEffects.Add(cardEffect);
  32. }
  33. Permanent thisPermanent = card.PermanentOfThisCard();
  34. bool isDigivolutionCard = thisPermanent != null && thisPermanent.DigivolutionCards.Contains(card);
  35. if (!isDigivolutionCard)
  36. {
  37. // Effects added by other card effects
  38. if (timing != EffectTiming.None)
  39. {
  40. #region Effects added by other card effects
  41. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  42. {
  43. if (player != null)
  44. {
  45. #region Effects of permanents in play
  46. foreach (Permanent permanent in player.GetFieldPermanents())
  47. {
  48. if (permanent.TopCard.cEntity_EffectController.cEntity_Effect != null)
  49. {
  50. foreach (CardSource cardSource in permanent.cardSources)
  51. {
  52. if (cardSource != permanent.TopCard)
  53. {
  54. if (!permanent.IsDigimon)
  55. {
  56. continue;
  57. }
  58. }
  59. foreach (ICardEffect cardEffect in cardSource.cEntity_EffectController.cEntity_Effect.GetCardEffects(EffectTiming.None, permanent.TopCard))
  60. {
  61. if (cardEffect is IAddSkillEffect)
  62. {
  63. if (cardEffect.IsInheritedEffect == (cardSource == permanent.TopCard))
  64. {
  65. continue;
  66. }
  67. if (cardEffect.CanUse(null))
  68. {
  69. if (!card.CanNotBeAffected(cardEffect))
  70. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. #endregion
  78. #region Effects added by players
  79. foreach (ICardEffect cardEffect in player.EffectList(EffectTiming.None))
  80. {
  81. if (cardEffect is IAddSkillEffect)
  82. {
  83. if (cardEffect.CanUse(null))
  84. {
  85. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  86. }
  87. }
  88. }
  89. #endregion
  90. }
  91. }
  92. #endregion
  93. }
  94. // Explore about EffectTiming.None only if added by me
  95. else
  96. {
  97. if (thisPermanent != null)
  98. {
  99. if (thisPermanent.TopCard.cEntity_EffectController.cEntity_Effect != null)
  100. {
  101. foreach (CardSource cardSource in thisPermanent.cardSources)
  102. {
  103. /*
  104. if (cardSource != thisPermanent.TopCard)
  105. {
  106. if (!thisPermanent.IsDigimon)
  107. {
  108. continue;
  109. }
  110. }
  111. */
  112. foreach (ICardEffect cardEffect in cardSource.cEntity_EffectController.cEntity_Effect.GetCardEffects(EffectTiming.None, thisPermanent.TopCard))
  113. {
  114. if (cardEffect is IAddSkillEffect)
  115. {
  116. if (cardEffect.IsInheritedEffect == (cardSource == thisPermanent.TopCard))
  117. {
  118. continue;
  119. }
  120. if (cardEffect.CanUse(null))
  121. {
  122. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. return GetCardEffects.Filter(cardEfect => cardEfect != null);
  132. }
  133. #endregion
  134. #region Reset the number of uses during that turn
  135. public void InitUseCountThisTurn()
  136. {
  137. UseEffectsThisTurn = new List<ICardEffect>();
  138. }
  139. #endregion
  140. #region set card effects
  141. public void AddCardEffect(string ID, string ClassName)
  142. {
  143. ID = ID.Split("-")[0];
  144. #region Generate and set an instance of the card effect class
  145. bool CanAttachEffectComponent()
  146. {
  147. if (string.IsNullOrEmpty(ClassName)) return false;
  148. if (Type.GetType(ClassName) == null)
  149. {
  150. if (Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}") == null) return false;
  151. }
  152. return true;
  153. }
  154. CEntity_Effect cEntity_Effect = null;
  155. if (CanAttachEffectComponent())
  156. {
  157. Type t = Type.GetType(ClassName);
  158. if (t == null)
  159. t = Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}");
  160. Component component = this.gameObject.AddComponent(t);
  161. if (component is CEntity_Effect)
  162. {
  163. cEntity_Effect = (CEntity_Effect)(component);
  164. }
  165. else
  166. {
  167. Debug.Log($"{ClassName} has error");
  168. }
  169. }
  170. else
  171. {
  172. cEntity_Effect = this.gameObject.AddComponent<EmptyEffectClass>();
  173. }
  174. this.cEntity_Effect = cEntity_Effect;
  175. #endregion
  176. }
  177. #endregion
  178. #region Gets the number of times the effect was used this turn
  179. public int GetUseCountThisTurn(ICardEffect cardEffect)
  180. {
  181. int useCount = 0;
  182. foreach (ICardEffect cardEffect1 in UseEffectsThisTurn)
  183. {
  184. if (cardEffect.IsSameEffect(cardEffect1))
  185. {
  186. useCount++;
  187. }
  188. }
  189. return useCount;
  190. }
  191. #endregion
  192. #region Whether the effect has reached the maximum number of times it can be used this turn.
  193. public bool isOverMaxCountPerTurn(ICardEffect cardEffect, int MaxCountPerTurn)
  194. {
  195. return GetUseCountThisTurn(cardEffect) >= MaxCountPerTurn;
  196. }
  197. #endregion
  198. #region Register as effects used this turn
  199. public void RegisterUseEfffectThisTurn(ICardEffect cardEffect)
  200. {
  201. UseEffectsThisTurn.Add(cardEffect);
  202. }
  203. #endregion
  204. }
  205. public class EmptyEffectClass : CEntity_Effect
  206. {
  207. }