CEntity_EffectController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. foreach (ICardEffect cardEffect in cardSource.cEntity_EffectController.cEntity_Effect.GetCardEffects(EffectTiming.None, thisPermanent.TopCard))
  104. {
  105. if (cardEffect is IAddSkillEffect)
  106. {
  107. if (cardEffect.IsInheritedEffect == (cardSource == thisPermanent.TopCard))
  108. {
  109. continue;
  110. }
  111. if (cardEffect.CanUse(null))
  112. {
  113. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. return GetCardEffects.Filter(cardEfect => cardEfect != null);
  123. }
  124. #endregion
  125. #region Reset the number of uses during that turn
  126. public void InitUseCountThisTurn()
  127. {
  128. UseEffectsThisTurn = new List<ICardEffect>();
  129. }
  130. #endregion
  131. #region set card effects
  132. public void AddCardEffect(string ID, string ClassName)
  133. {
  134. ID = ID.Split("-")[0];
  135. #region Generate and set an instance of the card effect class
  136. bool CanAttachEffectComponent()
  137. {
  138. if (string.IsNullOrEmpty(ClassName))
  139. return false;
  140. if (Type.GetType(ClassName) == null)
  141. {
  142. if (!ClassName.Contains("token"))
  143. {
  144. if (Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}") == null)
  145. return false;
  146. }
  147. else
  148. {
  149. if (Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}") == null)
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. CEntity_Effect cEntity_Effect = null;
  156. if (CanAttachEffectComponent())
  157. {
  158. Type t = Type.GetType(ClassName);
  159. if (t == null)
  160. {
  161. if (!ClassName.Contains("token"))
  162. t = Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}");
  163. else
  164. t = Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}");
  165. }
  166. Component component = this.gameObject.AddComponent(t);
  167. if (component is CEntity_Effect)
  168. {
  169. cEntity_Effect = (CEntity_Effect)(component);
  170. }
  171. else
  172. {
  173. Debug.Log($"{ClassName} has error");
  174. }
  175. }
  176. else
  177. {
  178. cEntity_Effect = this.gameObject.AddComponent<EmptyEffectClass>();
  179. }
  180. this.cEntity_Effect = cEntity_Effect;
  181. #endregion
  182. }
  183. #endregion
  184. #region Gets the number of times the effect was used this turn
  185. public int GetUseCountThisTurn(ICardEffect cardEffect)
  186. {
  187. int useCount = 0;
  188. foreach (ICardEffect cardEffect1 in UseEffectsThisTurn)
  189. {
  190. if (cardEffect.IsSameEffect(cardEffect1))
  191. {
  192. useCount++;
  193. }
  194. }
  195. return useCount;
  196. }
  197. #endregion
  198. #region Whether the effect has reached the maximum number of times it can be used this turn.
  199. public bool isOverMaxCountPerTurn(ICardEffect cardEffect, int MaxCountPerTurn)
  200. {
  201. return GetUseCountThisTurn(cardEffect) >= MaxCountPerTurn;
  202. }
  203. #endregion
  204. #region Register as effects used this turn
  205. public void RegisterUseEfffectThisTurn(ICardEffect cardEffect)
  206. {
  207. UseEffectsThisTurn.Add(cardEffect);
  208. }
  209. #endregion
  210. }
  211. public class EmptyEffectClass : CEntity_Effect
  212. {
  213. }