- Home /
Damage System - How to reduce damage properly?
Well the whole subject is much more complex than the headline states so I'll try to explain it as best as I can.
I have a Unit class, AttackMissile class and UnitManager class. Unit class call coroutine on AttackMissile class which moves the projectile toward target, when it reaches the target it should deal damage.
UnitManager class contains bunch of event that are embedded in methods to be called correspondingly to recreate some states that unit passes (Unit attacks / unit is attacked, unit deals damage / unit takes damage and etc) so I can attach different spells effect to this states. I'll take Dota's Axe for example and his passive, which triggers on UnitIsAttacked event. Let's say that for my example I want a spell that reduces damage by some % when UnitTakesDamage. In TestSpellClass I'll add a reference to which unit is monitored (Axe in our case) and i will subscribe the method that performs all about spell to the UnitTakesDamageEvent in UnitManager class by calling method UnitManager.Instance.UnitTakesDamageTrigger(some parameters...) which does following
public void UnitTakesDamageTrigger(some parameters...)
{
if(UnitTakesDamageEvent != null)
UnitTakesDamageEvent.Invoke(some parameters...)
}
And now comes the tricky part. If I want to deal damage in the end of coroutine that was called on the AttackMissile class, so my missile has reached target and now I need to deal damage, I call the method to invoke all effects that would affect the damage to be changed (in this case test ability), how can I do that? I can't pass objects by reference to the events or either have events with return type, I get that. What if that same damage is affected by let's say bonus 50 dmg, then % reduction of 15% and flat reduction of 15 dmg. That bring me to conclusion that i should have also damage handling class, DamageHandleClass where I would have some sort of array that hold all modifications of that damage, that performs those modification and them returns or deals that damage.
My question is am I thinking in right direction, and has anybody else stumbled upon same problem. I'm not asking for code solution just an idea which now I currently lack totally.
Thank you in advance.
This is just a standard program$$anonymous$$g issue. The way you'd do it in Unity is the same as how you'd do it anywhere else. It sounds like the sorts of things done in $$anonymous$$$$anonymous$$O's, with complex chains of damage modifiers (which always have bugs.)
The only Unity part, I think, is how you send damage through OnCollisionEnter. Just give the missile a "damageType" script, and have OCE grab it. It can even have a pointer to the firer, so you can get a list of damage modifiers.
Well this is not a program$$anonymous$$g issue, it's more of a logic or how the system is designed. I've done it today, just as my initial thought was, I have an array for bonus dmg table and an object which stores data about damage bonus (flat or not, amount, percentage etc) and I can easly extend it toward my needs.
Also whole thing is wrapped with many events (aka triggers) on which I can easily attach any kind of neat effects/spells/abilites etc, works like a charm. The only problem I had was is multiple access to some class field (in my case damage) but since I implemented array data storing this issue has gone (I'm actually using list). I'll explain more detaily what I've done if anyone stumbles upon this post, I'm too tired right now.
Thanks for your insights anyway man. Respect
Logic and data-structure/class design are what program$$anonymous$$g is all about. At least, that's what I learned in most of my classes.
The stuff UnityAnswers tends to help with, are specific things unique to Unity: layers vs triggers & raycasts; how to fill in a regular class using the Inspector; using Send$$anonymous$$essage ... .
"How would I design" are more for the forum (which I don't much care for.) And even then, they tend to be specific Unity problems.
Program$$anonymous$$g is paradigm used to solve those logic problems because any kind of problem can be brought down to a mathematical structure or object whatever you call it, then solved using program$$anonymous$$g. But that's just my point of view, I study math so I tend to be geeky sometimes xD Anyway cheers, gonna sum up the answer now.
Answer by MDarkwing · Jul 02, 2014 at 07:13 AM
So just as my original idea was, I have a list of objects that hold all the reductions and bonuses for damage and one method that calculates the damage. Same thing can be applied to let's say attack speed, or some other stats (strength, agility, intelligence, armor whatever stats you have in your game)
Objects hold info about that damage I called the class BuffDamage (has: int id, float damageAmount, bool flat). Hope someone will find this usefull.