Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 0) Add a Parry in input (I use Fire2)
- * 1) In ThirdPerson Controller add a Motion BlockParry call it "As your input name" and set
- * Start/Stop with Down and Up, if you like add an Effect (like Stamina/Mana drain).
- * 2) Add a bool parameter to Animator and set it as false.
- * 3) Add an animation inside the Animator (layer Full Body) called "Parry", and set the exit on Parry=False.
- * Also don't forget to uncheck the Has Exit Time.
- * 4) This code require 2 stats Dexterity and Constitution, so you have to add it to your stats
- * and also inside the Stats Handler inside the player.
- * Optionally you can change the script at lines 106 and 107
- * 5) In Equipment Handler/Items select your shield, then add 2 new CallBack, drag 'n drop your player on the null field
- * and choose BlockParry script and set OnEquip and OnUnequip.
- *
- * // A check is required to be sure that if the character is holding a weapon (sword) and have no shied,
- * // the parry will be called by the animation with start-stop sistem so, like in Prince Of Persia (the first in 2D)
- * // the blocking system will be effective just on certain frames.
- *
- * That's it, Have fun :)
- */
- using System.Linq;
- using UnityEngine;
- using DevionGames.UIWidgets;
- using DevionGames.InventorySystem;
- using DevionGames.StatSystem;
- namespace DevionGames
- {
- public class BlockParry : MotionState
- {
- private StatsHandler statsHandler;
- private EquipmentItem item;
- private ObjectProperty equipmentArmor;
- [SerializeField]
- private StatEffect effect;
- /// <summary>
- /// This method have to be CallBack from Equipment Handler:
- /// Click on "Items" and set the callback to run this method OnEquip!
- /// </summary>
- public void OnEquip()
- {
- statsHandler = gameObject.GetComponent<StatsHandler>();
- Stat armor = statsHandler.GetStat("Armor");
- ItemContainer container = WidgetUtility.Find<ItemContainer>("Equipment");
- item = container.GetItems<EquipmentItem>().Where(x => x.Name.Contains("Shield")).FirstOrDefault();
- equipmentArmor = item.FindProperty("Armor");
- statsHandler.AddModifier("Armor", -equipmentArmor.intValue, StatModType.Flat, gameObject);
- }
- /// <summary>
- /// This method have to be CallBack from Equipment Handler:
- /// Click on "Items" and set the callback to run this method OnUnEquip!
- ///
- /// Note: whe have re-add the shield value, 'cause the RPGKit will remove the Shield Armor value
- /// from the Armor stats, so if you have 4 + 30 from the shield you'll get 4 - 30 = -26 xD
- /// </summary>
- public void OnUnequip()
- {
- statsHandler = gameObject.GetComponent<StatsHandler>();
- statsHandler.RemoveModifiersFromSource("Armor", gameObject);
- }
- public override void OnStart()
- {
- if (m_Animator.GetBool("In Combat") == false)
- m_Animator.SetBool("In Combat", true);
- m_Animator.SetBool("Parry", true);
- statsHandler = gameObject.GetComponent<StatsHandler>();
- Stat armor = statsHandler.GetStat("Armor");
- armor.Add(GetShieldArmor());
- if(item != null)
- ApplyEffect(true);
- }
- public override void OnStop()
- {
- Stat armor = statsHandler.GetStat("Armor");
- armor.Subtract(GetShieldArmor());
- m_Animator.SetBool("Parry", false);
- if (item != null)
- ApplyEffect(false);
- }
- // This 2 methods are called from Sword Block animation, like we see in Prince Of Persia (2D Game)
- // were you're supposed to rise the block at the right time.
- public void SwordParryStart() { OnStart(); }
- public void SwordParryStop() { OnStop(); }
- /// <summary>
- /// This method will look at Equipment and if any shield get equipped will read the Armor value and return it.
- /// If no Shield is equipped it will take Dexterity + Constitution / 2 to emulate a sort block made by arm.
- /// </summary>
- private float GetShieldArmor()
- {
- ItemContainer container = WidgetUtility.Find<ItemContainer>("Equipment");
- //Get EquipmentItem(Type) by name.
- item = container.GetItems<EquipmentItem>().Where(x => x.Name.Contains("Shield")).FirstOrDefault();
- statsHandler = gameObject.GetComponent<StatsHandler>();
- Stat dex = statsHandler.GetStat("Dexterity");
- Stat cons = statsHandler.GetStat("Constitution");
- if (item != null)
- return item.FindProperty("Armor").intValue + (dex.Value + cons.Value / 2);
- else
- return dex.Value + cons.Value / 2;
- }
- private void ApplyEffect(bool toggle)
- {
- statsHandler = gameObject.GetComponent<StatsHandler>();
- if (effect != null)
- {
- if(toggle)
- statsHandler.AddEffect(effect);
- else
- statsHandler.RemoveEffect(effect);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement