CEntity_EffectController.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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))
  148. return false;
  149. if (Type.GetType(ClassName) == null)
  150. {
  151. if (!ClassName.Contains("token"))
  152. {
  153. if (Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}") == null)
  154. return false;
  155. }
  156. else
  157. {
  158. if (Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}") == null)
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. CEntity_Effect cEntity_Effect = null;
  165. if (CanAttachEffectComponent())
  166. {
  167. Type t = Type.GetType(ClassName);
  168. if (t == null)
  169. {
  170. if (!ClassName.Contains("token"))
  171. t = Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}");
  172. else
  173. t = Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}");
  174. }
  175. Component component = this.gameObject.AddComponent(t);
  176. if (component is CEntity_Effect)
  177. {
  178. cEntity_Effect = (CEntity_Effect)(component);
  179. }
  180. else
  181. {
  182. Debug.Log($"{ClassName} has error");
  183. }
  184. }
  185. else
  186. {
  187. cEntity_Effect = this.gameObject.AddComponent<EmptyEffectClass>();
  188. }
  189. this.cEntity_Effect = cEntity_Effect;
  190. #endregion
  191. }
  192. #endregion
  193. #region Gets the number of times the effect was used this turn
  194. public int GetUseCountThisTurn(ICardEffect cardEffect)
  195. {
  196. int useCount = 0;
  197. foreach (ICardEffect cardEffect1 in UseEffectsThisTurn)
  198. {
  199. if (cardEffect.IsSameEffect(cardEffect1))
  200. {
  201. useCount++;
  202. }
  203. }
  204. return useCount;
  205. }
  206. #endregion
  207. #region Whether the effect has reached the maximum number of times it can be used this turn.
  208. public bool isOverMaxCountPerTurn(ICardEffect cardEffect, int MaxCountPerTurn)
  209. {
  210. return GetUseCountThisTurn(cardEffect) >= MaxCountPerTurn;
  211. }
  212. #endregion
  213. #region Register as effects used this turn
  214. public void RegisterUseEfffectThisTurn(ICardEffect cardEffect)
  215. {
  216. UseEffectsThisTurn.Add(cardEffect);
  217. }
  218. #endregion
  219. }
  220. public class EmptyEffectClass : CEntity_Effect
  221. {
  222. }