From 246c62bb4bb4edba9414a57c490748de45137b34 Mon Sep 17 00:00:00 2001 From: Jason Tu Date: Mon, 10 Apr 2023 16:49:15 -0400 Subject: [PATCH 1/2] Fix player not exiting chopping state after depleting resource --- .../Jason Tu/StateMachine/StateBehaviour.cs | 9 +++++-- .../StateMachine/StateMachineBehaviour.cs | 17 ++++++------ Assets/Scenes/Game.unity | 27 +++++++++++++++++++ .../PlayerCollectingState.cs | 11 ++++++++ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Assets/Plugins/Jason Tu/StateMachine/StateBehaviour.cs b/Assets/Plugins/Jason Tu/StateMachine/StateBehaviour.cs index ef65653e..0b61830a 100644 --- a/Assets/Plugins/Jason Tu/StateMachine/StateBehaviour.cs +++ b/Assets/Plugins/Jason Tu/StateMachine/StateBehaviour.cs @@ -8,8 +8,13 @@ public abstract class StateBehaviour : MonoBehaviour { public class TransitionToArgs : EventArgs { - public StateEnumValue NewState; + public StateBehaviour NewState; } - public event EventHandler TransitionTo; + public event EventHandler OnTransitionTo; + + protected void TransitionTo(StateBehaviour newState) + { + OnTransitionTo?.Invoke(this, new TransitionToArgs { NewState = newState }); + } } diff --git a/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs b/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs index e8aebcb6..8f326368 100644 --- a/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs +++ b/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs @@ -30,37 +30,38 @@ public class StateMachineChangedArgs : EventArgs private void Start() { TransitionTo(initialState); - CurrentState.TransitionTo += StateBehaviour_OnTransitionTo; } private void OnDisable() { - CurrentState.TransitionTo -= StateBehaviour_OnTransitionTo; + if (CurrentState != null) + CurrentState.OnTransitionTo -= StateBehaviour_OnTransitionTo; } protected void TransitionTo(StateBehaviour newState) { // Disable current state, if there is any. if (CurrentState != null) + { CurrentState.enabled = false; + CurrentState.OnTransitionTo -= StateBehaviour_OnTransitionTo; + } // Enable new state. newState.enabled = true; + newState.OnTransitionTo += StateBehaviour_OnTransitionTo; // Emit change event. OnChanged?.Invoke(this, new StateMachineChangedArgs { OldState = CurrentState, NewState = newState }); // Update current state reference. CurrentState = newState; + + Debug.Log("Changed state:" + newState); } private void StateBehaviour_OnTransitionTo(object sender, StateBehaviour.TransitionToArgs args) { - CurrentState.TransitionTo -= StateBehaviour_OnTransitionTo; - - var newState = args.NewState.State; - newState.TransitionTo += StateBehaviour_OnTransitionTo; - - TransitionTo(newState); + TransitionTo(args.NewState); } } diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 3df5979a..d1715fa1 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -456,6 +456,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 44add19162a454c8e82448804e4430e4, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &3102984711005192729 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 7520308135305952835, guid: b7150f0c756f841548259da7df1bee5e, + type: 3} + m_PrefabInstance: {fileID: 4849677784639536218} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8f8abae68b4164198a69582533d50b13, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &3203872249585380618 PrefabInstance: m_ObjectHideFlags: 0 @@ -688,6 +700,16 @@ PrefabInstance: propertyPath: m_Name value: Player objectReference: {fileID: 0} + - target: {fileID: 7520308135305952832, guid: b7150f0c756f841548259da7df1bee5e, + type: 3} + propertyPath: playerMovingState + value: + objectReference: {fileID: 3102984711005192729} + - target: {fileID: 7520308135305952833, guid: b7150f0c756f841548259da7df1bee5e, + type: 3} + propertyPath: playerMovingState + value: + objectReference: {fileID: 3102984711005192729} - target: {fileID: 7520308135305952834, guid: b7150f0c756f841548259da7df1bee5e, type: 3} propertyPath: gameInput @@ -703,6 +725,11 @@ PrefabInstance: propertyPath: gameInput value: objectReference: {fileID: 63202250} + - target: {fileID: 7520308135305952838, guid: b7150f0c756f841548259da7df1bee5e, + type: 3} + propertyPath: playerMovingState + value: + objectReference: {fileID: 3102984711005192729} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: b7150f0c756f841548259da7df1bee5e, type: 3} --- !u!1001 &4996571555868802377 diff --git a/Assets/Scripts/Behaviours/PlayerStateMachine/PlayerCollectingState.cs b/Assets/Scripts/Behaviours/PlayerStateMachine/PlayerCollectingState.cs index 6792243d..3630d0ab 100644 --- a/Assets/Scripts/Behaviours/PlayerStateMachine/PlayerCollectingState.cs +++ b/Assets/Scripts/Behaviours/PlayerStateMachine/PlayerCollectingState.cs @@ -7,6 +7,10 @@ public abstract class PlayerCollectingState : StateBehaviour [NotNull] protected PlayerConfiguration playerConfiguration; + [SerializeField] + [NotNull(IgnorePrefab = true)] + protected PlayerMovingState playerMovingState; + protected Resource resourceBeingCollected; protected Quaternion desiredRotation; @@ -34,10 +38,16 @@ private void ResourceBeingCollected_OnCollectCompleted(object sender, EventArgs OnCollectCompleted(); } + private void ResourceBeingCollected_OnDepleted(object sender, EventArgs eventArgs) + { + TransitionTo(playerMovingState); + } + public void Initialize(Resource resourceBeingCollected) { this.resourceBeingCollected = resourceBeingCollected; resourceBeingCollected.OnCollectCompleted += ResourceBeingCollected_OnCollectCompleted; + resourceBeingCollected.OnDepleted += ResourceBeingCollected_OnDepleted; desiredRotation = GetDesiredRotation(resourceBeingCollected, transform); } @@ -46,6 +56,7 @@ protected virtual void OnDisable() if (resourceBeingCollected != null) { resourceBeingCollected.OnCollectCompleted -= ResourceBeingCollected_OnCollectCompleted; + resourceBeingCollected.OnDepleted -= ResourceBeingCollected_OnDepleted; resourceBeingCollected.ResetRemainingTime(); } } From 1074472dc1d6f24d7bb12438dd854ce3bd140d72 Mon Sep 17 00:00:00 2001 From: Jason Tu Date: Mon, 10 Apr 2023 16:50:59 -0400 Subject: [PATCH 2/2] Some cleanup --- .../StateMachine/StateMachineBehaviour.cs | 2 -- Assets/Prefabs/Player.prefab | 3 +++ Assets/Scenes/Game.unity | 27 ------------------- 3 files changed, 3 insertions(+), 29 deletions(-) diff --git a/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs b/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs index 8f326368..7736595c 100644 --- a/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs +++ b/Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs @@ -56,8 +56,6 @@ protected void TransitionTo(StateBehaviour newState) // Update current state reference. CurrentState = newState; - - Debug.Log("Changed state:" + newState); } private void StateBehaviour_OnTransitionTo(object sender, StateBehaviour.TransitionToArgs args) diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index d40f3c46..42ffcdad 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -409,6 +409,7 @@ MonoBehaviour: m_EditorClassIdentifier: playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758, type: 2} + playerMovingState: {fileID: 7520308135305952835} playerAnimationEvents: {fileID: 7873346240423897778} --- !u!114 &7520308135305952833 MonoBehaviour: @@ -424,6 +425,7 @@ MonoBehaviour: m_EditorClassIdentifier: playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758, type: 2} + playerMovingState: {fileID: 7520308135305952835} playerAnimationEvents: {fileID: 7873346240423897778} --- !u!114 &7520308135305952838 MonoBehaviour: @@ -439,6 +441,7 @@ MonoBehaviour: m_EditorClassIdentifier: playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758, type: 2} + playerMovingState: {fileID: 7520308135305952835} playerAnimationEvents: {fileID: 7873346240423897778} --- !u!1 &6524875693616862465 GameObject: diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index d1715fa1..3df5979a 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -456,18 +456,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 44add19162a454c8e82448804e4430e4, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!114 &3102984711005192729 stripped -MonoBehaviour: - m_CorrespondingSourceObject: {fileID: 7520308135305952835, guid: b7150f0c756f841548259da7df1bee5e, - type: 3} - m_PrefabInstance: {fileID: 4849677784639536218} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8f8abae68b4164198a69582533d50b13, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1001 &3203872249585380618 PrefabInstance: m_ObjectHideFlags: 0 @@ -700,16 +688,6 @@ PrefabInstance: propertyPath: m_Name value: Player objectReference: {fileID: 0} - - target: {fileID: 7520308135305952832, guid: b7150f0c756f841548259da7df1bee5e, - type: 3} - propertyPath: playerMovingState - value: - objectReference: {fileID: 3102984711005192729} - - target: {fileID: 7520308135305952833, guid: b7150f0c756f841548259da7df1bee5e, - type: 3} - propertyPath: playerMovingState - value: - objectReference: {fileID: 3102984711005192729} - target: {fileID: 7520308135305952834, guid: b7150f0c756f841548259da7df1bee5e, type: 3} propertyPath: gameInput @@ -725,11 +703,6 @@ PrefabInstance: propertyPath: gameInput value: objectReference: {fileID: 63202250} - - target: {fileID: 7520308135305952838, guid: b7150f0c756f841548259da7df1bee5e, - type: 3} - propertyPath: playerMovingState - value: - objectReference: {fileID: 3102984711005192729} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: b7150f0c756f841548259da7df1bee5e, type: 3} --- !u!1001 &4996571555868802377