CEntity_EffectController.cs 11 KB

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