BlockView.ts 5.71 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 { BlockSkill } from "../kernel/battle/BattleConst";
import { BlockData } from "../kernel/battle/map/BlockData";
import { MapView } from "../kernel/battle/map/MapView";
import { BlockSkillFireworkView } from "./BlcokSkillFireworkView";
import { BlockSkillBombView } from "./BlockSkillBombView";
import { BlockSkillSwitcherView } from "./BlockSkillSwitcherView";
import { BlcokSkillViewBase } from "./BlockSkillViewBase";
import ObjBaseView from "./ObjBaseView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BlockView extends ObjBaseView {

    protected _imgSkillIcon: cc.Node = null;
    protected _imgEffect: cc.Node = null;
    protected _txt: cc.Node = null;
    protected _light: cc.Node = null;

    get owner(): BlockData { return this._owner as BlockData; }
    private _mapView: MapView = null;

    protected _txtOffset: cc.Vec2 = new cc.Vec2();

    private _skillView: BlcokSkillViewBase = null;

    //#region interface

    onHit(): void {
        super.onHit();

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

        if (this._light) {
            let lightAni = this._light.getComponent(cc.Animation);
            lightAni && !lightAni.getAnimationState('fx_block_light_hit').isPlaying && lightAni.play('fx_block_light_hit', 0) && (this._light.active = true)
        }

        this._skillView && this._skillView.onCollision();
    }

    onDead() {

        this._mapView.playBlockParticle(this.node);

        this._skillView && this._skillView.onDead();

        this._imgEffect.destroy();
        this._imgSkillIcon.destroy();
        this._txt.destroy();
        this._light && this._light.destroy();
        super.onDead();
    }

    //#endregion

    //#region life cycle

    init(owner: BlockData, mapView: MapView) {

        this._imgSkillIcon = this.node.getChildByName('imgIcon');
        this._imgEffect = this.node.getChildByName('imgEffect');
        this._imgEffect.active = false;
        this._txt = this.node.getChildByName('txt');
        this._txtOffset.x = this._txt.x;
        this._txtOffset.y = this._txt.y;
        this._mapView = mapView;
        this._light = this.node.getChildByName('light');
        this._light && (this._light.active = false);

        super.init(owner, mapView);

        //分层显示
        this._imgEffect.removeFromParent();
        this._txt.removeFromParent();
        this._imgSkillIcon.removeFromParent();
        this._light && this._light.removeFromParent();

        mapView.effectLayer.addChild(this._imgEffect);
        mapView.txtLayer.addChild(this._txt);
        mapView.blockIconLayer.addChild(this._imgSkillIcon);
        this._light && mapView.lightLayer.addChild(this._light)

        if (owner.skill && owner.skill.skillId == BlockSkill.BOMB) {
            this._skillView = new BlockSkillBombView(this, this._mapView);
        }
        else if (owner.skill && owner.skill.skillId >= BlockSkill.FIREWORK_VERTICAL && owner.skill.skillId <= BlockSkill.FIREWORK_DOUBLE) {
            this._skillView = new BlockSkillFireworkView(this, this._mapView);
        }
        else if (owner.skill && owner.skill.skillId == BlockSkill.SWITCHER) {
            this._skillView = new BlockSkillSwitcherView(this, mapView);
        }


        this.updateSkillIcon();

        // this.onVisibleChange()
    }

    onPreview(): void {
        super.onPreview();
        this._txt.active = false;
    }



    protected onDestroy(): void {
        super.onDestroy();
        this._skillView && this._skillView.onDestroy();
        this._skillView = null;
        this._imgEffect = null;
        this._imgSkillIcon = null;
        this._txt = null;
    }

    //#endregion

    //#region override method

    protected onPositionChange(x: number, y: number): void {
        super.onPositionChange(x, y);

        this._imgEffect.setPosition(x, y);
        this._txt.setPosition(x + this._txtOffset.x, y + this._txtOffset.y);
        this._imgSkillIcon.setPosition(x, y);
        this._light && this._light.setPosition(x, y);

        this._skillView && this._skillView.onPositionChange(x, y);
    }

    protected onLazyUpdate(): void {
        this._txt.getComponent(cc.Label).string = this.owner.hp.toString();
    }

    protected onVisibleChange(): void {
        super.onVisibleChange();

        this._imgEffect.active = this._visible;
        this._imgEffect.active = false; //暂时屏蔽
        this._imgSkillIcon.active = (this._visible && this.owner.skill && this.owner.skill.skillImg != '');
        this._txt.active = this._visible && this.owner.hp > 0;
    }

    //#endregion

    //#region private method

    private updateSkillIcon() {
        if (this.owner.skill && this.owner.skill.skillImg != "") {

            // this._imgSkillIcon.active = true;

            cc.resources.load("battle/" + this.owner.skill.skillImg, cc.SpriteFrame, (err, spf) => {
                // const frame = (spf as cc.SpriteAtlas).getSpriteFrame(this._owner.iconName);
                if (!err && this.isValid) { //有可能销毁后进入回调,增加判断 isValid
                    this._imgSkillIcon.getComponent(cc.Sprite).spriteFrame = spf as cc.SpriteFrame;
                }
                else {
                    // console.error(err.message);
                }

            });

        }
        else {
            this._imgSkillIcon.active = false;
        }
    }

    //#endregion
}