import { _decorator, Collider, Component, ITriggerEvent, Node, sp } from 'cc';
import { constant } from '../framework/constant';
import { gameMangager } from '../framework/gameMangager';

const { ccclass, property } = _decorator;


const OUTOFBOUNCE = 50

@ccclass('enemyPlane')
export class enemyPlane extends Component {

    // @property
    // public enemySpeed = 0;
    private _enemySpeed = 0;

    public enemyType = constant.EnemyPlaneType.TYPE1

    private _gameManager: gameMangager = null;


    start() {

    }

    update(deltaTime: number) {
        const pos = this.node.position;
        const movePos = pos.x - this._enemySpeed;
        this.node.setPosition(movePos, pos.y, pos.z)
        if (movePos > OUTOFBOUNCE) {
            this.node.destroy()
        }



    }

    show(gameManager: any, speed: number) {
        this._gameManager = gameManager
        this._enemySpeed = speed
    }
    // 碰撞业务
    onEnable() {
        const collider = this.getComponent(Collider)
        collider.on('onTriggerEnter', this._onTriggerEnter, this)
    }
    onDisable() {
        const collider = this.getComponent(Collider)
        collider.off('onTriggerEnter', this._onTriggerEnter, this)
    }

    private _onTriggerEnter(event: ITriggerEvent) {
        const collisionGroup = event.otherCollider.getGroup();
        console.log('敌人');
        if (collisionGroup === constant.CollisionType.SELF_BULLET || collisionGroup === constant.CollisionType.SELF_PLANE) {
            this.node.destroy()
            this._gameManager.addScore()

        }
    }
}


