SurroundMotionStreakAction.ts 3.19 KB
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;
enum MoveDirection {
    Clockwise,
    Anticlockwise
}
@ccclass
export default class SurroundMotionStreakAction extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    @property
    moveSpeed: number = 1;
    @property
    fadeTime: number = 1;
    @property
    minSeg: number = 1;
    @property
    stroke: number = 10;
    @property
    direction: MoveDirection = MoveDirection.Clockwise;
    @property({ type: cc.Node })
    actNode: cc.Node = new cc.Node;
    @property({ type: cc.Texture2D })
    motionStreakTexture: cc.Texture2D = new cc.Texture2D;
    private readonly _baseTime: number = 1;


    onLoad() { }

    start() {

    }

    onEnable() {
        let lt = cc.v2(this.node.width * (-1) / 2, this.node.height / 2);
        let rt = cc.v2(this.node.width / 2, this.node.height / 2);
        let rb = cc.v2(this.node.width / 2, this.node.height * (-1) / 2);
        let lb = cc.v2(this.node.width * (-1) / 2, this.node.height * (-1) / 2);
        let actTime: number = this._baseTime / this.moveSpeed;
        let max = Math.max(this.node.width, this.node.height);
        let wTime = this.node.width / max * actTime;
        let hTime = this.node.height / max * actTime;
        let ltAct = cc.moveTo(hTime, lt);
        let rtAct = cc.moveTo(wTime, rt);
        let rbAct = cc.moveTo(hTime, rb);
        let lbAct = cc.moveTo(wTime, lb);
        if (this.direction === MoveDirection.Anticlockwise) {
            ltAct = cc.moveTo(wTime, lt);
            rtAct = cc.moveTo(hTime, rt);
            rbAct = cc.moveTo(wTime, rb);
            lbAct = cc.moveTo(hTime, lb);
        }
        let seqClockwise = cc.sequence(ltAct, rtAct, rbAct, lbAct);
        let seqAnticlockwise = cc.sequence(rtAct, ltAct, lbAct, rbAct);
        let motionStreak = this.actNode.getComponent(cc.MotionStreak);
        if (!motionStreak) {
            motionStreak = this.actNode.addComponent(cc.MotionStreak);
            motionStreak.texture = this.motionStreakTexture;
            motionStreak.fadeTime = this.fadeTime;
            motionStreak.minSeg = this.minSeg;
            motionStreak.stroke = this.stroke;
        }
        motionStreak.enabled = true;
        this.actNode.stopAllActions();
        if (this.direction === MoveDirection.Clockwise) {
            this.actNode.x = this.node.width * (-1) / 2;
            this.actNode.y = this.node.height / 2;
            this.actNode.runAction(cc.repeatForever(seqClockwise));
        } else if (this.direction === MoveDirection.Anticlockwise) {
            this.actNode.x = this.node.width / 2;
            this.actNode.y = this.node.height / 2;
            this.actNode.runAction(cc.repeatForever(seqAnticlockwise));
        }
    }

    onDisable() {
        let motionStreak = this.actNode.getComponent(cc.MotionStreak);
        if (motionStreak) {
            motionStreak.enabled = false;
        }
        this.node.stopAllActions();
    }
}