// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System;
using System.Collections.Generic;
using System.Linq;
using Doozy.Runtime.Common.Extensions;
using Doozy.Runtime.Signals;
namespace Doozy.Runtime.Mody
{
/// Base class for ModyEvents designed to trigger one or more s
[Serializable]
public abstract class ModyEventBase
{
/// Default event name
public const string k_DefaultEventName = "Unnamed";
/// Name of the event
public string EventName;
/// List of action runners that trigger set actions on referenced modules
public List Runners;
/// Returns TRUE if the Runners count is greater than zero
public bool hasRunners => Runners.Count > 0;
/// Returns TRUE this ModyEvent has runners
public virtual bool hasCallbacks => hasRunners;
protected ModyEventBase() : this(k_DefaultEventName) {}
protected ModyEventBase(string eventName)
{
EventName = eventName;
Runners = new List();
}
/// Execute the event
public virtual void Execute(Signal signal = null)
{
Runners.RemoveNulls();
Runners.ForEach(r => r.Execute());
}
/// Run the action with the given action name on the target
/// Target ModyModule
/// Name of the action
/// True if the operation was successful and false otherwise
public bool RunsAction(ModyModule module, string actionName)
{
Runners.RemoveNulls();
return
Runners
.Where(runner => runner.Module == module)
.Any(runner => runner.ActionName.Equals(actionName));
}
/// Runs the actions on the given target
/// Target ModyModule
/// True if the operation was successful and false otherwise
public bool RunsModule(ModyModule module)
{
Runners.RemoveNulls();
return
Runners
.Any(runner => runner.Module == module);
}
}
/// Extension methods for
public static class ModyEventBaseExtensions
{
/// Set the event name for the target
/// Target ModyEventBase
/// Event name
/// Type of ModyEventBase
public static T SetEventName(this T target, string eventName) where T : ModyEventBase
{
target.EventName = eventName;
return target;
}
}
}