import FF from "../../FF";
import FSprite from "../../object/FSprite";
import MSprite from "../../object/MSprite";
import BaseEvent from "../base/BaseEvent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FTransportDoor extends BaseEvent {

    @property({
        displayName: "传送位置",
    })
    posList: cc.Vec2 = cc.v2(0, 0);

    @property({
        displayName: '默认图片',
        type: cc.SpriteFrame
    })
    defaultIcon: cc.SpriteFrame = null;

    @property({
        displayName: '踩的图片',
        type: cc.SpriteFrame
    })
    mIcon: cc.SpriteFrame = null;

    ff: FF;

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group == 'A' && other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            this.node.getComponent(cc.Sprite).spriteFrame = this.mIcon;
            this.setPos();
        }
    }

    setPos() {
        this.pause();
        let mSprite = this.ff.mainSprite;
        mSprite.node.stopActionByTag(1);
        let action = cc.tween(mSprite).sequence(
            cc.delayTime(0.2),
            cc.callFunc(() => {
                let roles = this.ff.getGroupBy("A");
                roles && roles.forEach((sprite, index) => {
                    sprite.setPosition(this.posList);
                })
                this.node.getComponent(cc.Sprite).spriteFrame = this.defaultIcon;
                this.resume();
            })
        ).start();
        action.tag(1);
    }

    /**
     * 暂停当前游戏
     */
    public pause() {
        this.ff.pauseSprite(true)
    }
    /**
     * 恢复游戏
     */
    public resume() {
        this.ff.pauseSprite(false)
    }

}
