Skip to content

Commit

Permalink
fix: animatorController shouldnt set parameter value
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhuang committed Jul 26, 2024
1 parent 7ea24f2 commit 009adba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
49 changes: 39 additions & 10 deletions packages/core/src/animation/Animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assignmentClone, ignoreClone } from "../clone/CloneManager";
import { ClearableObjectPool } from "../utils/ClearableObjectPool";
import { AnimatorController } from "./AnimatorController";
import { AnimatorControllerLayer } from "./AnimatorControllerLayer";
import { AnimatorControllerParameter } from "./AnimatorControllerParameter";
import { AnimatorControllerParameter, AnimatorControllerParameterValueType } from "./AnimatorControllerParameter";
import { AnimatorState } from "./AnimatorState";
import { AnimatorStateTransition } from "./AnimatorStateTransition";
import { KeyframeValueType } from "./Keyframe";
Expand Down Expand Up @@ -53,6 +53,8 @@ export class Animator extends Component {
private _curveOwnerPool: Record<number, Record<string, AnimationCurveOwner<KeyframeValueType>>> = Object.create(null);
@ignoreClone
private _animationEventHandlerPool = new ClearableObjectPool(AnimationEventHandler);
@ignoreClone
private _parametersValeMap: Record<string, AnimatorControllerParameterValueType> = Object.create(null);

@ignoreClone
private _tempAnimatorStateInfo: IAnimatorStateInfo = { layerIndex: -1, state: null };
Expand Down Expand Up @@ -222,6 +224,31 @@ export class Animator extends Component {
return this._animatorController?._parametersMap[name] ?? null;
}

/**
* Get the value of the given parameter.
* @param name - The name of the parameter
* @param value - The value of the parameter
*/
getParameterValue(name: string): AnimatorControllerParameterValueType {
const parameter = this._animatorController?._parametersMap[name];
if (parameter) {
return this._parametersValeMap[name] ?? parameter.value;
}
return undefined;
}

/**
* Set the value of the given parameter.
* @param name - The name of the parameter
* @param value - The value of the parameter
*/
setParameterValue(name: string, value: AnimatorControllerParameterValueType) {
const parameter = this._animatorController?._parametersMap[name];
if (parameter && parameter.value !== value) {
this._parametersValeMap[name] = value;
}
}

/**
* @internal
*/
Expand Down Expand Up @@ -260,7 +287,8 @@ export class Animator extends Component {
}

this._animatorLayersData.length = 0;
this._curveOwnerPool = {};
this._curveOwnerPool = Object.create(null);
this._parametersValeMap = Object.create(null);
this._animationEventHandlerPool.clear();

if (this._controllerUpdateFlag) {
Expand Down Expand Up @@ -1292,39 +1320,40 @@ export class Animator extends Component {
for (let i = 0, n = conditions.length; i < n; ++i) {
let pass = false;
const { mode, parameterName: name, threshold } = conditions[i];
const parameter = this.getParameter(name);
if (!parameter) {
const parameterValue = this.getParameterValue(name);

if (parameterValue === undefined) {
return false;
}

switch (mode) {
case AnimatorConditionMode.Equals:
if (parameter.value === threshold) {
if (parameterValue === threshold) {
pass = true;
}
break;
case AnimatorConditionMode.Greater:
if (parameter.value > threshold) {
if (parameterValue > threshold) {
pass = true;
}
break;
case AnimatorConditionMode.Less:
if (parameter.value < threshold) {
if (parameterValue < threshold) {
pass = true;
}
break;
case AnimatorConditionMode.NotEquals:
if (parameter.value !== threshold) {
if (parameterValue !== threshold) {
pass = true;
}
break;
case AnimatorConditionMode.If:
if (parameter.value === true) {
if (parameterValue === true) {
pass = true;
}
break;
case AnimatorConditionMode.IfNot:
if (parameter.value === false) {
if (parameterValue === false) {
pass = true;
}
break;
Expand Down
16 changes: 2 additions & 14 deletions packages/core/src/animation/AnimatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export class AnimatorController extends ReferResource {
/**
* Add a parameter to the controller.
* @param name - The name of the parameter
* @param value - The value of the parameter
* @param initialValue - The initial value of the parameter
*/
addParameter(name: string, value?: AnimatorControllerParameterValueType): AnimatorControllerParameter;
addParameter(name: string, initialValue?: AnimatorControllerParameterValueType): AnimatorControllerParameter;

/**
* Add a parameter to the controller.
Expand Down Expand Up @@ -83,18 +83,6 @@ export class AnimatorController extends ReferResource {
return this._parametersMap[name] || null;
}

/**
* Set the value of the given parameter.
* @param name - The name of the parameter
* @param value - The value of the parameter
*/
setParameterValue(name: string, value: AnimatorControllerParameterValueType) {
const parameter = this._parametersMap[name];
if (parameter && parameter.value !== value) {
parameter.value = value;
}
}

/**
* Get the layer by name.
* @param name - The layer's name.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/animation/AnimatorControllerParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export type AnimatorControllerParameterValueType = number | string | boolean;
export class AnimatorControllerParameter {
/** The name of the parameter. */
name: string;
/** The value of the parameter. */
/** The initial value of the parameter. */
value: AnimatorControllerParameterValueType;
}
8 changes: 4 additions & 4 deletions tests/src/core/Animator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ describe("Animator test", function () {
animator.update(0.001);
expect(animator.getCurrentAnimatorState(0).name).to.eq("Run");

animator.animatorController.setParameterValue("playerSpeed", 0.4);
animator.setParameterValue("playerSpeed", 0.4);
// @ts-ignore
animator.engine.time._frameCount++;
animator.update(runToWalkTime - 0.001);
Expand All @@ -425,7 +425,7 @@ describe("Animator test", function () {
animator.update(0.001);
expect(animator.getCurrentAnimatorState(0).name).to.eq("Walk");

animator.animatorController.setParameterValue("playerSpeed", 0);
animator.setParameterValue("playerSpeed", 0);
// @ts-ignore
animator.engine.time._frameCount++;
animator.update(anyToIdleTime - 0.001);
Expand Down Expand Up @@ -537,7 +537,7 @@ describe("Animator test", function () {
animator.update(0.001);
expect(animator.getCurrentAnimatorState(0).name).to.eq("Run");

animator.animatorController.setParameterValue("playerSpeed", 0.4);
animator.setParameterValue("playerSpeed", 0.4);
// @ts-ignore
animator.engine.time._frameCount++;
animator.update(runToWalkTime - 0.001);
Expand All @@ -547,7 +547,7 @@ describe("Animator test", function () {
animator.update(0.001);
expect(animator.getCurrentAnimatorState(0).name).to.eq("Walk");

animator.animatorController.setParameterValue("playerSpeed", 0);
animator.setParameterValue("playerSpeed", 0);
// @ts-ignore
animator.engine.time._frameCount++;
animator.update(anyToIdleTime - 0.001);
Expand Down

0 comments on commit 009adba

Please sign in to comment.