Egret – ts(Egret) 与 js 的调用

ts 是 js 的超集,因此只要是 js 与 js 可以互相调用的,ts 均可以调用,只不过需要增加声明来解决编译时报错。
ts 最终生成的文件为 js,因此 js 调用 ts 其实就是 js 调用 js。
而在互相调用之前,需要进行声明!即生成d.ts文件。
下面我们重点来说下如何生成d.ts文件


(function(p2){

    // exports
    p2.KinematicCharacterController = KinematicCharacterController;

    // imports
    var vec2 = p2.vec2;
    var Ray = p2.Ray;
    var RaycastResult = p2.RaycastResult;
    var AABB = p2.AABB;
    var EventEmitter = p2.EventEmitter;

    // constants
    var ZERO = vec2.create();
    var UNIT_Y = vec2.fromValues(0,1);

    // math helpers
    function sign(x){
        return x >= 0 ? 1 : -1;
    }
    function lerp(factor, start, end){
        return start + (end - start) * factor;
    }
    function clamp(value, min, max){
        return Math.min(max, Math.max(min, value));
    }
    function angle(a, b){
        return Math.acos(vec2.dot(a, b));
    }
    function expandAABB(aabb, amount){
        var halfAmount = amount * 0.5;
        aabb.lowerBound[0] -= halfAmount;
        aabb.lowerBound[1] -= halfAmount;
        aabb.upperBound[0] += halfAmount;
        aabb.upperBound[1] += halfAmount;
    }
    ...
    /**
     * Should be executed after each physics tick, using the physics deltaTime.
     * @param {number} deltaTime
     */
    KinematicCharacterController.prototype.update = (function(){
        var scaledVelocity = vec2.create();
        return function (deltaTime) {
            var input = this.input;
            var velocity = this.velocity;
            var controller = this;

            var wallDirX = (controller.collisions.left) ? -1 : 1;
            var targetVelocityX = input[0] * this.moveSpeed;

            var smoothing = this.velocityXSmoothing;
            smoothing *= controller.collisions.below ? this.accelerationTimeGrounded : this.accelerationTimeAirborne;
            var factor = 1 - Math.pow(smoothing, deltaTime);
            velocity[0] = lerp(factor, velocity[0], targetVelocityX);
            if(Math.abs(velocity[0]) < this.velocityXMin){
                velocity[0] = 0;
            }

            var wallSliding = false;
            if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity[1] < 0) {
                wallSliding = true;

                if (velocity[1] < -this.wallSlideSpeedMax) {
                    velocity[1] = -this.wallSlideSpeedMax;
                }

                if (this.timeToWallUnstick > 0) {
                    velocity[0] = 0;

                    if (input[0] !== wallDirX && input[0] !== 0) {
                        this.timeToWallUnstick -= deltaTime;
                    } else {
                        this.timeToWallUnstick = this.wallStickTime;
                    }
                } else {
                    this.timeToWallUnstick = this.wallStickTime;
                }
            }

            if (this._requestJump) {
                this._requestJump = false;

                if (wallSliding) {
                    if (wallDirX === input[0]) {
                        velocity[0] = -wallDirX * this.wallJumpClimb[0];
                        velocity[1] = this.wallJumpClimb[1];
                    } else if (input[0] === 0) {
                        velocity[0] = -wallDirX * this.wallJumpOff[0];
                        velocity[1] = this.wallJumpOff[1];
                    } else {
                        velocity[0] = -wallDirX * this.wallLeap[0];
                        velocity[1] = this.wallLeap[1];
                    }
                }

                if (controller.collisions.below) {
                    velocity[1] = this.maxJumpVelocity;
                }
            }

            if (this._requestUnJump) {
                this._requestUnJump = false;
                if (velocity[1] > this.minJumpVelocity) {
                    velocity[1] = this.minJumpVelocity;
                }
            }

            velocity[1] += this.gravity * deltaTime;
            vec2.scale(scaledVelocity, velocity, deltaTime);
            controller.move(scaledVelocity, input);

            if (controller.collisions.above || controller.collisions.below) {
                velocity[1] = 0;
            }
        };
    })();

})(this.p2);

我们来写下p2.KinematicCharacterController的声明文件如下


declear module p2 {
    export function sign(x:number);
    export function lerp(factor:number, start:number, end:number);
    export function clamp(value:number, min:number, max:number);
    export function angle(a:number, b:number);
    export function expandAABB(aabb:{}, amount:number);
    //
    export class RaycastController {
        constructor(options: {});
        updateRaycastOrigins();
        calculateRaySpacing();
    }

    export class Controller {
        constructor(options: {});
        resetCollisions(velocity:number);
        moveWithZeroInput(velocity:number, standingOnPlatform:boolean);
        move(velocity:number, input:number, standingOnPlatform:boolean);
        emitRayCastEvent():{};
        horizontalCollisions(velocity:number);
        verticalCollisions(velocity:number, input:any);
        climbSlope(velocity:number, slopeAngle:number);
        descendSlope(velocity:number);
        resetFallingThroughPlatform();
    }

    export class KinematicCharacterController {
        constructor(options: {});
        setJumpKeyState(isDown:boolean);
        update():any;
    }
}

点击下载
欢迎提问

欢迎留言

avatar
  Subscribe  
Notify of