import FqLogin from "../../../login/FqLogin";
import { AudioMgr } from "../../../main/ViewManage";
import FF from "../FF";
import FMap from "../map/FMap";
import FSprite from "../object/FSprite";

/**
 * 打开机关
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FOpenTrigger extends cc.Component {

    /**
     * 控制的栅栏
     */
    @property([cc.Node])
    mFenceTrigger: Array<cc.Node> = [];

    @property([cc.Node])
    guides: Array<cc.Node> = [];//引导标识

    @property(cc.SpriteFrame)
    closeFrame: cc.SpriteFrame = null;

    private fMap: FMap;
    private isOver = false;
    onLoad() {
        this.fMap = this.node.parent.parent.getComponent(FMap);
        // this.mFenceTrigger.forEach(element => {
        //     this.showFence(element,'open');
        // });
    }

    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (this.isOver) {
            return;
        }
        if (other.node.group == 'bullet') {//子弹不能触发
            return;
        }
        this.isOver = true;
        this.node.removeComponent(cc.PhysicsBoxCollider);
        let ff = this.fMap.ff;
        ff.pauseSprite(true);

        let camera = this.fMap.mCamera;

        let cameraPos = this.mFenceTrigger[0];
        let pos = cc.v2();
        let winsize = cc.winSize;
        pos.x = cameraPos.x - winsize.width / 2;
        pos.y = cameraPos.y - winsize.height / 2;

        for (let i = 0; i < this.guides.length; i++) {
            const element = this.guides[i];
            element.destroy();
        }
        this.guides = [];

        let sprite = this.node.getComponent(cc.Sprite);
        sprite.spriteFrame = this.closeFrame;
        cc.tween(camera.node).sequence(
            cc.moveTo(1.5, pos),
            cc.callFunc(() => {
                for (let i = 0; i < this.mFenceTrigger.length; i++) {
                    const element = this.mFenceTrigger[i];
                    this.showFence(element, 'close');

                }
                ff.main.playerEffectByPath(AudioMgr.openDoor);
            }),
            cc.delayTime(1),
            cc.callFunc(() => {
                ff.pauseSprite(false);
                for (let i = 0; i < this.mFenceTrigger.length; i++) {
                    const element = this.mFenceTrigger[i];
                    element.active = false;

                }
                FqLogin.commitEvent(this.node.name, '', '');
            })
        ).start();
    }

    private showFence(element, action) {
        let nodes = element.children;
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            let spine = element.getComponent(sp.Skeleton);
            if (spine) {
                spine.setAnimation(0, action, false);
            }
        }
    }



}
