CEntity_EffectController.cs 10 KB

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