CEntity_EffectController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. UnityEngine.Debug.Log($"ADD SKILL CHECK {cardSource.BaseENGCardNameFromEntity}: {cardEffect.IsInheritedEffect}== {(cardSource == permanent.TopCard)} || {cardSource.IsFlipped}");
  64. if (cardEffect.IsInheritedEffect == (cardSource == permanent.TopCard) || (cardEffect.IsInheritedEffect && cardSource.IsFlipped))
  65. {
  66. continue;
  67. }
  68. UnityEngine.Debug.Log($"ADD SKILL CHECK {cardEffect.CanUse(null)}");
  69. if (cardEffect.CanUse(null))
  70. {
  71. UnityEngine.Debug.Log($"ADD SKILL CHECK {!card.CanNotBeAffected(cardEffect)}");
  72. if (!card.CanNotBeAffected(cardEffect))
  73. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. #endregion
  81. #region Effects from security
  82. foreach (CardSource source in player.SecurityCards)
  83. {
  84. if (source.IsFlipped)
  85. continue;
  86. foreach (ICardEffect cardEffect in source.EffectList(EffectTiming.None))
  87. {
  88. if (cardEffect is IAddSkillEffect)
  89. {
  90. if (cardEffect.CanUse(null))
  91. {
  92. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  93. }
  94. }
  95. }
  96. }
  97. #endregion
  98. #region Effects added by players
  99. foreach (ICardEffect cardEffect in player.EffectList(EffectTiming.None))
  100. {
  101. if (cardEffect is IAddSkillEffect)
  102. {
  103. if (cardEffect.CanUse(null))
  104. {
  105. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  106. }
  107. }
  108. }
  109. #endregion
  110. }
  111. }
  112. #endregion
  113. }
  114. // Explore about EffectTiming.None only if added by me
  115. else
  116. {
  117. if (thisPermanent != null)
  118. {
  119. if (thisPermanent.TopCard.cEntity_EffectController.cEntity_Effect != null)
  120. {
  121. foreach (CardSource cardSource in thisPermanent.cardSources)
  122. {
  123. foreach (ICardEffect cardEffect in cardSource.cEntity_EffectController.cEntity_Effect.GetCardEffects(EffectTiming.None, thisPermanent.TopCard))
  124. {
  125. if (cardEffect is IAddSkillEffect)
  126. {
  127. if (cardEffect.IsInheritedEffect == (cardSource == thisPermanent.TopCard) || cardEffect.IsInheritedEffect == cardSource.IsFlipped)
  128. {
  129. continue;
  130. }
  131. if (cardEffect.CanUse(null))
  132. {
  133. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. if (CardEffectCommons.IsExistInSecurity(card))
  141. {
  142. foreach (ICardEffect cardEffect in card.cEntity_EffectController.cEntity_Effect.GetCardEffects(EffectTiming.None, card))
  143. {
  144. if (cardEffect is IAddSkillEffect)
  145. {
  146. if (cardEffect.CanUse(null))
  147. {
  148. GetCardEffects = ((IAddSkillEffect)cardEffect).GetCardEffect(card, GetCardEffects, timing);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. return GetCardEffects.Filter(cardEfect => cardEfect != null);
  156. }
  157. #endregion
  158. #region Reset the number of uses during that turn
  159. public void InitUseCountThisTurn()
  160. {
  161. UseEffectsThisTurn = new List<ICardEffect>();
  162. }
  163. #endregion
  164. #region set card effects
  165. public void AddCardEffect(string ID, string ClassName)
  166. {
  167. ID = ID.Split("-")[0];
  168. #region Generate and set an instance of the card effect class
  169. bool CanAttachEffectComponent()
  170. {
  171. if (string.IsNullOrEmpty(ClassName))
  172. return false;
  173. if (Type.GetType(ClassName) == null)
  174. {
  175. if (!ClassName.Contains("token"))
  176. {
  177. if (Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}") == null)
  178. return false;
  179. }
  180. else
  181. {
  182. if (Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}") == null)
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. CEntity_Effect cEntity_Effect = null;
  189. if (CanAttachEffectComponent())
  190. {
  191. Type t = Type.GetType(ClassName);
  192. if (t == null)
  193. {
  194. if (!ClassName.Contains("token"))
  195. t = Type.GetType($"DCGO.CardEffects.{ID}.{ClassName}");
  196. else
  197. t = Type.GetType($"DCGO.CardEffects.Tokens.{ClassName}");
  198. }
  199. Component component = this.gameObject.AddComponent(t);
  200. if (component is CEntity_Effect)
  201. {
  202. cEntity_Effect = (CEntity_Effect)(component);
  203. }
  204. else
  205. {
  206. Debug.Log($"{ClassName} has error");
  207. }
  208. }
  209. else
  210. {
  211. cEntity_Effect = this.gameObject.AddComponent<EmptyEffectClass>();
  212. }
  213. this.cEntity_Effect = cEntity_Effect;
  214. #endregion
  215. }
  216. #endregion
  217. #region Gets the number of times the effect was used this turn
  218. public int GetUseCountThisTurn(ICardEffect cardEffect)
  219. {
  220. int useCount = 0;
  221. foreach (ICardEffect cardEffect1 in UseEffectsThisTurn)
  222. {
  223. if (cardEffect.IsSameEffect(cardEffect1))
  224. {
  225. useCount++;
  226. }
  227. }
  228. return useCount;
  229. }
  230. #endregion
  231. #region Whether the effect has reached the maximum number of times it can be used this turn.
  232. public bool isOverMaxCountPerTurn(ICardEffect cardEffect, int MaxCountPerTurn)
  233. {
  234. return GetUseCountThisTurn(cardEffect) >= MaxCountPerTurn;
  235. }
  236. #endregion
  237. #region Register as effects used this turn
  238. public void RegisterUseEfffectThisTurn(ICardEffect cardEffect)
  239. {
  240. UseEffectsThisTurn.Add(cardEffect);
  241. }
  242. #endregion
  243. }
  244. public class EmptyEffectClass : CEntity_Effect
  245. {
  246. }