Skip to content

Commit

Permalink
refactor: opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhuang committed Sep 11, 2024
1 parent cadfe28 commit b2bdeed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/animation/Animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,23 @@ export class Animator extends Component {
/**
* Create a cross fade from the current state to another state.
* @param stateName - The state name
* @param duration - The duration of the transition (normalized)
* @param normalizedTransitionDuration - The duration of the transition (normalized)
* @param layerIndex - The layer index(default -1). If layer is -1, play the first state with the given state name
* @param normalizedTimeOffset - The time offset between 0 and 1(default 0)
*/
crossFade(stateName: string, duration: number, layerIndex: number = -1, normalizedTimeOffset: number = 0): void {
crossFade(
stateName: string,
normalizedTransitionDuration: number,
layerIndex: number = -1,
normalizedTimeOffset: number = 0
): void {
if (this._controllerUpdateFlag?.flag) {
this._reset();
}

const { state, layerIndex: playLayerIndex } = this._getAnimatorStateInfo(stateName, layerIndex);
const { manuallyTransition } = this._getAnimatorLayerData(playLayerIndex);
manuallyTransition.duration = duration;
manuallyTransition.duration = normalizedTransitionDuration;
manuallyTransition.offset = normalizedTimeOffset;
manuallyTransition.destinationState = state;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/animation/AnimatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AnimatorController extends ReferResource {
* Add a parameter to the controller.
* @param name - The name of the parameter
* @param defaultValue - The default value of the parameter
* @param isTrigger - Is the parameter a trigger, if true, the parameter will act as a trigger, trigger work mostly like bool parameter, but their values are reset to false after check a Transition.
* @param isTrigger - Determines if the parameter is a trigger. If true, the parameter work mostly like bool parameter, but their values are reset to false after animation update.
*/
addParameter(
name: string,
Expand Down
8 changes: 6 additions & 2 deletions tests/src/core/Animator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ describe("Animator test", function () {
anyTransition.hasExitTime = true;
anyTransition.exitTime = 0.7;
let anyToIdleTime =
// @ts-ignore
(anyTransition.exitTime - toIdleTransition.duration) * walkState._getDuration() +
// @ts-ignore
(anyTransition.duration * idleState._getDuration()) / idleSpeed;

Expand Down Expand Up @@ -520,6 +522,8 @@ describe("Animator test", function () {
anyTransition.hasExitTime = true;
anyTransition.exitTime = 0.3;
let anyToIdleTime =
// @ts-ignore
(1 - anyTransition.exitTime - toIdleTransition.duration) * walkState._getDuration() +
// @ts-ignore
(anyTransition.duration * idleState._getDuration()) / idleSpeed;

Expand Down Expand Up @@ -716,7 +720,7 @@ describe("Animator test", function () {
const layerData = animator._getAnimatorLayerData(0);
animatorController.addParameter("playRun", 0);
const stateMachine = animatorController.layers[0].stateMachine;
stateMachine.clearEntryTransitions();
stateMachine.clearEntryStateTransitions();
stateMachine.clearAnyStateTransitions();
const walkState = animator.findAnimatorState("Run");
// For test clipStartTime is not 0 and transition duration is 0
Expand Down Expand Up @@ -800,7 +804,7 @@ describe("Animator test", function () {
// @ts-ignore
const layerData = animator._getAnimatorLayerData(0);
const stateMachine = animatorController.layers[0].stateMachine;
stateMachine.clearEntryTransitions();
stateMachine.clearEntryStateTransitions();
stateMachine.clearAnyStateTransitions();
const walkState = animator.findAnimatorState("Walk");
walkState.clearTransitions();
Expand Down

0 comments on commit b2bdeed

Please sign in to comment.