Hi guys,
I have set up my item system and my weapons are currently created in the same model : scriptable objects derived classes.
So far, I’ve got a Base Item class inheriting Scriptable Objects, and my BaseWeapon class derives from BaseItem.
So far, so good.
Now, I have to set up my damages system, every weapon has different damages type (think Physical, Fire Whatever …) and Enemy (and player) will have a Defense that combine the corresponding type (PhysicalDef, FireDef…).
So when a weapon hits, I would (very basically) sum all the different type and subtract the different defenses.
It looks pretty easy so far but I want to add a sort of RPG attribute system (Strength, Intellect etc) that would impact the associated damages types…
So, how would you guys go with a system like this ?
I was thinking of a “Damages Manager” that would be responsible for containing static methods for dealing damages but I don’t really know how to pass all the appropriate variables.
Using arguments is my first thought, but a basic weapon with just physical damages would imply to pass a lot of =0 variables and it seems like a big waste and a a pretty inflexible solution.
Any input, or advice would be really appreciated !
don’t pass the values one by one, pass a reference to the weapon and the target, the calculating class can then access the components/attributes from those references.
Ok, that makes total sense
So the pattern would look like :
a template weapon script that pretty much checks for collisions with enemy
if collision, template gets the reference from the current weapon and enemy health and send it to the damages manager which does its magic and actually apply the damages.
Where would the attributes kick in ? I guess I can just gets them at the moment of the damages calculation but I don’t know why it sounds a little bit “noobish” to me ^^
[EDIT] By the way, my attributes are also Scriptable Objects stored in a list in my game saved data.
You have to get them somehow. Most likely this will be like a getStats() call or something. It has to be done and it is the logical way. I see it as a couple of options. Your damage manager could take in the following: The player and enemy and from there get their armor/weapons/stats and calculate OR could take in stats that were already prepped. Either way it’s the same, it just depends on you want it to do that will fit your needs. You can always look for “the best way” but honestly sometimes the simplest is the best and doesn’t affect anything. Also once you get it all connected and working, you might figure out a better way and refactor everything. This is common and will happen a lot and because you programmed it out you were able to see it in a different way so it became better over time.
Right now, here is what my DamagesManager looks like.Please feel free to point ouf every flaws ^^
I’ve just implemented ONE modifier for now, but obviously it’s pretty much the same idea for calculating every damages type.
public class DamagesManager : MonoBehaviour{
public static DamagesManager Instance;
private int base_Physical;
private int base_Magic;
private int base_Fire;
private int base_Ice;
private int base_Dark;
private int modifierConstant = 200;
private List<Attribute> attributeList;
void Awake () {
if (Instance == null) {
DontDestroyOnLoad (gameObject);
Instance = this;
} else if (Instance != this) {
Destroy(gameObject);
}
if (attributeList == null) {
attributeList = GameManager.Instance.savedPlayerData.attributeList;
}
}
public static void CalculateDamagesOnPlayer(BaseWeapon weapon, Defense defense){
Instance.base_Physical = weapon.baseDamage_Physical;
Instance.base_Magic = weapon.baseDamage_Magic; //
Instance.base_Fire = weapon.baseDamage_Fire;
Instance.base_Ice = weapon.baseDamage_Ice;
Instance.base_Dark = weapon.baseDamage_Dark;
int Physical_Total = weapon.baseDamage_Physical - defense.defense_Physical; // TODO Got to add modifier with Stregnth ...
int Magic_Total = weapon.baseDamage_Magic - defense.defense_Magic; // add modifier Intelect... etc
int Fire_Total = weapon.baseDamage_Fire - defense.defense_Fire;
int Ice_Total = weapon.baseDamage_Ice - defense.defense_Ice;
int Dark_Total = weapon.baseDamage_Dark - defense.defense_Dark;
int TotalDamages = Physical_Total + Magic_Total + Fire_Total + Ice_Total + Dark_Total;
var health = defense.gameObject.GetComponent<Health> ();
if (health != null) {
health.takeDamage (TotalDamages);
}
}
public static void CalculateDamagesOnEnemy(BaseWeapon weapon, Defense defense){
Instance.base_Physical = (weapon.baseDamage_Physical+((weapon.baseDamage_Physical * Instance.attributeList[0].attributeValue)/Instance.modifierConstant)); // strength is attribute 0 in list
Instance.base_Magic = weapon.baseDamage_Magic; // TODO Got to multiply/divide/whatever by Intelect attribute ... etc
Instance.base_Fire = weapon.baseDamage_Fire;
Instance.base_Ice = weapon.baseDamage_Ice;
Instance.base_Dark = weapon.baseDamage_Dark;
int Physical_Total = weapon.baseDamage_Physical - defense.defense_Physical;
int Magic_Total = weapon.baseDamage_Magic - defense.defense_Magic;
int Fire_Total = weapon.baseDamage_Fire - defense.defense_Fire;
int Ice_Total = weapon.baseDamage_Ice - defense.defense_Ice;
int Dark_Total = weapon.baseDamage_Dark - defense.defense_Dark;
int TotalDamages = Physical_Total + Magic_Total + Fire_Total + Ice_Total + Dark_Total;
var healthEnemy = defense.gameObject.GetComponent<EnemyHealth> ();
if (healthEnemy != null) {
healthEnemy.currentHealth -= TotalDamages;
}
}
}
That looks fine to me. It will work. I’d say to keep going with the idea and see if it expands in complexity and at that point, I would think about refactoring it into a better system if needed. But at the point you are at, over architecturing it takes away from the game progress