import { _decorator, Component, instantiate, Node, Prefab, UITransform, Vec2, Vec3 } from 'cc';
import { weiqi_item } from './weiqi_item';
import { car } from '../car';
const { ccclass, property } = _decorator;

@ccclass('weiqi_manager')
export class weiqi_manager extends Component {
    @property(Prefab) weiqi_pf:Prefab = null
    private mCar:car = null
    private COUNT:number = 10
    public init(car:car){
        this.mCar = car
        this.node.removeAllChildren()
        for (let index = 0; index < this.COUNT; index++) {
            let node = instantiate(this.weiqi_pf)
            node.parent = this.node
            node.getComponent(weiqi_item).hide()
        }
    }

    public getDir(){
        let car_world_p = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)
        let car_in_effect_p = this.node.getComponent(UITransform).convertToNodeSpaceAR(car_world_p)
        let dir = new Vec3(car_in_effect_p.x,car_in_effect_p.y-300)
        return dir
    }

    public getOriginPos(){
        // let car_world_p = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)
        // let car_in_effect_p = this.node.getComponent(UITransform).convertToNodeSpaceAR(car_world_p)
        // let dir = new Vec3(car_in_effect_p.x,car_in_effect_p.y-this.mCar.node.getComponent(UITransform).contentSize.height*0.5)
        // return dir
        return  new Vec3(this.mCar.node.position.x,this.mCar.node.position.y-this.mCar.spr.getComponent(UITransform).contentSize.height*0.5)
    }

    public startEffect(){
        for (let index = 0; index < this.node.children.length; index++) {
            const node = this.node.children[index];
            node.getComponent(weiqi_item).play(index*0.1)
        }
    }

    // protected update(dt: number): void {
    //     this.node.position = new Vec3(0,this.mCar.node.parent.position.y)
    // }

}


