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

2532 app create state machine for animations #2537

Closed
wants to merge 14 commits into from

Conversation

0reo
Copy link
Contributor

@0reo 0reo commented Mar 9, 2022

closes #2532

Initial state machine which will be used for animations. There is already a state system in place for player actions, which this will work along side. Future updates to this state machine will allow for an action to trigger animations with multiple states which will be managed here.

This PR currently has a minimal actual implementation, Avatars register themselves for tracking, and states are registered for the player avatar with dummy functions, but it's not actually controlling anything right now(although it could). Submitting this now because I want access to this as I decouple the animation function handling from the render loop.

State machine can currently:

  • register any object for state tracking
  • trigger state activation/deactivation
  • hold related state data(state name, run time, etc)
  • provide access to all tracked objects, all the relevant state information for a those objects
  • work along side the existing code base until a full transition can be done
  • associate animation functions to be triggered when a state is active(not using this until animation functions are decoupled from animationHelper)
  • execute animation functions internally(not using this until animation functions are decoupled from animationHelper)

To help with testing, you can add the following to avatars.ja at line 1132, which will provide access to the state machine and avatar via the console.

    window.StateMachine = StateMachine;
    window.avatar = this;

you can test triggering the dummy functions by running the following in the console

StateMachine; //access state machine, view all registered objects and states
avatar.tracker; //see tracked instance of avatar
avatar.tracker.addToGraph(['jump']) //replace jump with any available action, make sure it's an array when adding to graph
avatar.tracker.activate('jump') //triggers selected state function
avatar.tracker.deactivate('jump') //stop selected state function

Also, I'd recommend hiding white space changes when reviewing

@0reo 0reo linked an issue Mar 9, 2022 that may be closed by this pull request
// add object to state machine
registerObj(name, obj) {
this.tracking.has(name) || this.tracking.set(name, {
id: crypto.randomUUID(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have makeId in utils:

app/util.js

Line 609 in 51acb44

export function makeId(length) {

It could be changed to crypto.randomUUID() but the codebase should have one place that defines the shape of IDs (since that may need to be changed).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm easy either way, I'll have the SM use makeId and we can have it use UUID's in another ticket if we want to

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moving to makeId is fine for now.

this.getActionsList().forEach((name) => {
this.avatar.tracker.registerState({
name,
animationFn: ()=>{console.log(`${name} func ran`);}
Copy link
Contributor

@avaer avaer Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging? Probably shouldn't ship this in PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for debugging, will remove

@avaer
Copy link
Contributor

avaer commented Mar 10, 2022

The state engine needs thinking about how we are blending states (lerp, multiply, or something else), in which order, and for which bone subset. The order and bone blend functions both matter a lot, and are not easily defined declaratively.

Is the plan just to copy the execution logic from Avatar's update, but to abstract the state setting?

@avaer
Copy link
Contributor

avaer commented Mar 10, 2022

window.StateMachine = StateMachine;
window.avatar = this;

If you want this you could just float this change yourself. I use the same trick but by committing this it actually breaks clean global assumptions for everyone.

Can we remove?

Copy link
Contributor

@avaer avaer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, some questions.

@0reo
Copy link
Contributor Author

0reo commented Mar 10, 2022

The state engine needs thinking about how we are blending states (lerp, multiply, or something else), in which order, and for which bone subset. The order and bone blend functions both matter a lot, and are not easily defined declaratively.

Is the plan just to copy the execution logic from Avatar's update, but to abstract the state setting?

I'm planning on creating an animation factory which will handle how blending is handled and other animation specific code. To keep things moving swiftly I'm setting things up the new system to follow the same general execution logic we're currently using, and once the new system is handling that I will continue to expand ordering. The state machine graph will be used to chain and order animation sub-states when actions trigger a state transition. For example, action A1 could trigger state S1, which has animation X, and action A2 could trigger state S2, which has animation X, then Y, then Z, then back to X. And yes, they aren't easily defined declaratively which is why that part's not yet included.

#2533 is the animation factory ticket, it doesn't reference bone subsets specifically but I'll make it explicit

@0reo
Copy link
Contributor Author

0reo commented Mar 10, 2022

window.StateMachine = StateMachine;
window.avatar = this;

If you want this you could just float this change yourself. I use the same trick but by committing this it actually breaks clean global assumptions for everyone.

Can we remove?

this is not actually in this PR, just a note for QA if it's helpful for them when testing

@0reo 0reo added this to the Animations milestone Mar 11, 2022
@@ -408,6 +409,9 @@ class Avatar {
};
}

const stateName = "playerAvatar"
StateMachine.registerObj(options.name ?? stateName, this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't used yet, but we cannot depend on options.name being unique, so this will collide and overwrite.

What was the intent here?

Copy link
Contributor

@avaer avaer Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why is this mutating its argument this rather than returning a tracker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering objects first checks to see if it's already being tracked on line 55 of StateMachine.js. If it is, it won't reregister it, it will just take the object being passed and provide it with the tracker.

@avaer
Copy link
Contributor

avaer commented Mar 11, 2022

The state mechanism probably needs a redesign. This PR is fine but it's not adding any functionality, and the state engine tracking is bugged (name collisions resulting in shared state).

When there is functionality to add we can re-open.

@avaer avaer closed this Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

App: Create state machine for animations
2 participants