Very similarly to how we did it in Hit().
In this case, your projectiles will have to get the Elemental damages in their setup in addition to the normal damage.
Our projectiles are spawned in the WeaponConfig, which happens to already have the ElementalDamages array… (edit) First, we need to add an overload method for SetTarget() in Projectile.cs:
private IEnumerable<ElementalDamage> elementalDamages;
public void SetTarget(Health target, GameObject instigator, float calculatedDamage, IEnumerable<ElementalDamage> elementalDamages)
{
this.projTarget = target;
this.damage = damage;
this.instigator = instigator;
this.elementalDamages = elementalDamages;
Destroy(gameObject, maxLifetime);
}
And in WeaponConfig.LaunchProjectile, we want to add our IEnumerable to the parameters:
public void LaunchProjectile(Transform rightHandTransform, Transform leftHandTransform, Transform spellbookTransform, Health target, GameObject instigator, float calculatedDamage)
{
Projectile projectileInstance = Instantiate(projectile, GetTransforms(rightHandTransform, leftHandTransform, spellbookTransform).position, Quaternion.identity);
projectileInstance.SetTarget(target, instigator, calculatedDamage, GetElementalDamages());
}
Finally, in the OnTrigger method of Projectile, we need to apply any elementalDamages that were sent to the projectile:
private void OnTriggerEnter(Collider other)
{
if(other.GetComponent<Health>() != projTarget) return;
if(projTarget.IsDead()) return;
projTarget.TakeDamage(instigator, damage);
foreach (ElementalDamage elementalDamage in elementalDamages)
{
damage = elementalDamage.amount;
float boosts = 0;
foreach (IElementalDamageProvider provider in instigator.GetComponents<IElementalDamageProvider>())
{
foreach (float amount in provider.GetElementalDamageBoost(elementalDamage.damageType))
{
boosts += amount;
}
}
boosts /= 100f;
float resistances = 0;
foreach (IElementalResistanceProvider provider in projTarget.GetComponents<IElementalResistanceProvider>())
{
foreach (float amount in provider.GetElementalResistance(elementalDamage.damageType))
{
resistances += amount;
}
}
resistances /= 100f;
damage += damage * boosts;
damage -= damage * resistances;
if (damage <= 0) continue;
//Line below changed to add damage type for elemental damage.
projTarget.TakeDamage(gameObject, damage, elementalDamage.damageType.ToString());
}
projSpeed = 0; //Prevents piercing as it is right now, may change later
if(hitEffect != null)
{
Instantiate(hitEffect, GetAimLocation(), transform.rotation);
}
if(destroyOnHit == null)
{
Destroy(gameObject);
Destroy(hitEffect);
}
foreach(GameObject toDestroy in destroyOnHit)
{
Destroy(toDestroy);
}
Destroy(gameObject, lifeAfterImpact);
}