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

/**
 * 滚动的石头
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class FStoenRoll extends cc.Component {

    @property({
        displayName: '数值(怪物id)'
    })
    public monsterId = 1001;

    @property({
        displayName: '起点到终点时间(秒)'
    })
    public time = 2;

    @property({
        displayName: '每次滚动停留时间(秒)'
    })
    public interval = 2;

    @property({
        type: cc.Node,
        displayName: '滚动目的地'
    })
    public target: cc.Node = null;

    @property({
        type: sp.Skeleton,
        displayName: '滚动动画'
    })
    public spine: sp.Skeleton = null;

    @property({
        displayName: "警告",
        type: cc.Node
    })
    warnNode: cc.Node = null;

    private initPos: cc.Vec2;
    private targetPos: cc.Vec2;

    private ff: FF;
    private isRoll = false;

    onLoad() {
        this.initPos = this.node.getPosition();
        this.targetPos = this.target.getPosition();

        cc.tween(this.node).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(() => {
                this.node.getComponent(cc.RigidBody).enabledContactListener = true;
                this.node.getComponent(cc.RigidBody).allowSleep = false;
                this.node.opacity = 255;
                this.node.setPosition(this.initPos);
                this.isRoll = true;
                this.warnAction();
            }),
            cc.moveBy(this.time, this.targetPos),
            cc.callFunc(() => {
                this.node.getComponent(cc.RigidBody).allowSleep = true;
                this.isRoll = false;
                if(this.warnNode){
                    this.warnNode.stopAllActions();
                    this.warnNode.opacity = 0;
                }
            }),
            cc.fadeOut(0.5),
            cc.callFunc(() => {
                this.node.getComponent(cc.RigidBody).enabledContactListener = false;
            }),
        ).repeatForever().start();
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        // if(!this.isRoll){
        //     return;
        // }
        if (other.node.group == 'A'&& other.tag == 1) {
            let obj = other.node.getComponent(FSprite);
            if (obj.hp > 0) {
                this.ff = obj.ff;
                let main = this.ff.main;
                let attrData = main.sManage.getMonsterData(this.monsterId);
                // this.sprite.atkjs(target);
                obj.bAtkjs(attrData);
            }

        }
    }

    warnAction() {
        if(this.warnNode){
            this.warnNode.opacity = 0;
            cc.tween(this.warnNode).sequence(
                cc.fadeTo(0.2, 120),
                cc.fadeTo(0.2, 0),
            ).repeatForever().start();
        }
       
    }


}
