import { _decorator, Collider, Component, ITriggerEvent, Node } from 'cc';
import { constant } from '../framework/constant';
const { ccclass, property } = _decorator;


const OUTOFRANGE = 50

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

    @property 
    public bulletSpeed = 0;


    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.ENEMY_PLANE ) {
            this.node.destroy()
        }
    }

    update(deltaTime: number) {
        const pos = this.node.position;
        const moveLength = pos.x + this.bulletSpeed;
        this.node.setPosition(moveLength, pos.y, pos.z)
        if(moveLength > OUTOFRANGE){
            this.node.destroy()
            
        }
        
    }
}


