I’m making a “condition system” that I use in my targeting system to get an appropriate target, in Ability to specify the conditions under which the ability can be cast, and in my triggering system to check if some conditions are met before executing some actions.
For example this is a condition:
2
[Serializable]
public abstract class BaseCondition
{
[SerializeField] private readonly Candidate _candidate;
public abstract bool Check(params DynamicCandidate[] candidates);
}
[Serializable]
public class Condition : BaseCondition
{
[SerializeField] private Candidate _candidate;
[SerializeField] private Game.Condition _condition;
public override bool Check(params DynamicCandidate[] candidates)
{
return _condition.Check(_candidate.GetValue(candidates));
}
}
I have a bunch of real conditions (Game.Condition) such as “HasItem”, “IsInState”, “HasStatusEffect”, etc. that derive from condition. I also have some And/Or conditions as well.
And in the _candidate field you can put one of those inside:
1
public abstract class Candidate
{
public abstract Game.Candidate GetValue(params DynamicCandidate[] candidates);
}
[Serializable]
public class DynamicCandidate : Candidate
{
[SerializeField] private readonly RoleType _role;
private readonly GameObject _value;
public DynamicCandidate(GameObject value, RoleType role)
{
_value = value;
_role = role;
}
public override Game.Candidate GetValue(params DynamicCandidate[] candidates)
{
for (int i = 0; i < candidates.Length; i++)
{
if (candidates[i]._role == _role) return new ActorCandidate(candidates[i]._value);
}
throw new NullReferenceException("RoleType");
}
}
[Serializable]
public class StaticCandidate : Candidate
{
[SerializeField] private readonly GameObject _value;
public override Game.Candidate GetValue(params DynamicCandidate[] candidates)
{
return new ActorCandidate(_value);
}
}
This lets you use either a GameObject that already exists in the scene or use an object that was involved (targeter, target, caster, etc.).
This gives great flexibility in how spells select targets: you could make a spell check the caster’s health. If it’s over 100, it heals an ally, if it’s not, it hurts an enemy. You could make an ability only castable when the caster has a certain item. Just some examples. In the future I’d like to make it even more flexible and have conditions that can check stuff about an ability / status effect.
The thing is I don’t know if this is the best way to do this though as I haven’t see any such system before so I’m a bit curious as to what everyone does?