Advertisement
MaximilianPs

RPG KIT - Block/Parry

Jan 24th, 2022 (edited)
389
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  0) Add a Parry in input (I use Fire2)
  3.  *  1) In ThirdPerson Controller add a Motion BlockParry call it "As your input name" and set
  4.  *     Start/Stop with Down and Up, if you like add an Effect (like Stamina/Mana drain).
  5.  *  2) Add a bool parameter to Animator and set it as false.
  6.  *  3) Add an animation inside the Animator (layer Full Body) called "Parry", and set the exit on Parry=False.
  7.  *      Also don't forget to uncheck the Has Exit Time.
  8.  *  4) This code require 2 stats Dexterity and Constitution, so you have to add it to your stats
  9.  *     and also inside the Stats Handler inside the player.
  10.  *     Optionally you can change the script at lines 106 and 107
  11.  *  5) In Equipment Handler/Items select your shield, then add 2 new CallBack, drag 'n drop your player on the null field
  12.  *     and choose BlockParry script and set OnEquip and OnUnequip.
  13.  *  
  14.  *  // A check is required to be sure that if the character is holding a weapon (sword) and have no shied,
  15.  *  // the parry will be called by the animation with start-stop sistem so, like in Prince Of Persia (the first in 2D)
  16.  *  // the blocking system will be effective just on certain frames.
  17.  *  
  18.  *  That's it, Have fun :)
  19.  */
  20.  
  21. using System.Linq;
  22. using UnityEngine;
  23. using DevionGames.UIWidgets;
  24. using DevionGames.InventorySystem;
  25. using DevionGames.StatSystem;
  26.  
  27. namespace DevionGames
  28. {
  29.     public class BlockParry : MotionState
  30.     {
  31.         private StatsHandler statsHandler;
  32.         private EquipmentItem item;
  33.         private ObjectProperty equipmentArmor;
  34.         [SerializeField]
  35.         private StatEffect effect;
  36.  
  37.         /// <summary>
  38.         /// This method have to be CallBack from Equipment Handler:
  39.         /// Click on "Items" and set the callback to run this method OnEquip!
  40.         /// </summary>
  41.         public void OnEquip()
  42.         {
  43.             statsHandler = gameObject.GetComponent<StatsHandler>();
  44.             Stat armor = statsHandler.GetStat("Armor");
  45.  
  46.             ItemContainer container = WidgetUtility.Find<ItemContainer>("Equipment");
  47.             item = container.GetItems<EquipmentItem>().Where(x => x.Name.Contains("Shield")).FirstOrDefault();
  48.             equipmentArmor = item.FindProperty("Armor");
  49.  
  50.             statsHandler.AddModifier("Armor", -equipmentArmor.intValue, StatModType.Flat, gameObject);
  51.         }
  52.  
  53.         /// <summary>
  54.         /// This method have to be CallBack from Equipment Handler:
  55.         /// Click on "Items" and set the callback to run this method OnUnEquip!
  56.         ///
  57.         /// Note: whe have re-add the shield value, 'cause the RPGKit will remove the Shield Armor value
  58.         ///       from the Armor stats, so if you have 4 + 30 from the shield you'll get 4 - 30 = -26 xD
  59.         /// </summary>
  60.         public void OnUnequip()
  61.         {
  62.             statsHandler = gameObject.GetComponent<StatsHandler>();
  63.             statsHandler.RemoveModifiersFromSource("Armor", gameObject);
  64.         }
  65.  
  66.         public override void OnStart()
  67.         {
  68.             if (m_Animator.GetBool("In Combat") == false)
  69.                 m_Animator.SetBool("In Combat", true);
  70.  
  71.             m_Animator.SetBool("Parry", true);
  72.  
  73.             statsHandler = gameObject.GetComponent<StatsHandler>();
  74.            
  75.             Stat armor = statsHandler.GetStat("Armor");
  76.             armor.Add(GetShieldArmor());
  77.            
  78.             if(item != null)
  79.                 ApplyEffect(true);
  80.         }
  81.  
  82.         public override void OnStop()
  83.         {
  84.             Stat armor = statsHandler.GetStat("Armor");
  85.             armor.Subtract(GetShieldArmor());
  86.             m_Animator.SetBool("Parry", false);
  87.  
  88.             if (item != null)
  89.                 ApplyEffect(false);
  90.         }
  91.  
  92.         // This 2 methods are called from Sword Block animation, like we see in Prince Of Persia (2D Game)
  93.         // were you're supposed to rise the block at the right time.
  94.         public void SwordParryStart() { OnStart(); }
  95.  
  96.         public void SwordParryStop()  { OnStop();  }
  97.  
  98.         /// <summary>
  99.         /// This method will look at Equipment and if any shield get equipped will read the Armor value and return it.
  100.         /// If no Shield is equipped it will take Dexterity + Constitution / 2 to emulate a sort block made by arm.
  101.         /// </summary>
  102.         private float GetShieldArmor()
  103.         {
  104.             ItemContainer container = WidgetUtility.Find<ItemContainer>("Equipment");
  105.  
  106.             //Get EquipmentItem(Type) by name.
  107.             item = container.GetItems<EquipmentItem>().Where(x => x.Name.Contains("Shield")).FirstOrDefault();
  108.            
  109.             statsHandler = gameObject.GetComponent<StatsHandler>();
  110.             Stat dex = statsHandler.GetStat("Dexterity");
  111.             Stat cons = statsHandler.GetStat("Constitution");
  112.  
  113.             if (item != null)
  114.                 return item.FindProperty("Armor").intValue + (dex.Value + cons.Value / 2);
  115.             else
  116.                 return dex.Value + cons.Value / 2;
  117.         }
  118.  
  119.         private void ApplyEffect(bool toggle)
  120.         {
  121.             statsHandler = gameObject.GetComponent<StatsHandler>();
  122.  
  123.             if (effect != null)
  124.             {
  125.                 if(toggle)
  126.                     statsHandler.AddEffect(effect);
  127.                 else
  128.                     statsHandler.RemoveEffect(effect);
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement