Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix player not exiting chopping state after depleting resource #50

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions Assets/Plugins/Jason Tu/StateMachine/StateBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ public abstract class StateBehaviour : MonoBehaviour
{
public class TransitionToArgs : EventArgs
{
public StateEnumValue NewState;
public StateBehaviour NewState;
}

public event EventHandler<TransitionToArgs> TransitionTo;
public event EventHandler<TransitionToArgs> OnTransitionTo;

protected void TransitionTo(StateBehaviour newState)
{
OnTransitionTo?.Invoke(this, new TransitionToArgs { NewState = newState });
}
}
15 changes: 7 additions & 8 deletions Assets/Plugins/Jason Tu/StateMachine/StateMachineBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ 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 });
Expand All @@ -56,11 +60,6 @@ protected void TransitionTo(StateBehaviour 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);
}
}
3 changes: 3 additions & 0 deletions Assets/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758,
type: 2}
playerMovingState: {fileID: 7520308135305952835}
playerAnimationEvents: {fileID: 7873346240423897778}
--- !u!114 &7520308135305952833
MonoBehaviour:
Expand All @@ -424,6 +425,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758,
type: 2}
playerMovingState: {fileID: 7520308135305952835}
playerAnimationEvents: {fileID: 7873346240423897778}
--- !u!114 &7520308135305952838
MonoBehaviour:
Expand All @@ -439,6 +441,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
playerConfiguration: {fileID: 11400000, guid: 0e29962fbc3e34aaea3620693e304758,
type: 2}
playerMovingState: {fileID: 7520308135305952835}
playerAnimationEvents: {fileID: 7873346240423897778}
--- !u!1 &6524875693616862465
GameObject:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -46,6 +56,7 @@ protected virtual void OnDisable()
if (resourceBeingCollected != null)
{
resourceBeingCollected.OnCollectCompleted -= ResourceBeingCollected_OnCollectCompleted;
resourceBeingCollected.OnDepleted -= ResourceBeingCollected_OnDepleted;
resourceBeingCollected.ResetRemainingTime();
}
}
Expand Down