[ad_1]
I’m making an attempt to make a dynamic buff system, the place every element is manipulating the bottom worth of the ability. Those elements needs to be executed in a sure order however the order inside the identical kind doesn’t matter.
All modifiers have the identical foundation.
public interface IBaseMod {
float ApplyModifier(float unique);
}
There exists for now three completely different layers which might be construct on this.
public interface IPlusMod: IBaseMod {}
public interface IPMultMod: IBaseMod {}
public interface IPCapMod: IBaseMod {}
Those are used for the precise implementation, specifically
public class Add35: MonoBehaviour, IPlusMod {
public float ApplyModifier(float unique) {
return unique + 35;
}
}
public class Mult2: MonoBehaviour, IMultMod {
public float ApplyModifier(float unique) {
return unique * 2;
}
}
public class Cap50: MonoBehaviour, ICapMod {
public float ApplyModifier(float unique) {
return unique <= 50 ? unique : 50;
}
}
I’ve a take a look at ability that has these three elements
Now inside the ability I would really like the entry the implementation of the modifier in an outlined approach.
public class TestSkill: MonoBehaviour {
public void Start() {
Test1();
Test2();
Test3();
}
personal void Test1() {
float baseValue = 5f;
foreach (var mod in GetComponents<IBaseMod>()) {
baseValue = mod.ApplyModifier(baseValue);
}
Debug.LogFormat("Test1 returns {0}", baseValue);
}
personal void Test2() {
float baseValue = 5f;
foreach (var mod in GetComponents<IPlusMod>()) {
baseValue = mod.ApplyModifier(baseValue);
}
foreach (var mod in GetComponents<IMultMod>()) {
baseValue = mod.ApplyModifier(baseValue);
}
foreach (var mod in GetComponents<ICapMod>()) {
baseValue = mod.ApplyModifier(baseValue);
}
Debug.LogFormat("Test2 returns {0}", baseValue);
}
public interface[] order = new interface[]{IPlusMod, IMultMod, ICapMod};
personal void Test3() {
float baseValue = 5f;
foreach (var modType so as) {
foreach (var mod in GetComponents<modType>()) {
baseValue = mod.ApplyModifier(baseValue);
}
}
Debug.LogFormat("Test3 returns {0}", baseValue);
}
enum MODTYP {CAP, MULT, ADD};
personal MODTYP[] order = new MODTYP[3] {MODTYP.ADD, MODTYP.MULT, MODTYP.CAP};
personal IBaseMod[] helper(MODTYP modtyp) {
change (modtyp) {
case MODTYP.ADD: {
return GetComponents<IPlusMod>();
}
case MODTYP.MULT: {
return GetComponents<IMultMod>();
}
case MODTYP.CAP: {
return GetComponents<ICapMod>();
}
}
return null;
}
personal void Test4() {
float baseValue = 5;
foreach (var modType so as) {
foreach (var mod in helper(modType)) {
baseValue = mod.ApplyModifier(baseValue);
}
}
Debug.LogFormat("Test4 returns {0}", baseValue);
}
}
The return worth for Test1() is 45 and for Test2() is 50. I wish to have the results of Test2() in a approach that’s not onerous coded (these 3 interfaces are for now solely an instance, I would really like to have the ability to change the order dynamically at runtime and never at all times have all of them). With Test4() the order could be modified at runtime with the assistance of an enum that defines the order and a helper class that has a case for every of the enum and a corresponding GetComponents. That nonetheless feels a bit off.
If doable I wish to have a logic much like Test3() the place I can retailer the order in an array or listing, iterate over it and use that as the sort for GetComponents. When doable I wish to keep away from that the interface itself has the order hardcoded.
[ad_2]
