Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
using System;
using System.Collections.Generic;
using Fluctio.FluctioSim.Core.Components.MachineLearning.TriggerActions;
using UnityEngine;

namespace Fluctio.FluctioSim.Core.Components.MachineLearning.Triggers
{
	[AddComponentMenu("")] // hide component from menu
	[DefaultExecutionOrder(1000)] // pretty much after everything, at least after all actions
	public class ActionsExecutor : MonoBehaviour
	{
		
		#region Actions
		
		private static readonly List<TriggerAction> ScheduledActions = new();
		
		public static void ScheduleActions(IReadOnlyList<TriggerAction> actions)
		{
			ScheduledActions.AddRange(actions);
		}

		private static void ExecuteActions()
		{
			var actionsToExecute = new List<TriggerAction>(ScheduledActions);
			ScheduledActions.Clear();
			actionsToExecute.Sort();
			actionsToExecute.ForEach(action => action.OnExecute());
		}

		private void FixedUpdate()
		{
			ExecuteActions();
		}
		
		#endregion

		#region Singleton and creation

		private static ActionsExecutor _instance = null;

		private static void CheckOneInstance(ActionsExecutor allowedInstance)
		{
			if (_instance != null)
			{
				throw new InvalidOperationException($"{nameof(ActionsExecutor)} is singleton, but something tried to create multiple instances");
			}
		}

		[RuntimeInitializeOnLoadMethod]
		private static void Init()
		{
			CheckOneInstance(null);
			var gameObject = new GameObject(nameof(ActionsExecutor))
			{
				hideFlags = HideFlags.HideInHierarchy,
			};
			_instance = gameObject.AddComponent<ActionsExecutor>();
		}

		private void Awake()
		{
			CheckOneInstance(this);
		}

		#endregion
	}
}