Azeranth

Dynamic Unity Spell System

Jul 29th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SpellLogic : MonoBehaviour
  2. {
  3.     public SpellData SpellData;
  4.     SpellEffect SpellEffect;
  5.     // Use this for initialization
  6.     void Start ()
  7.     {
  8.         SpellEffect = SpellData.Effect;
  9.         SpellEffect.OnStart();
  10.         StartCoroutine(SpellEffect.OnTick());
  11.         Instantiate(SpellData.ParticleSystem, gameObject.transform);
  12.     }
  13.    
  14.     // Update is called once per frame
  15.     void Update ()
  16.     {
  17.        
  18.     }
  19. }
  20.  
  21. public class SpellData : ScriptableObject
  22. {
  23.     private string name;
  24.     private ResourceType resourceType;
  25.     private float resourceCost;
  26.     private ParticleSystem particleSystem;
  27.     private SpellEffect effect;
  28.  
  29.     public string Name
  30.     {
  31.         get
  32.         {
  33.             return name;
  34.         }
  35.  
  36.         set
  37.         {
  38.             name = value;
  39.         }
  40.     }
  41.     public ResourceType ResourceType
  42.     {
  43.         get
  44.         {
  45.             return resourceType;
  46.         }
  47.  
  48.         set
  49.         {
  50.             resourceType = value;
  51.         }
  52.     }
  53.     public float ResourceCost
  54.     {
  55.         get
  56.         {
  57.             return resourceCost;
  58.         }
  59.  
  60.         set
  61.         {
  62.             resourceCost = value;
  63.         }
  64.     }
  65.     public ParticleSystem ParticleSystem
  66.     {
  67.         get
  68.         {
  69.             return particleSystem;
  70.         }
  71.  
  72.         set
  73.         {
  74.             particleSystem = value;
  75.         }
  76.     }
  77.     public SpellEffect Effect
  78.     {
  79.         get
  80.         {
  81.             return effect;
  82.         }
  83.  
  84.         set
  85.         {
  86.             effect = value;
  87.         }
  88.     }
  89. }
  90.  
  91. using System.Collections;
  92. using System.Collections.Generic;
  93. using UnityEngine;
  94.  
  95. [CreateAssetMenu(fileName = "New Spell Effect", menuName = "Spell Effect")]
  96. public abstract class SpellEffect : ScriptableObject
  97. {
  98.     public GameObject Spell;
  99.     public float Duration = 1f;
  100.     public List<GameObject> Targets;
  101.     public List<StatusEffect> StatusEffects;
  102.     public CharacterController Caster;
  103.  
  104.     public abstract void OnStart();
  105.     public abstract IEnumerator OnTick();
  106. }
  107.  
  108. public class HealSpellEffect : SpellEffect
  109. {
  110.     public override void OnStart()
  111.     {
  112.         Destroy(Spell,Duration);
  113.     }
  114.     public override IEnumerator OnTick()
  115.     {
  116.         do
  117.         {
  118.             foreach (GameObject item in Targets)
  119.             {
  120.                 item.GetComponent<IDamagable>().ApplyDamage(HealAmount, DamageType);
  121.             }
  122.             yield return new WaitForSeconds(Interval);
  123.         }
  124.         while (true);
  125.     }
  126.  
  127.     public float HealAmount;
  128.     public DamageType DamageType;
  129.     public float Interval;
  130. }
  131.  
  132. public class PoisonSpellEffect : SpellEffect
  133. {
  134.     public float Interval;
  135.     public float Damage;
  136.     public DamageType DamageType;
  137.     private StatusEffect statusEffect = new StatusEffect { Name = "Poisoned", Description = "Deals damage repeatedly"};
  138.     public override void OnStart()
  139.     {
  140.         statusEffect.Duration = Duration;
  141.         statusEffect.Interval = Interval;
  142.         Destroy(Spell, Duration);
  143.         foreach (GameObject member in Targets)
  144.         {
  145.             statusEffect.OnProc += delegate { member.GetComponent<IDamagable>().ApplyDamage(Damage, DamageType); };
  146.             member.GetComponent<IAfflictable>().Afflict(statusEffect);
  147.         }
  148.     }
  149.     public override IEnumerator OnTick()
  150.     {
  151.         yield return null;
  152.     }
  153. }
  154.  
  155. public class StatusEffect : ScriptableObject
  156. {
  157.     public delegate void OnProcDelegate();
  158.     public delegate void OnExpireDelegate(StatusEffect target);
  159.  
  160.     private string name;
  161.     private string description;
  162.     private float duration;
  163.     private float interval;
  164.     private OnProcDelegate onProc;
  165.     private OnExpireDelegate onExpire;
  166.  
  167.     public string Name
  168.     {
  169.         get
  170.         {
  171.             return name;
  172.         }
  173.  
  174.         set
  175.         {
  176.             name = value;
  177.         }
  178.     }
  179.     public string Description
  180.     {
  181.         get
  182.         {
  183.             return description;
  184.         }
  185.  
  186.         set
  187.         {
  188.             description = value;
  189.         }
  190.     }
  191.     public float Duration
  192.     {
  193.         get
  194.         {
  195.             return duration;
  196.         }
  197.  
  198.         set
  199.         {
  200.             duration = value;
  201.             if (duration <= 0) OnExpire(this);
  202.         }
  203.     }
  204.     public float Interval
  205.     {
  206.         get
  207.         {
  208.             return interval;
  209.         }
  210.  
  211.         set
  212.         {
  213.             interval = value;
  214.         }
  215.     }
  216.     public OnProcDelegate OnProc
  217.     {
  218.         get
  219.         {
  220.             return onProc;
  221.         }
  222.  
  223.         set
  224.         {
  225.             onProc = value;
  226.         }
  227.     }
  228.     public OnExpireDelegate OnExpire
  229.     {
  230.         get
  231.         {
  232.             return onExpire;
  233.         }
  234.  
  235.         set
  236.         {
  237.             onExpire = value;
  238.         }
  239.     }
  240.  
  241.     public IEnumerator Proc()
  242.     {
  243.         OnProc();
  244.         yield return new WaitForSeconds(Interval);
  245.     }
  246. }
  247.  
  248. public class CharacterController : MonoBehaviour, IAfflictable, IDamagable
  249. {
  250.     List<SpellData> spellList = new List<SpellData>();
  251.     List<StatusEffect> statusEffects = new List<StatusEffect>();
  252.  
  253.     public List<SpellData> SpellList
  254.     {
  255.         get
  256.         {
  257.             return spellList;
  258.         }
  259.  
  260.         set
  261.         {
  262.             spellList = value;
  263.         }
  264.     }
  265.     public List<StatusEffect> StatusEffects
  266.     {
  267.         get
  268.         {
  269.             return statusEffects;
  270.         }
  271.  
  272.         set
  273.         {
  274.             statusEffects = value;
  275.         }
  276.     }
  277.    
  278.     #region Methods
  279.     void Start()
  280.     {
  281.  
  282.     }
  283.  
  284.     void Update()
  285.     {
  286.  
  287.     }
  288.  
  289.     public void Afflict(StatusEffect statusEffect)
  290.     {
  291.         statusEffect.OnExpire += RemoveStatusEffect;
  292.         StartCoroutine(statusEffect.Proc());
  293.         StatusEffects.Add(statusEffect);
  294.     }
  295.     public void RemoveStatusEffect(StatusEffect Target)
  296.     {
  297.         StatusEffects.Remove(Target);
  298.     }
  299.  
  300.     public float ApplyDamage(float Damage, DamageType DamageType)
  301.     {
  302.         return 0f;
  303.     }
  304.     #endregion
  305. }
Add Comment
Please, Sign In to add comment