From 6cf7f721f3af6f79f982d7b5608b494ef2636a20 Mon Sep 17 00:00:00 2001 From: p3v1Xamk Date: Sat, 25 Mar 2023 21:06:43 +0200 Subject: [PATCH] Got rid of FSMBaseState and added RemainState bool to FSMState #1. Fixed scriptable references #2. Removed RemainState #3. --- .../{RemainInState.asset => RemainState.asset} | 7 +++++-- ...nInState.asset.meta => RemainState.asset.meta} | 2 +- .../CharacterAndAI/Characters/FSMCharacter.cs | 4 ++-- .../FSMActions/FSMActionInterestingObject.cs | 1 + .../CharacterAndAI/FSMActions/FSMActionPatrol.cs | 2 ++ Assets/Scripts/CharacterAndAI/FSMBaseState.cs | 6 ------ .../Scripts/CharacterAndAI/FSMBaseState.cs.meta | 11 ----------- Assets/Scripts/CharacterAndAI/FSMRemainState.cs | 7 ------- .../Scripts/CharacterAndAI/FSMRemainState.cs.meta | 11 ----------- Assets/Scripts/CharacterAndAI/FSMState.cs | 15 ++++++++++++--- Assets/Scripts/CharacterAndAI/FSMTransition.cs | 8 ++++---- 11 files changed, 27 insertions(+), 47 deletions(-) rename Assets/FSMScriptables/States/{RemainInState.asset => RemainState.asset} (66%) rename Assets/FSMScriptables/States/{RemainInState.asset.meta => RemainState.asset.meta} (79%) delete mode 100644 Assets/Scripts/CharacterAndAI/FSMBaseState.cs delete mode 100644 Assets/Scripts/CharacterAndAI/FSMBaseState.cs.meta delete mode 100644 Assets/Scripts/CharacterAndAI/FSMRemainState.cs delete mode 100644 Assets/Scripts/CharacterAndAI/FSMRemainState.cs.meta diff --git a/Assets/FSMScriptables/States/RemainInState.asset b/Assets/FSMScriptables/States/RemainState.asset similarity index 66% rename from Assets/FSMScriptables/States/RemainInState.asset rename to Assets/FSMScriptables/States/RemainState.asset index 7ba62ba..e397518 100644 --- a/Assets/FSMScriptables/States/RemainInState.asset +++ b/Assets/FSMScriptables/States/RemainState.asset @@ -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 diff --git a/Assets/FSMScriptables/States/RemainInState.asset.meta b/Assets/FSMScriptables/States/RemainState.asset.meta similarity index 79% rename from Assets/FSMScriptables/States/RemainInState.asset.meta rename to Assets/FSMScriptables/States/RemainState.asset.meta index 2afd523..b7b1112 100644 --- a/Assets/FSMScriptables/States/RemainInState.asset.meta +++ b/Assets/FSMScriptables/States/RemainState.asset.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f0bf743df33397041be856c48cd2e3af +guid: 7b4b1cfac11b40946b3710170e9de320 NativeFormatImporter: externalObjects: {} mainObjectFileID: 11400000 diff --git a/Assets/Scripts/CharacterAndAI/Characters/FSMCharacter.cs b/Assets/Scripts/CharacterAndAI/Characters/FSMCharacter.cs index 621564d..aeefec6 100644 --- a/Assets/Scripts/CharacterAndAI/Characters/FSMCharacter.cs +++ b/Assets/Scripts/CharacterAndAI/Characters/FSMCharacter.cs @@ -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; diff --git a/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionInterestingObject.cs b/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionInterestingObject.cs index 2c0358a..2a492f2 100644 --- a/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionInterestingObject.cs +++ b/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionInterestingObject.cs @@ -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(); AISensor enemySightSensor = stateMachine.GetComponent(); diff --git a/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionPatrol.cs b/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionPatrol.cs index ac62d89..3411c70 100644 --- a/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionPatrol.cs +++ b/Assets/Scripts/CharacterAndAI/FSMActions/FSMActionPatrol.cs @@ -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(); diff --git a/Assets/Scripts/CharacterAndAI/FSMBaseState.cs b/Assets/Scripts/CharacterAndAI/FSMBaseState.cs deleted file mode 100644 index 89d4f9a..0000000 --- a/Assets/Scripts/CharacterAndAI/FSMBaseState.cs +++ /dev/null @@ -1,6 +0,0 @@ -using UnityEngine; - -public class FSMBaseState : ScriptableObject -{ - public virtual void Execute(FSMCharacter machine) { } -} diff --git a/Assets/Scripts/CharacterAndAI/FSMBaseState.cs.meta b/Assets/Scripts/CharacterAndAI/FSMBaseState.cs.meta deleted file mode 100644 index cd4693c..0000000 --- a/Assets/Scripts/CharacterAndAI/FSMBaseState.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 418cff0100766ba4bafd668a74ae3f02 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/CharacterAndAI/FSMRemainState.cs b/Assets/Scripts/CharacterAndAI/FSMRemainState.cs deleted file mode 100644 index 0046ce0..0000000 --- a/Assets/Scripts/CharacterAndAI/FSMRemainState.cs +++ /dev/null @@ -1,7 +0,0 @@ -using UnityEngine; - -[CreateAssetMenu(menuName = "FSM/Remain In State", fileName = "RemainInState")] -public sealed class FSMRemainState : FSMBaseState -{ - -} diff --git a/Assets/Scripts/CharacterAndAI/FSMRemainState.cs.meta b/Assets/Scripts/CharacterAndAI/FSMRemainState.cs.meta deleted file mode 100644 index ec5d4eb..0000000 --- a/Assets/Scripts/CharacterAndAI/FSMRemainState.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 2acdbe22ce1dc2a499e3937dd7c25642 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/CharacterAndAI/FSMState.cs b/Assets/Scripts/CharacterAndAI/FSMState.cs index 3fdb913..8a53af3 100644 --- a/Assets/Scripts/CharacterAndAI/FSMState.cs +++ b/Assets/Scripts/CharacterAndAI/FSMState.cs @@ -2,17 +2,26 @@ using UnityEngine; [CreateAssetMenu(menuName = "FSM/State")] -public sealed class FSMState : FSMBaseState +public class FSMState : ScriptableObject { public List Action = new List(); public List Transitions = new List(); + 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) diff --git a/Assets/Scripts/CharacterAndAI/FSMTransition.cs b/Assets/Scripts/CharacterAndAI/FSMTransition.cs index 6be65f7..596df60 100644 --- a/Assets/Scripts/CharacterAndAI/FSMTransition.cs +++ b/Assets/Scripts/CharacterAndAI/FSMTransition.cs @@ -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; }