import { _decorator, BoxCollider2D, CircleCollider2D, Component, Label, Node, Size, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { model_content_item_data } from '../data';
import { base } from './base';
import { car } from './car';
const { ccclass, property } = _decorator;

@ccclass('wall')
export class wall extends base {
    private active_node:boolean = false
    private mCar:car = null;
    public init(data:model_content_item_data,car:car){
        this.mData = data
        this.active_node = false
        this.mCar = car
        this.node.position = new Vec3(this.mData.x,this.mData.y,this.node.position.z)
        this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
        this.node.getComponent(BoxCollider2D).size = new Size(this.mData.w,this.mData.h)
    }

    protected ReSetNode(){
        this.node.position = Vec3.ZERO
    }

    getIsActive(){
        return this.active_node
    }

    getIsCanCollider(){
        let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
        let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
        return Vec3.distance(p,this.mCar.node.position)<500
    }

    updateBoxShowStatus(){
        this.node.getComponent(Sprite).enabled = this.active_node
    }

    protected lateUpdate(dt: number): void {
        if(this.mCar!=null){
            let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
            let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
            if(p.y<(this.mCar.node.position.y+1800)&&p.y>this.mCar.node.position.y&&!this.active_node){
                this.active_node = true
            }

            if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
                this.active_node = false
            }
            this.updateBoxShowStatus()
        }
    }
}


