A cool way to do this, is to add “modifiers” or component scripts to your character. You do this by using Polymorphism and a base class.
Here is an example:
using UnityEngine;
using System.Collections;
public class Modifier : MonoBehaviour {
public float timeRemaining = 10;
public string modifierText = "Generic Modifier";
public int order = 0;
// Update is called once per frame
void Update () {
DoTimer ();
}
public Character GetCharacter(){
return transform.root.gameObject.GetComponent<Character>();
}
public Character GetTarget(){
Character chr = transform.root.gameObject.GetComponent<Character>();
if(!chr) return null;
return chr.target;
}
public virtual void DoTimer(){
if (timeRemaining != Mathf.Infinity) {
timeRemaining -= Time.deltaTime;
}
if (timeRemaining <= 0) Destroy (this);
}
public virtual float ModifyDamage(float value){ return value; }
public virtual float ModifyAccuracy(float value){ return value; }
public virtual float ModifyAvoidance(float value){ return value; }
public virtual float ModifyHealing(float value){ return value; }
public virtual float ModifyDamageResistance(float value){ return value; }
// these are here for after effects, like heal on damage.
public virtual void FinalDamage(float value){}
public virtual void FinalHeal(float value){}
}
using this as a base class, you crate things like this:
using UnityEngine;
using System.Collections;
public class Blessed : Modifier {
public float timeRemaining = Mathf.Infinity;
public string modifierText = "Blessed, damage +1, healing + 1";
public override float ModifyDamage(float value) {
return value + 1;
}
public override void FinalDamage(float value){
Character chr = GetCharacter ();
if (!chr) return;
chr.ApplyHealing (ModifyHealing (1));
}
}
Now, all you have to do is to run a loop on all the modifiers and call specific things. like
value = mod.ModifyDamage(value);
This changes the value. Using the order, you simply count to the max order number and modify them in order.
I did probably about 20 scripts just to make sure things worked out ok. I did things like food buffs, debuffs, cursed items debuff throwing modifiers (like acidic, gives an acid debuff) I did protectors, like AidProtection which stops damage. (the acid debuff looks for the AcidProtection and stops it from being attached or stops the damage if it is attached. The timer part works just fine because I am subclassing it,all I have to do is to rewrite the time remaining part. I use Mathf.Infinity to make permenant modifiers, like a Acidic sword is permenant, but a SpellBlessed sword would not be permenant.
I can check the character for all modifiers and apply them to the damage, healing and other things. it is very expandable IMO.
I created the Character base class, which is what the Blessed item does. and apply healing every hit. I also created a Weapon base class, with default attack and hit stuff.
Since this is an entire system and involves way too many scripts, I am not going to share it all here.