Skip to content

Instantly share code, notes, and snippets.

@JorisVanEijden
Last active August 14, 2019 17:50
Show Gist options
  • Save JorisVanEijden/0e9c18bc35709e4eecd2b3b9719f3e80 to your computer and use it in GitHub Desktop.
Save JorisVanEijden/0e9c18bc35709e4eecd2b3b9719f3e80 to your computer and use it in GitHub Desktop.
Example alteration system
public delegate int Formula1I(ref int result, int a);
private struct WeightedCallback<T>
{
public int Weight;
public T Callback;
}
private static readonly Dictionary<string, List<WeightedCallback<Formula1I>>> FormulaAlters1I = new Dictionary<string, List<WeightedCallback<Formula1I>>>();
public static void RegisterFormulaAlter(string formula, Formula1I callback, int weight = 1)
{
WeightedCallback<Formula1I> weightedCallback = new WeightedCallback<Formula1I> {Weight = weight, Callback = callback};
List<WeightedCallback<Formula1I>> alterations;
if (FormulaAlters1I.TryGetValue(formula, out alterations))
{
alterations.Add(weightedCallback);
alterations = alterations.OrderBy(o => o.Weight).ToList();
}
else
{
alterations = new List<WeightedCallback<Formula1I>>();
}
FormulaAlters1I[formula] = alterations;
}
private static void ApplyAlterations1I(string name, int input, ref int result)
{
List<WeightedCallback<Formula1I>> alterations;
if (FormulaAlters1I.TryGetValue(name, out alterations))
{
foreach (var alteration in alterations)
{
alteration.Callback.Invoke(ref result, input);
}
}
}
public static int DamageModifier(int strength)
{
int result = (int)Mathf.Floor((float)(strength - 50) / 5f);
ApplyAlterations1I("DamageModifier", strength, ref result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment