Skip to content

Commit

Permalink
Got rid of FSMBaseState and added RemainState bool to FSMState #1. Fi…
Browse files Browse the repository at this point in the history
…xed scriptable references #2. Removed RemainState #3.
  • Loading branch information
p3v1Xamk committed Mar 25, 2023
1 parent 8f5c716 commit 6cf7f72
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ MonoBehaviour:
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2acdbe22ce1dc2a499e3937dd7c25642, type: 3}
m_Name: RemainInState
m_Script: {fileID: 11500000, guid: 568f58b8f3dcbe048bf5380145a1305a, type: 3}
m_Name: RemainState
m_EditorClassIdentifier:
Action: []
Transitions: []
RemainState: 0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/Scripts/CharacterAndAI/Characters/FSMCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public class FSMCharacter : NavMeshAgentCharacter
{
[SerializeField, Tooltip("State from which the AI starts executing next states depending on decisions.")] private FSMBaseState _initialState;
[ReadOnly, Tooltip("Current state for debugging.")] public FSMBaseState CurrentState;
[SerializeField, Tooltip("State from which the AI starts executing next states depending on decisions.")] private FSMState _initialState;
[ReadOnly, Tooltip("Current state for debugging.")] public FSMState CurrentState;

public enum EmotionalState { Default, Interested, Afraid }
public EmotionalState CurrentEmotionalState = EmotionalState.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class FSMActionInterestingObject : FSMAction
{
public override void Execute(FSMCharacter stateMachine)
{
base.Execute(stateMachine);
Debug.Log("Execute sight interesting object");
var navMeshAgent = stateMachine.GetComponent<NavMeshAgent>();
AISensor enemySightSensor = stateMachine.GetComponent<AISensor>();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/CharacterAndAI/FSMActions/FSMActionPatrol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class FSMActionPatrol : FSMAction
public float CloseEnoughDistance = 0.5f;
public override void Execute(FSMCharacter stateMachine)
{
base.Execute(stateMachine);
Debug.Log("Execute Patrol Action");
if (!stateMachine.Agent.hasPath || stateMachine.Agent.remainingDistance < CloseEnoughDistance)
{
stateMachine.SetNextPatrolPointDestination();
Expand Down
6 changes: 0 additions & 6 deletions Assets/Scripts/CharacterAndAI/FSMBaseState.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/CharacterAndAI/FSMBaseState.cs.meta

This file was deleted.

7 changes: 0 additions & 7 deletions Assets/Scripts/CharacterAndAI/FSMRemainState.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/CharacterAndAI/FSMRemainState.cs.meta

This file was deleted.

15 changes: 12 additions & 3 deletions Assets/Scripts/CharacterAndAI/FSMState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
using UnityEngine;

[CreateAssetMenu(menuName = "FSM/State")]
public sealed class FSMState : FSMBaseState
public class FSMState : ScriptableObject
{
public List<FSMAction> Action = new List<FSMAction>();
public List<FSMTransition> Transitions = new List<FSMTransition>();
public bool RemainState = false;

public override void Execute(FSMCharacter machine)
public virtual void Execute(FSMCharacter machine)
{
foreach (var action in Action)
{
if (action.ExecuteOnce && !action.Executed)
Debug.Log("Execute state action: " + action);
if (action.ExecuteOnce/* && !action.Executed*/)
{
if (!action.Executed)
action.Execute(machine);
}
else
{
action.Execute(machine);
}
}

foreach (var transition in Transitions)
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/CharacterAndAI/FSMTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
public sealed class FSMTransition : ScriptableObject
{
public FSMDecision Decision;
public FSMBaseState TrueState;
public FSMBaseState FalseState;
public FSMState TrueState;
public FSMState FalseState;

public void Execute(FSMCharacter stateMachine)
{
if (Decision.Decide(stateMachine) && !(TrueState is FSMRemainState))
if (Decision.Decide(stateMachine) && !TrueState.RemainState)
{
//TrueState and falseState to simply FSMState instead of base state.
//
stateMachine.CurrentState = TrueState;
}
else if (!(FalseState is FSMRemainState))
else if (!FalseState.RemainState)
{
stateMachine.CurrentState = FalseState;
}
Expand Down

0 comments on commit 6cf7f72

Please sign in to comment.