import Boy from "./Boy";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Bot extends cc.Component {
    boy:Boy;
    target:Boy;

    onLoad(){
        this.boy = this.node.getComponent(Boy);
        
        this.schedule(this.updateOperate,0.8-0.6*window.gm.botStrength);
    }

    updateOperate(){
        if(window.gm.isGameOver)return;
        //find a nearest target
        let distance = 10000;
        window.gm.boys.forEach((boy:Boy)=>{
            if(boy!=this.boy&&boy.isValid){
                let newDistance = this.boy.node.position.sub(boy.node.position).mag();
                if(newDistance<distance){
                    distance = newDistance;
                    this.target = boy;
                }
            }
        });
        //check target whether valid
        if(this.target&&this.target.isValid){
            let key = Math.floor(Math.random()*10000)%11;
            if(key<10*window.gm.botStrength){
                this.moveToTarget();
            }else{
                this.randomMove();
            }
        }
    }

    randomMove(){
        // console.log("机器人随机移动");
        let key = Math.floor(Math.random()*10000)%10;
        if(key<3){
            this.boy.leftUp();
        }else if(key<6){
            this.boy.rightUp();
        }else if(key<8){
            this.boy.up();
        }  
    }

    moveToTarget(){
        let targetX = this.target.node.x;
        let targetY = this.target.node.y;
        let targetScale = this.target.body.scaleX;
        if(this.node.y<0){
            if(this.node.y<targetY+55){
                if(targetScale<0){
                    if(targetX+55>cc.winSize.width/2-30){
                        targetX -= 55*2;
                    }
                    if(this.node.x<targetX+55){
                        this.boy.rightUp();
                    }else if(this.node.x>targetX+80){
                        this.boy.leftUp();
                    }else{
                        if(this.node.y<targetY&&this.node.y>targetY-55){
                            this.boy.leftUp();
                        }else if(this.node.y<targetY){
                            this.boy.up();
                        }
                    }
                }else if(targetScale>0){
                    if(targetX-55<-cc.winSize.width/2+30){
                        targetX += 55*2;
                    }
                    if(this.node.x>targetX-55){
                        this.boy.leftUp();
                    }else if(this.node.x<targetX-80){
                        this.boy.rightUp();
                    }else{
                        if(this.node.y<targetY&&this.node.y>targetY-55){
                            this.boy.rightUp();
                        }else if(this.node.y<targetY){
                            this.boy.up();
                        }
                    }
                }
            }
        }else{
            if(this.node.y<targetY-55){
                if(targetScale<0){
                    if(this.node.x<targetX+55){
                        this.boy.rightUp();
                    }else if(this.node.x>targetX+80){
                        this.boy.leftUp();
                    }else{
                        if(this.node.y<targetY&&this.node.y>targetY-55){
                            this.boy.leftUp();
                        }else if(this.node.y<targetY){
                            this.boy.up();
                        }
                    }
                }else if(targetScale>0){
                    if(this.node.x>targetX-55){
                        this.boy.leftUp();
                    }else if(this.node.x<targetX-80){
                        this.boy.rightUp();
                    }else{
                        if(this.node.y<targetY&&this.node.y>targetY-55){
                            this.boy.rightUp();
                        }else if(this.node.y<targetY){
                            this.boy.up();
                        }
                    }
                }
            }
        }
    }
}