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

/**
 * 地刺
 */
const { ccclass, property } = cc._decorator;

interface ThornSprite {
    isAtk: boolean,
    sprite: FSprite
}


@ccclass
export default class Thorn extends BaseEvent {

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

    @property({
        displayName: '地刺出现间隔时间'
    })
    public interval = 2;

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

    private isAtk = false;
    private isPause = false;

    //当前处于碰撞区域的对象
    private spriteList: Array<ThornSprite> = [];

    start() {
        this.run();
    }
    update(dt) {
        if (!this.ff.lockCamera) {
            if (!this.ff.lockCamera != this.isPause) {
                this.isPause = true;
                this.spine.paused = true;
                return
            }
        } else {
            if (!this.ff.lockCamera != this.isPause) {
                this.isPause = false;
                this.spine.paused = false;
            }
        }
        if (this.isAtk) {
            this.spriteList.forEach(element => {
                this.rmHP(element);
            });
        }
    }
    private rmHP(thornSprite: ThornSprite) {
        if (thornSprite.isAtk) {
            return;
        }
        if (thornSprite.sprite.hp <= 0) {
            return;
        }
        thornSprite.isAtk = true;
        let main = this.ff.main;
        let attrData = main.sManage.getMonsterData(this.monsterId);
        thornSprite.sprite.bAtkjs(attrData);
    }

    private run() {
        this.spriteList.forEach(element => {
            element.isAtk = false;
        });
        cc.tween(this).sequence(
            cc.delayTime(this.interval),
            cc.callFunc(() => {
                this.playAtk();
            })
        ).start()
    }

    private playAtk() {
        this.isAtk = true;
        this.spine.setAnimation(0, 'atk', false);
        this.spine.setCompleteListener(() => {
            this.isAtk = false;
            this.spine.setCompleteListener(null);
            this.spine.setAnimation(0, 'idle', true);
            this.run()
        });
    }
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group == 'A') {
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            this.pushSprite(obj);
        }
    }
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, other: cc.PhysicsCollider) {
        if (other.node.group == 'A') {
            let obj = other.node.getComponent(FSprite);
            this.removeSprite(obj);
        }
    }

    private pushSprite(sprite: FSprite) {
        for (let i = 0; i < this.spriteList.length; i++) {
            const element = this.spriteList[i];
            if (element.sprite == sprite) {
                return
            }
        }
        this.spriteList.push({
            isAtk: false,
            sprite: sprite
        })
    }
    private removeSprite(sprite: FSprite) {
        for (let i = 0; i < this.spriteList.length; i++) {
            const element = this.spriteList[i];
            if (element.sprite == sprite) {
                this.spriteList.splice(i, 1);
                break
            }
        }
    }

}
