Advertisement
Velken_Iakov

Untitled

Oct 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.71 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = UnityEngine.Random;
  6.  
  7. public class Minion : Character
  8. {
  9. public MinionCard Card;
  10.  
  11. public Mechanics Mechanics;
  12.  
  13. public override int CurrentAttack
  14. {
  15. get
  16. {
  17. int modifiedAttack = BaseAttack;
  18.  
  19. foreach (Func<int, int> modifier in AttackModifiers)
  20. {
  21. modifiedAttack = modifier.Invoke(modifiedAttack);
  22.  
  23. if (modifiedAttack < 0)
  24. {
  25. modifiedAttack = 0;
  26. }
  27. }
  28.  
  29. foreach (Func<int, int> modifier in AuraAttackModifiers)
  30. {
  31. modifiedAttack = modifier.Invoke(modifiedAttack);
  32.  
  33. if (modifiedAttack < 0)
  34. {
  35. modifiedAttack = 0;
  36. }
  37. }
  38.  
  39. return modifiedAttack;
  40. }
  41. }
  42.  
  43. public override int MaxHealth
  44. {
  45. get
  46. {
  47. int modifiedHealth = BaseHealth;
  48.  
  49. foreach (Func<int, int> modifier in HealthModifiers)
  50. {
  51. modifiedHealth = modifier.Invoke(modifiedHealth);
  52.  
  53. if (modifiedHealth < 0)
  54. {
  55. modifiedHealth = 1;
  56. }
  57. }
  58.  
  59. foreach (Func<int, int> modifier in AuraHealthModifiers)
  60. {
  61. modifiedHealth = modifier.Invoke(modifiedHealth);
  62.  
  63. if (modifiedHealth < 0)
  64. {
  65. modifiedHealth = 1;
  66. }
  67. }
  68.  
  69. return modifiedHealth;
  70. }
  71. }
  72.  
  73. #region Constructor
  74.  
  75. public Minion(MinionCard card)
  76. {
  77. Player = card.Player;
  78. Card = card;
  79. Card.Minion = this;
  80.  
  81. BaseAttack = card.BaseAttack;
  82.  
  83. CurrentHealth = card.CurrentHealth;
  84. BaseHealth = card.BaseHealth;
  85.  
  86. HasTaunt = card.HasTaunt;
  87. HasCharge = card.HasCharge;
  88. HasPoison = card.HasPoison;
  89. HasWindfury = card.HasWindfury;
  90. HasDivineShield = card.HasDivineShield;
  91. IsElusive = card.IsElusive;
  92. IsForgetful = card.IsForgetful;
  93. IsStealth = card.IsStealth;
  94.  
  95. SpellPower = card.SpellPower;
  96.  
  97. CurrentArmor = 0;
  98.  
  99. Mechanics = Card.Mechanics;
  100. }
  101.  
  102. #endregion
  103.  
  104. #region Methods
  105.  
  106. public override void Attack(Character target)
  107. {
  108. Debugger.LogMinion(this, "starting attack to " + target.GetName());
  109.  
  110. // Removing stealth of the Minion
  111. IsStealth = false;
  112.  
  113. // Checking if minion is Forgetful
  114. if (IsForgetful)
  115. {
  116. // Checking if there's more than 1 enemy (hero + minions)
  117. if (Player.Enemy.Minions.Count > 0)
  118. {
  119. // Random 50% chance
  120. if (RNG.RandomBool())
  121. {
  122. // TODO : Forgetful animation/effect
  123.  
  124. // Creating a list of possible targets
  125. List<Character> possibleTargets = new List<Character>();
  126.  
  127. // Adding the enemy hero to the list
  128. possibleTargets.Add(Player.Enemy.Hero);
  129.  
  130. // Adding all enemy minions to the list
  131. foreach (Minion enemyMinion in Player.Enemy.Minions)
  132. {
  133. possibleTargets.Add(enemyMinion);
  134. }
  135.  
  136. // Removing the current target from the possible targets list
  137. possibleTargets.Remove(target);
  138.  
  139. // Selecting a target by random
  140. int randomTarget = Random.Range(0, possibleTargets.Count);
  141.  
  142. // Setting the current target as the random target
  143. target = possibleTargets[randomTarget];
  144.  
  145. Debugger.LogMinion(this, "switched target to " + target.GetTypeName() + " (forgetful)");
  146. }
  147. }
  148. }
  149.  
  150. // Firing OnPreAttack events
  151. MinionPreAttackEvent minionPreAttackEvent = EventManager.Instance.OnMinionPreAttack(this, target, CurrentAttack);
  152. Mechanics.OnPreAttack.OnNext(minionPreAttackEvent);
  153.  
  154. // Redefining target in case it changed when firing events
  155. target = minionPreAttackEvent.Target;
  156.  
  157. // Checking if the Attack was not cancelled
  158. if (minionPreAttackEvent.Status != PreStatus.Cancelled)
  159. {
  160. // Getting both characters current attack
  161. int attackerAttack = minionPreAttackEvent.DamageAmount;
  162. int targetAttack = target.CurrentAttack;
  163.  
  164. // Queueing attack animation
  165. ActionQueue.Instance.Add(() => Controller.AnimateAttack(target, attackerAttack, targetAttack), 1f);
  166.  
  167. if (target.IsHero())
  168. {
  169. // Damaging the enemy hero
  170. target.Damage(this, attackerAttack);
  171.  
  172. // Checking the death of the enemy hero
  173. target.CheckDeath();
  174. }
  175. else if (target.IsMinion())
  176. {
  177. Minion targetMinion = target.As<Minion>();
  178.  
  179. // Checking if both minions are still alive
  180. if (this.IsAlive() && target.IsAlive())
  181. {
  182. // Damaging both minions
  183. target.Damage(this, attackerAttack);
  184. this.Damage(target, targetAttack);
  185.  
  186. // Checking the death of both characters
  187. target.CheckDeath();
  188. this.CheckDeath();
  189.  
  190. // Checking for poison on both minions
  191. if (this.HasPoison)
  192. {
  193. if (attackerAttack > 0)
  194. {
  195. Debugger.LogMinion(this, "killed by posion of " + targetMinion.GetName());
  196.  
  197. EventManager.Instance.OnMinionPoisoned(targetMinion, this);
  198.  
  199. Destroy();
  200. }
  201. }
  202.  
  203. if (targetMinion.HasPoison)
  204. {
  205. if (targetAttack > 0)
  206. {
  207. Debugger.LogMinion(targetMinion, "killed by posion of " + this.GetName());
  208.  
  209. EventManager.Instance.OnMinionPoisoned(this, targetMinion);
  210.  
  211. Destroy();
  212. }
  213. }
  214. }
  215. }
  216.  
  217. // Adding 1 to the current turn attacks counter
  218. CurrentTurnAttacks++;
  219.  
  220. // Firing OnAttacked events
  221. MinionAttackedEvent minionAttackedEvent = EventManager.Instance.OnMinionAttacked(this, target);
  222. Mechanics.OnAttacked.OnNext(minionAttackedEvent);
  223. }
  224.  
  225. ActionQueue.Instance.Add(GameManager.Instance.GameUpdate, 0f);
  226. }
  227.  
  228. public override void Heal(int healAmount)
  229. {
  230. Debugger.LogMinion(this, "healing for " + healAmount);
  231.  
  232. // Firing OnMinionPreHeal events
  233. MinionPreHealEvent minionPreHealEvent = EventManager.Instance.OnMinionPreHeal(this, healAmount);
  234.  
  235. // TODO : Check if heal is transformed to damage and if so, call Damage instead
  236.  
  237. // Healing the minion
  238. CurrentHealth = Mathf.Min(CurrentHealth + minionPreHealEvent.HealAmount, MaxHealth);
  239.  
  240. // Firing OnMinionHealed events
  241. EventManager.Instance.OnMinionHealed(this, minionPreHealEvent.HealAmount);
  242.  
  243. // Showing Heal splat
  244. if (minionPreHealEvent.HealAmount > 0)
  245. {
  246. InterfaceManager.Instance.SpawnHealSplatOn(this.Controller, minionPreHealEvent.HealAmount);
  247. }
  248.  
  249. CheckEnrage();
  250.  
  251. ActionQueue.Instance.Add(GameManager.Instance.GameUpdate, 0f);
  252. }
  253.  
  254. public override void Damage(Character attacker, int damageAmount)
  255. {
  256. // Firing OnMinionPreDamage events
  257. MinionPreDamageEvent minionPreDamageEvent = EventManager.Instance.OnMinionPreDamage(attacker, this, damageAmount);
  258. Mechanics.OnPreDamage.OnNext(minionPreDamageEvent);
  259.  
  260. Debugger.LogMinion(this, "receiving " + minionPreDamageEvent.DamageAmount + " damage by " + attacker.GetName());
  261.  
  262. // Checking for Divine Shield
  263. if (HasDivineShield)
  264. {
  265. // Setting off Divine Shield
  266. HasDivineShield = false;
  267. }
  268. else
  269. {
  270. // Substracting the damage to the current health of the Minion
  271. CurrentHealth -= minionPreDamageEvent.DamageAmount;
  272. }
  273.  
  274. // TODO : Show health loss sprite + amount on token
  275.  
  276. // Creating the MinionDamagedEvent
  277. MinionDamagedEvent minionDamagedEvent = EventManager.Instance.OnMinionDamaged(attacker, this, minionPreDamageEvent.DamageAmount);
  278. Mechanics.OnDamaged.OnNext(minionDamagedEvent);
  279.  
  280. CheckEnrage();
  281.  
  282. ActionQueue.Instance.Add(GameManager.Instance.GameUpdate, 0f);
  283. }
  284.  
  285. public void CheckEnrage()
  286. {
  287. if (CurrentHealth != MaxHealth)
  288. {
  289. Mechanics.OnEnraged.OnNext(this);
  290. }
  291. else
  292. {
  293. Mechanics.OnDisenraged.OnNext(this);
  294. }
  295.  
  296. ActionQueue.Instance.Add(() => GameManager.Instance.GameUpdate(), 0f);
  297. }
  298.  
  299. public override void CheckDeath()
  300. {
  301. if (IsAlive() == false)
  302. {
  303. Debugger.LogMinion(this, "died (check)");
  304.  
  305. ActionQueue.Instance.Add(Die, 0f);
  306. }
  307. }
  308.  
  309. public void Die()
  310. {
  311. if (Player.DeadMinions.Contains(Card) == false)
  312. {
  313. Debugger.LogMinion(this, "died");
  314.  
  315. // TODO : Custom animations, sounds, etc ?
  316.  
  317. // Firing OnMinionDied events
  318. EventManager.Instance.OnMinionDied(this);
  319. Mechanics.Deathrattle.OnNext(this);
  320.  
  321. // Removing the minion from the board
  322. Remove();
  323.  
  324. // Adding the Minion to the list of dead Minions
  325. Player.DeadMinions.Add(Card);
  326.  
  327. GameManager.Instance.CurrentTurnDeadMinions++;
  328.  
  329. SoundManager.Instance.PlayMinionSound(Card, "Death", 0.25f);
  330. }
  331. else
  332. {
  333. Debugger.LogMinion(this, " is already in the dead minions list (ERROR)");
  334. }
  335.  
  336. GameManager.Instance.GameUpdate();
  337. }
  338.  
  339. public void Destroy()
  340. {
  341. Debugger.LogMinion(this, "destroyed");
  342.  
  343. // TODO : Custom animations, sounds, etc ?
  344.  
  345. // Firing OnMinionDied events
  346. EventManager.Instance.OnMinionDied(this);
  347. Mechanics.Deathrattle.OnNext(this);
  348.  
  349. // Removing the minion from the board
  350. Remove();
  351.  
  352. // Adding the Minion to the list of dead Minions
  353. Player.DeadMinions.Add(Card);
  354.  
  355. GameManager.Instance.CurrentTurnDeadMinions++;
  356.  
  357. SoundManager.Instance.PlayMinionSound(Card, "Death", 0.25f);
  358.  
  359. GameManager.Instance.GameUpdate();
  360. }
  361.  
  362. private void Remove()
  363. {
  364. Debugger.LogMinion(this, "removed");
  365.  
  366. // Removing the minion from the player list of minions
  367. Player.Minions.Remove(this);
  368.  
  369. // Removing the controller from the board and destroying it
  370. Player.BoardController.RemoveMinion(this);
  371.  
  372. // Delaying controller destroy
  373. Controller.StartCoroutine(DelayedRemove());
  374. }
  375.  
  376. private IEnumerator DelayedRemove()
  377. {
  378. yield return new WaitForSeconds(1);
  379.  
  380. // Destroying the controller
  381. Controller.DestroyController();
  382. }
  383.  
  384. // TODO : Animation
  385. public BaseCard ReturnToHand()
  386. {
  387. Debugger.LogMinion(this, "returns to Hand");
  388.  
  389. // Removing the minion from the board
  390. Remove();
  391.  
  392. // Creating a new copy of the card
  393. BaseCard returnedCard = Card.Copy();
  394.  
  395. // Adding the card to the Hand
  396. Player.AddCardToHand(returnedCard);
  397.  
  398. return returnedCard;
  399. }
  400.  
  401. // TODO : Animation
  402. public BaseCard ReturnToEnemyHand()
  403. {
  404. Debugger.LogMinion(this, "returns to enemy Hand");
  405.  
  406. // Removing the minion from the board
  407. Remove();
  408.  
  409. // Creating a new copy of the card
  410. BaseCard returnedCard = Card.Copy();
  411.  
  412. // Adding the card to the Hand
  413. Player.Enemy.AddCardToHand(returnedCard);
  414.  
  415. return returnedCard;
  416. }
  417.  
  418. // TODO : Animation
  419. public BaseCard ReturnToDeck()
  420. {
  421. Debugger.LogMinion(this, "returns to Deck");
  422.  
  423. // Removing the minion from the board
  424. Remove();
  425.  
  426. // Creating a new copy of the card
  427. BaseCard returnedCard = Card.Copy();
  428.  
  429. // Adding the card to the Deck
  430. Player.AddCardToDeck(Card.Copy());
  431.  
  432. return returnedCard;
  433. }
  434.  
  435. public void Silence()
  436. {
  437. Debugger.LogMinion(this, "silenced");
  438.  
  439. SoundManager.Instance.Play("Game_Mechanic_Silence");
  440.  
  441. Mechanics.RemoveAll();
  442. AttackModifiers.Clear();
  443. HealthModifiers.Clear();
  444.  
  445. if (CurrentHealth > MaxHealth)
  446. {
  447. CurrentHealth = MaxHealth;
  448. }
  449.  
  450. HasTaunt = false;
  451. HasCharge = false;
  452. HasPoison = false;
  453. HasWindfury = false;
  454. HasDivineShield = false;
  455. IsImmune = false;
  456. IsElusive = false;
  457. IsStealth = false;
  458. IsNonAttack = false;
  459.  
  460. SpellPower = 0;
  461.  
  462. IsFrozen = false;
  463. UnfreezeNextTurn = false;
  464. IsForgetful = false;
  465.  
  466. IsSilenced = true;
  467.  
  468. if (Card.MinionAura != null)
  469. {
  470. AuraManager.Instance.RemoveMinionAura(Card.MinionAura);
  471. }
  472.  
  473. if (Card.CardAura != null)
  474. {
  475. AuraManager.Instance.RemoveCardAura(Card.CardAura);
  476. }
  477.  
  478. if (Card.HeroPowerAura != null)
  479. {
  480. AuraManager.Instance.RemoveHeroPowerAura(Card.HeroPowerAura);
  481. }
  482.  
  483. if (Card.HeroAura != null)
  484. {
  485. AuraManager.Instance.RemoveHeroAura(Card.HeroAura);
  486. }
  487.  
  488. GameManager.Instance.GameUpdate();
  489. }
  490.  
  491. // TODO : Animation
  492. public void TransformInto(MinionCard minionCard)
  493. {
  494. Debugger.LogMinion(this, "transforming into " + minionCard.Name);
  495.  
  496. // Setting the owner of the card
  497. minionCard.SetOwner(Player);
  498.  
  499. MinionController adaptedController = (MinionController) Controller;
  500.  
  501. // Creating a new Minion and adapting the current controller
  502. Minion newMinion = new Minion(minionCard);
  503. newMinion.Controller = adaptedController;
  504. adaptedController.Minion = newMinion;
  505. adaptedController.HoverController.Card = minionCard;
  506.  
  507. // Updating visuals of the controller
  508. newMinion.Controller.UpdateSprites();
  509. newMinion.Controller.UpdateNumbers();
  510. adaptedController.HoverController.UpdateNumbers();
  511. adaptedController.HoverController.UpdateSprites();
  512.  
  513. // Replacing the old minion in the player minion list
  514. int oldMinionIndex = Player.Minions.IndexOf(this);
  515. Player.Minions[oldMinionIndex] = newMinion;
  516. }
  517.  
  518. #endregion
  519.  
  520. #region Getter Methods
  521.  
  522. public int GetPosition()
  523. {
  524. return Player.BoardController.GetPositionOf((MinionController) Controller);
  525. }
  526.  
  527. #endregion
  528.  
  529. #region Condition Checkers
  530.  
  531. public override bool IsMinion()
  532. {
  533. return true;
  534. }
  535.  
  536. public bool IsNextTo(Minion other)
  537. {
  538. if (this.IsFriendlyOf(other))
  539. {
  540. if (this.IsAlive() && other.IsAlive())
  541. {
  542. int positionDelta = this.Controller.As<MinionController>().BoardPosition - other.Controller.As<MinionController>().BoardPosition;
  543.  
  544. if (Math.Abs(positionDelta) == 1)
  545. {
  546. return true;
  547. }
  548. }
  549. }
  550.  
  551. return false;
  552. }
  553.  
  554. public override bool CanAttack()
  555. {
  556. if (CurrentAttack > 0)
  557. {
  558. if (IsNonAttack == false)
  559. {
  560. if (IsFrozen == false)
  561. {
  562. if (IsSleeping == false || HasCharge)
  563. {
  564. if (HasWindfury)
  565. {
  566. return CurrentTurnAttacks < 2;
  567. }
  568. else
  569. {
  570. return CurrentTurnAttacks < 1;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. return false;
  577. }
  578.  
  579. #endregion
  580. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement