namespace Zinnia.Rule
{
using System;
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Extension;
using Zinnia.Rule.Collection;
///
/// Matches a given object against a collections of rules and emits the associated event.
///
public class RulesMatcher : MonoBehaviour
{
///
/// The rule and event association that can be matched against.
///
[Serializable]
public class Element
{
[Tooltip("The rule to match against.")]
[SerializeField]
private RuleContainer rule;
///
/// The rule to match against.
///
public RuleContainer Rule
{
get
{
return rule;
}
set
{
rule = value;
}
}
///
/// Emitted when the is valid.
///
public UnityEvent Matched = new UnityEvent();
}
[Tooltip("A collection of rules to potentially match against.")]
[SerializeField]
private RulesMatcherElementObservableList elements;
///
/// A collection of rules to potentially match against.
///
public RulesMatcherElementObservableList Elements
{
get
{
return elements;
}
set
{
elements = value;
}
}
///
/// Attempts to match the given object to the rules within the collection. If a match occurs then the appropriate event is emitted.
///
/// The source to provide to the rule for validity checking.
public virtual void Match(object source)
{
if (!this.IsValidState() || Elements == null)
{
return;
}
foreach (Element element in Elements.NonSubscribableElements)
{
if (element.Rule.Accepts(source))
{
element.Matched?.Invoke();
}
}
}
}
}