//$ Copyright 2015-22, Code Respawn Technologies Pvt Ltd - All Rights Reserved $// using System.Collections.Generic; namespace DungeonArchitect.Utils { public class StackSystem where TSharedState : new() { protected TStaticState staticState; private Stack stack = new Stack(); private bool running = false; private bool foundResult = false; private TResult result; private TSharedState sharedState = new TSharedState(); public delegate void ExecuteFrameDelegate(TState top, TStaticState staticState, TSharedState sharedState, StackSystem stackSystem); public StackSystem(TStaticState staticState) { this.staticState = staticState; } public TSharedState SharedState => sharedState; public bool Running { get => running; } public bool FoundResult { get => foundResult; } public TResult Result { get => result; } public Stack Stack { get => stack; } public void Initialize(TState state) { stack.Push(state); running = true; } public void PushFrame(TState state) { stack.Push(state); } public void FinalizeResult(TResult result) { running = false; foundResult = true; this.result = result; } public void Halt() { running = false; } public void ExecuteStep(ExecuteFrameDelegate executeFrame) { if (stack.Count == 0) { running = false; } if (!running) { return; } var top = stack.Pop(); if (executeFrame != null) { executeFrame.Invoke(top, staticState, sharedState, this); } } } }