Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions StateMachine/Assets/StateMachine/CustomAction.cs
Original file line number Diff line number Diff line change
@@ -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...");
}
}
10 changes: 10 additions & 0 deletions StateMachine/Assets/StateMachine/IStateAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IStateAction
{
void OnEnter();
void OnExit();
void OnTransition();
}
5 changes: 4 additions & 1 deletion StateMachine/Assets/StateMachine/NewState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public class NewState

public List<StateTransition> stateTransitions = new List<StateTransition>();

public List<IStateAction> actions = new List<IStateAction>();

public NewState(string stateName, StateExtension stateScript)
{
this.stateName = stateName;
this.stateScript = stateScript;
this.actions = new List<IStateAction>();
}
}

Expand All @@ -25,4 +28,4 @@ public class StateTransition
{
public string stateTransitionName;
public BoolData boolData;
}
}
51 changes: 50 additions & 1 deletion StateMachine/Assets/StateMachine/NewestStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Start()
currentState = states.Contains(states[0]) ? states[0] : null;
orginalState = currentState;
stateDictionary[currentState].enabled = true;
ExecuteOnEnterActions(currentState);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -113,6 +116,7 @@ void StateTransition()
{
if (stateTransition.boolData.GetData())
{
ExecuteOnTransitionActions(currentState);
SetState(states.Find(state => state.stateName == stateTransition.stateTransitionName));
stateTransition.boolData.SetData(false);
}
Expand All @@ -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()
{
Expand Down Expand Up @@ -175,4 +224,4 @@ void Update()
// EditorUtility.SetDirty(stateMachine);
// }
// }
// }
// }
59 changes: 59 additions & 0 deletions StateMachine/Assets/StateMachine/StateMachineUI.cs
Original file line number Diff line number Diff line change
@@ -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<string> stateNames = new List<string>();
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<CustomAction>();
return customAction;
}
}
11 changes: 9 additions & 2 deletions StateMachine/Assets/Tool Box #2/Data/BoolData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down