diff --git a/cocos/animation/cubic-spline-value.ts b/cocos/animation/cubic-spline-value.ts index 9a2007b35c..2f06f789f1 100644 --- a/cocos/animation/cubic-spline-value.ts +++ b/cocos/animation/cubic-spline-value.ts @@ -4,45 +4,45 @@ import { Quat, Vec2, Vec3, Vec4 } from '../core/value-types'; import * as vmath from '../core/vmath'; import { ILerpable } from './types'; -class CubicSplineValueBase implements ILerpable { - @property - public dataPoint: T; - - @property - public inTangent: T; - - @property - public outTangent: T; - - constructor (dataPoint: T, inTangent: T, outTangent: T) { - this.dataPoint = dataPoint; - this.inTangent = inTangent; - this.outTangent = outTangent; - } - - public getNoLerp () { - return this.dataPoint; - } - - public lerp (to: CubicSplineValueBase, t: number, dt: number) { - return this.dataPoint; - } +interface ICubicSplineValue extends ILerpable { + dataPoint: T; + inTangent: T; + outTangent: T; + lerp (to: ICubicSplineValue, t: number, dt: number): T; + getNoLerp (): T; } +type CubicSplineValueConstructor = new (dataPoint: T, inTangent: T, outTangent: T) => ICubicSplineValue; + type ScaleFx = (out: T, v: T, s: number) => T; type ScaleAndAddFx = (out: T, v1: T, v2: T, s: number) => T; function makeCubicSplineValueConstructor ( name: string, constructorX: new () => T, scaleFx: ScaleFx, - scaleAndAdd: ScaleAndAddFx): typeof CubicSplineValueBase { + scaleAndAdd: ScaleAndAddFx): CubicSplineValueConstructor { let tempValue = new constructorX(); let m0 = new constructorX(); let m1 = new constructorX(); @ccclass(name) - class CubicSplineValueClass extends CubicSplineValueBase { + class CubicSplineValueClass implements ICubicSplineValue { + @property + public dataPoint: T = new constructorX(); + + @property + public inTangent: T = new constructorX(); + + @property + public outTangent: T = new constructorX(); + + constructor (dataPoint: T, inTangent: T, outTangent: T) { + this.dataPoint = dataPoint; + this.inTangent = inTangent; + this.outTangent = outTangent; + } + public lerp (to: CubicSplineValueClass, t: number, dt: number) { const p0 = this.dataPoint; const p1 = to.dataPoint; @@ -61,29 +61,48 @@ function makeCubicSplineValueConstructor ( tempValue = scaleAndAdd(tempValue, tempValue, m1, f_3); return tempValue; } + + public getNoLerp () { + return this.dataPoint; + } } return CubicSplineValueClass; } -export const CubicSplineVec2Value: typeof CubicSplineValueBase = makeCubicSplineValueConstructor( +export const CubicSplineVec2Value = makeCubicSplineValueConstructor( 'cc.CubicSplineVec2Value', Vec2, vmath.vec2.scale, vmath.vec2.scaleAndAdd); cc.CubicSplineVec2Value = CubicSplineVec2Value; -export const CubicSplineVec3Value: typeof CubicSplineValueBase = makeCubicSplineValueConstructor( +export const CubicSplineVec3Value = makeCubicSplineValueConstructor( 'cc.CubicSplineVec3Value', Vec3, vmath.vec3.scale, vmath.vec3.scaleAndAdd); cc.CubicSplineVec3Value = CubicSplineVec3Value; -export const CubicSplineVec4Value: typeof CubicSplineValueBase = makeCubicSplineValueConstructor( +export const CubicSplineVec4Value = makeCubicSplineValueConstructor( 'cc.CubicSplineVec4Value', Vec4, vmath.vec4.scale, vmath.vec4.scaleAndAdd); cc.CubicSplineVec4Value = CubicSplineVec4Value; -export const CubicSplineQuatValue: typeof CubicSplineValueBase = makeCubicSplineValueConstructor( +export const CubicSplineQuatValue = makeCubicSplineValueConstructor( 'cc.CubicSplineQuatValue', Quat, vmath.quat.scale, vmath.quat.scaleAndAdd); cc.CubicSplineQuatValue = CubicSplineQuatValue; @ccclass('cc.CubicSplineNumberValue') -export class CubicSplineNumberValue extends CubicSplineValueBase { +export class CubicSplineNumberValue implements ICubicSplineValue { + @property + public dataPoint: number = 0; + + @property + public inTangent: number = 0; + + @property + public outTangent: number = 0; + + constructor (dataPoint: number, inTangent: number, outTangent: number) { + this.dataPoint = dataPoint; + this.inTangent = inTangent; + this.outTangent = outTangent; + } + public lerp (to: CubicSplineNumberValue, t: number, dt: number) { const p0 = this.dataPoint; const p1 = to.dataPoint; @@ -98,5 +117,9 @@ export class CubicSplineNumberValue extends CubicSplineValueBase { const f_3 = t_3 - t_2; return p0 * f_0 + m0 * f_1 + p1 * f_2 + m1 * f_3; } + + public getNoLerp () { + return this.dataPoint; + } } cc.CubicSplineNumberValue = CubicSplineNumberValue;