LaserView.ts 4.43 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

import { BattleEvent, LASER_DIR } from "../kernel/battle/BattleConst";
import { BattleManager } from "../kernel/battle/BattleManager";
import { LaserObj } from "../kernel/battle/map/LaserObj";
import { MapView } from "../kernel/battle/map/MapView";
import { ObjData } from "../kernel/battle/map/ObjData";
import ObjBaseView from "./ObjBaseView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LaserView extends ObjBaseView {

    private _effect: cc.Node;
    private _laser: cc.Node;
    private _vLaser: cc.Node;

    //#region interface

    onHit(): void {

        let ani = this._effect.getComponent(cc.Animation);
        !ani.getAnimationState('ani_yxz_tsfk_jg02').isPlaying && ani.play('ani_yxz_tsfk_jg02', 0);

        if (this._laser) {
            !this._laser.active && (this._laser.active = true);
            let laserAni = this._laser.getComponent(cc.Animation);
            !laserAni.getAnimationState('fx_laser_02').isPlaying && laserAni.play('fx_laser_02', 0);
        }

        if (this._vLaser) {
            !this._vLaser.active && (this._vLaser.active = true);
            let laserAni = this._vLaser.getComponent(cc.Animation);
            !laserAni.getAnimationState('fx_laser_02').isPlaying && laserAni.play('fx_laser_02', 0);
        }
    }

    //#endregion

    //#region  LIFE-CYCLE CALLBACKS:

    init(owner: ObjData, mapView: MapView): void {

        let laserObj = owner as LaserObj;

        if (laserObj.dir == LASER_DIR.HORIZEN || laserObj.dir == LASER_DIR.COMBINE) {
            this._laser = cc.instantiate(mapView.prefabCollection.getChildByName('skillSpine').getChildByName('laser'));
            this._laser.scaleX = mapView.width / this._laser.width;
            this._laser.active = false;

            let laserAni = this._laser.getComponent(cc.Animation);
            laserAni.on('finished', () => { this._laser.active = false; }, this);
        }

        if (laserObj.dir == LASER_DIR.VERTICAL || laserObj.dir == LASER_DIR.COMBINE) {
            this._vLaser = cc.instantiate(mapView.prefabCollection.getChildByName('skillSpine').getChildByName('laser'));
            this._vLaser.scaleX = mapView.height / this._vLaser.width;
            this._vLaser.angle = 90;
            this._vLaser.active = false;

            let vlaserAni = this._vLaser.getComponent(cc.Animation);
            vlaserAni.on('finished', () => { this._vLaser.active = false; }, this);
        }

        this._effect = this.node.getChildByName('UI_yxz_tsfk_jg2');
        this._effect.removeFromParent();

        super.init(owner, mapView);
        this._img.active = false;

        mapView.effectLayer.addChild(this._effect);
        this._laser && mapView.effectLayer.addChild(this._laser);
        this._vLaser && mapView.effectLayer.addChild(this._vLaser);

        BattleManager.ins.eventNode.on(BattleEvent.BATTLE_START, this.onBattleStart, this);
    }

    protected onPositionChange(x: number, y: number): void {
        super.onPositionChange(x, y);
        this._effect.setPosition(x, y);
        this._laser && this._laser.setPosition(0, y);
        this._vLaser && this._vLaser.setPosition(x, 0);
    }

    protected onVisibleChange(): void {
        this._img.active = false; //图片一直隐藏
    }

    protected onDestroy(): void {
        BattleManager.ins.eventNode.off(BattleEvent.BATTLE_START, this.onBattleStart, this);
        this._effect.destroy();
        this._laser && this._laser.destroy();
        this._vLaser && this._vLaser.destroy();
    }

    protected onImageLoaded(): void {

        //更新特效的图片
        this._effect.getChildByName('UI_yxz_tsfk_jg2').getComponent(cc.Sprite).spriteFrame = this._img.getComponent(cc.Sprite).spriteFrame;

        cc.resources.load('battle/' + this.owner.imgName + 'a', cc.SpriteFrame, (error, asset) => {

            if (!this._destroyed) {
                this._effect.getChildByName('UI_yxz_tsfk_jg2a').getComponent(cc.Sprite).spriteFrame = asset as cc.SpriteFrame;
            }

        })
    }

    // update (dt) {}

    //#endregion

    private onBattleStart() {

        let ani = this._effect.getComponent(cc.Animation);
        ani.play("ani_yxz_tsfk_jg01", 0);
    }
}