fix cubic spline classes error (#4679)

This commit is contained in:
Leslie Leigh
2019-06-24 15:10:45 +08:00
committed by Weiliang Wang
parent 06e0b457ae
commit cfc9af914f
+53 -30
View File
@@ -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<T> 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>, t: number, dt: number) {
return this.dataPoint;
}
interface ICubicSplineValue<T> extends ILerpable {
dataPoint: T;
inTangent: T;
outTangent: T;
lerp (to: ICubicSplineValue<T>, t: number, dt: number): T;
getNoLerp (): T;
}
type CubicSplineValueConstructor<T> = new (dataPoint: T, inTangent: T, outTangent: T) => ICubicSplineValue<T>;
type ScaleFx<T> = (out: T, v: T, s: number) => T;
type ScaleAndAddFx<T> = (out: T, v1: T, v2: T, s: number) => T;
function makeCubicSplineValueConstructor<T> (
name: string,
constructorX: new () => T,
scaleFx: ScaleFx<T>,
scaleAndAdd: ScaleAndAddFx<T>): typeof CubicSplineValueBase {
scaleAndAdd: ScaleAndAddFx<T>): CubicSplineValueConstructor<T> {
let tempValue = new constructorX();
let m0 = new constructorX();
let m1 = new constructorX();
@ccclass(name)
class CubicSplineValueClass extends CubicSplineValueBase<T> {
class CubicSplineValueClass implements ICubicSplineValue<T> {
@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<T> (
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<number> {
export class CubicSplineNumberValue implements ICubicSplineValue<number> {
@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<number> {
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;