diff --git a/StateMachine/Assets/StateMachine/CustomAction.cs b/StateMachine/Assets/StateMachine/CustomAction.cs new file mode 100644 index 0000000..90e54e6 --- /dev/null +++ b/StateMachine/Assets/StateMachine/CustomAction.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CustomAction : MonoBehaviour, IStateAction +{ + public void OnEnter() + { + Debug.Log("Entering state..."); + } + + public void OnExit() + { + Debug.Log("Exiting state..."); + } + + public void OnTransition() + { + Debug.Log("Transitioning state..."); + } +} diff --git a/StateMachine/Assets/StateMachine/IStateAction.cs b/StateMachine/Assets/StateMachine/IStateAction.cs new file mode 100644 index 0000000..c03a97f --- /dev/null +++ b/StateMachine/Assets/StateMachine/IStateAction.cs @@ -0,0 +1,10 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public interface IStateAction +{ + void OnEnter(); + void OnExit(); + void OnTransition(); +} diff --git a/StateMachine/Assets/StateMachine/NewState.cs b/StateMachine/Assets/StateMachine/NewState.cs index 6eecabb..e8977d1 100644 --- a/StateMachine/Assets/StateMachine/NewState.cs +++ b/StateMachine/Assets/StateMachine/NewState.cs @@ -13,10 +13,13 @@ public class NewState public List stateTransitions = new List(); + public List actions = new List(); + public NewState(string stateName, StateExtension stateScript) { this.stateName = stateName; this.stateScript = stateScript; + this.actions = new List(); } } @@ -25,4 +28,4 @@ public class StateTransition { public string stateTransitionName; public BoolData boolData; -} \ No newline at end of file +} diff --git a/StateMachine/Assets/StateMachine/NewestStateMachine.cs b/StateMachine/Assets/StateMachine/NewestStateMachine.cs index bd32205..cc49781 100644 --- a/StateMachine/Assets/StateMachine/NewestStateMachine.cs +++ b/StateMachine/Assets/StateMachine/NewestStateMachine.cs @@ -34,6 +34,7 @@ void Start() currentState = states.Contains(states[0]) ? states[0] : null; orginalState = currentState; stateDictionary[currentState].enabled = true; + ExecuteOnEnterActions(currentState); } } @@ -72,12 +73,14 @@ public void SetState(NewState newState) if (newState != currentState) { + ExecuteOnExitActions(currentState); previousState = currentState; currentState = newState; stateDictionary[previousState].enabled = false; stateDictionary[orginalState].enabled = false; stateDictionary[currentState].enabled = true; + ExecuteOnEnterActions(currentState); } } @@ -113,6 +116,7 @@ void StateTransition() { if (stateTransition.boolData.GetData()) { + ExecuteOnTransitionActions(currentState); SetState(states.Find(state => state.stateName == stateTransition.stateTransitionName)); stateTransition.boolData.SetData(false); } @@ -121,6 +125,51 @@ void StateTransition() } + void ExecuteOnEnterActions(NewState state) + { + foreach (var action in state.actions) + { + try + { + action.OnEnter(); + } + catch (System.Exception ex) + { + Debug.LogError($"Error executing OnEnter action: {ex.Message}"); + } + } + } + + void ExecuteOnExitActions(NewState state) + { + foreach (var action in state.actions) + { + try + { + action.OnExit(); + } + catch (System.Exception ex) + { + Debug.LogError($"Error executing OnExit action: {ex.Message}"); + } + } + } + + void ExecuteOnTransitionActions(NewState state) + { + foreach (var action in state.actions) + { + try + { + action.OnTransition(); + } + catch (System.Exception ex) + { + Debug.LogError($"Error executing OnTransition action: {ex.Message}"); + } + } + } + // Update is called once per frame void Update() { @@ -175,4 +224,4 @@ void Update() // EditorUtility.SetDirty(stateMachine); // } // } -// } \ No newline at end of file +// } diff --git a/StateMachine/Assets/StateMachine/StateMachineUI.cs b/StateMachine/Assets/StateMachine/StateMachineUI.cs new file mode 100644 index 0000000..6ee667c --- /dev/null +++ b/StateMachine/Assets/StateMachine/StateMachineUI.cs @@ -0,0 +1,59 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class StateMachineUI : MonoBehaviour +{ + public Dropdown stateDropdown; + public Button addActionButton; + public InputField actionInputField; + public NewestStateMachine stateMachine; + + void Start() + { + PopulateStateDropdown(); + addActionButton.onClick.AddListener(AddActionToState); + } + + void PopulateStateDropdown() + { + stateDropdown.ClearOptions(); + List stateNames = new List(); + foreach (var state in stateMachine.states) + { + stateNames.Add(state.stateName); + } + stateDropdown.AddOptions(stateNames); + } + + void AddActionToState() + { + string selectedStateName = stateDropdown.options[stateDropdown.value].text; + NewState selectedState = stateMachine.states.Find(state => state.stateName == selectedStateName); + + if (selectedState != null) + { + string actionName = actionInputField.text; + IStateAction newAction = CreateAction(actionName); + if (newAction != null) + { + selectedState.actions.Add(newAction); + Debug.Log($"Action {actionName} added to state {selectedStateName}"); + } + else + { + Debug.LogError($"Failed to create action: {actionName}"); + } + } + } + + IStateAction CreateAction(string actionName) + { + // Here you can implement logic to create different types of actions based on the actionName + // For simplicity, we will just create a CustomAction + GameObject actionObject = new GameObject(actionName); + CustomAction customAction = actionObject.AddComponent(); + return customAction; + } +} diff --git a/StateMachine/Assets/Tool Box #2/Data/BoolData.cs b/StateMachine/Assets/Tool Box #2/Data/BoolData.cs index 94cc18f..cb501eb 100644 --- a/StateMachine/Assets/Tool Box #2/Data/BoolData.cs +++ b/StateMachine/Assets/Tool Box #2/Data/BoolData.cs @@ -13,8 +13,15 @@ public class BoolData : DataBase // Set the bool public void SetData(bool newData) { - data = newData; - //Debug.Log(data); + try + { + data = newData; + //Debug.Log(data); + } + catch (System.Exception ex) + { + Debug.LogError($"Error setting data: {ex.Message}"); + } } // when a bool is true turn another bool false