import FF from "../../FF";
import FSprite from "../../object/FSprite";
import WOneByone from "./WOneByone";

/**
 * 结束事件提示
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FEndSign extends cc.Component {

    @property(cc.Prefab)
    mMapDialog: cc.Prefab = null;

    @property(cc.Node)
    mD1: cc.Node = null;

    private ff:FF = null;
    private isOver = false;

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(this.isOver){
            return
        }
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.isOver = true;
                this.node.removeComponent(cc.PhysicsBoxCollider);
                this.run();
            }
        }
    }

    private run(){
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;

        let map = this.ff.mMap;
        let winSize = cc.winSize
        let camera = map.mCamera;
        let cx = this.node.x - winSize.width/2;
        let cy = this.node.y - winSize.height/2;

        cc.tween(camera.node).sequence(
            cc.moveTo(0.7,cc.v2(cx,cy)),
            cc.callFunc(()=>{
                this.run1();
            })
        ).start()
    }
    private run1(){
        this.mD1.active = true;

        let spine = this.mD1.getChildByName('chuansongmen1');
        spine.active = true;
        let monster = this.mD1.getChildByName('monster56');
        monster.active = true;
        monster.opacity = 0;

        cc.tween(monster).sequence(
            cc.fadeIn(1),
            cc.callFunc(()=>{
                this.d1();
            })
        ).start();

    }

    private d1(){
        let dialogs = [
            '看来你已经学会了基本的冒险方法，去吧，到了那边会有东西接应你的。',
            '加油，我的孩子，拯救众神的任务就交给你了',
        ];

        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.mD1.x;
        node.y = this.mD1.y + this.mD1.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            this.mD1.active = false;
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();

    }

}
