import EventType from "./EventType";
import Boy from "./Boy";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GroupController extends cc.Component {
    boyGroup:cc.Node;

    boxGroup:cc.Node;
    boxs:cc.Node[];

    gearGroup:cc.Node;
    bigSaw:cc.Node;

    trackGroup:cc.Node;
    tracks:cc.Node[];

    bloodGroup:cc.Node;

    lightGroup:cc.Node;
    lights:cc.Node[];
    lightDirection:number = 1;
    lightAngle:number = 0;
    lightSpeed:number = 10;
    lightMaxAngle:number = 25;

    onLoad(){
        window.gc = this;

        this.boyGroup = this.node.getChildByName('BoyGroup');

        this.boxGroup = this.node.getChildByName('BoxGroup');
        this.boxs = this.boxGroup.children;

        this.gearGroup = this.node.getChildByName('GearGroup');
        this.bigSaw = this.gearGroup.getChildByName('BigSaw');
        this.bigSaw.addComponent(BigSaw);

        this.trackGroup = this.node.getChildByName('TrackGroup');
        this.tracks = this.trackGroup.children;
        this.tracks.forEach((track:cc.Node)=>{
            if(track.name.indexOf('Left')>-1){
                track.getChildByName('Root').addComponent(Track).setDirection(-1);
            }else if(track.name.indexOf('Right')>-1){
                track.getChildByName('Root').addComponent(Track).setDirection(1);
            }
        })

        this.bloodGroup = this.node.getChildByName('BloodGroup');

        this.lightGroup = this.node.getChildByName('LightGroup');
        this.lights = this.lightGroup.children;
    }


    update(dt: number) {
        //light animate
        if(this.lightAngle<-this.lightMaxAngle){
            this.lightDirection = 1;
        }else if(this.lightAngle>this.lightMaxAngle){
            this.lightDirection = -1;
        }
        this.lightAngle += dt*this.lightSpeed*this.lightDirection;
        this.lights.forEach((light:cc.Node)=>{
            light.angle = this.lightAngle;
        });
    }
}
class BigSaw extends cc.Component{

    onCollisionEnter(other:cc.Collider){
        if(other.node.group==EventType.GROUP_BOY){
            let boy = other.node.getComponent(Boy);
            if(boy&&boy.isValid){
                if (boy.isRunOnHost()) boy.uploadFrame_beKilled(100)
            }
        }
    }
}

class Track extends cc.Component{
    acc:number = 3000;
    direction:number = 0;

    setDirection(direction:number){
        this.direction = direction;
    }

    onCollisionEnter(other:cc.Collider){
        if(other.node.group==EventType.GROUP_BOY){
            let boy = other.node.getComponent(Boy);
            if(boy&&boy.isValid){
                if (boy.isRunOnHost()) boy.addAcc(cc.v2(this.direction*this.acc,0));
            }
        }
    }

    onCollisionExit(other:cc.Collider){
        if(other.node.group==EventType.GROUP_BOY){
            let boy = other.node.getComponent(Boy);
            if(boy&&boy.isValid){
                if (boy.isRunOnHost()) boy.subAcc(cc.v2(this.direction*this.acc,0));
            }
        }
    }
}
