import { getTexture } from "./utils";
export default class GameTest1 extends engine.Container {
    constructor() {
        super();
        this.once(engine.Event.ADDED_TO_STAGE, this.setup, this);
    }

    rightHand: engine.Sprite;
    ambrella: engine.Sprite;
    playBtn: engine.Sprite;
    human: engine.Sprite;

    setup() {
        let playBg = new engine.Sprite(getTexture('c9ff7c30-2734-417d-a811-d88b6b7140b8'))
        this.addChild(playBg)

        this.human = new engine.Sprite(
            getTexture("487c1ca7-dea5-4732-b4b6-acb5406ee3a4")
        );
        this.human.x = 290;
        this.human.y = 900;

        this.rightHand = new engine.Sprite(
            getTexture("c7129f77-92a6-479e-9994-7d4b39040a15")
        );
        this.rightHand.x = -70;
        this.rightHand.y = -65;
        this.rightHand.anchorX = 196;
        this.rightHand.anchorY = 253;

        this.addChild(this.human);
        this.human.addChild(this.rightHand);

        this.playBtn = new engine.Sprite(
            getTexture("9e347bef-6e91-4c92-ba5f-f9a52e656207")
        );
        this.playBtn.x = 138;
        this.playBtn.y = 200;
        this.addChild(this.playBtn);

    }

    dripArray: any[] = [];
    clicknum: number = 0;
    R: number = 250;
    D: number = 340;
    angle: number;

    con: engine.Sprite = new engine.Sprite();

    start() {
        const middleX = this.human.x + this.human.width / 2;
        const validX1 = this.human.x;
        const validX2 = this.human.x + this.human.width;

        // setInterval(()=>{
        //     let drip = new engine.Sprite(getTexture('07f74bf6-416f-445e-98d5-021efe4c9fdc'))
        //     drip.x = Math.random() * ((this.human.x + 180)) + (this.human.x - 180) ;
        //     drip.y = Math.random() * (drip.height);
        //     this.dripArray.push(drip)
        //     this.addChild(drip);
        // },100)

        let drip = new engine.Sprite(
            getTexture("07f74bf6-416f-445e-98d5-021efe4c9fdc")
        );
        drip.x = 200;
        drip.y = 0;
        this.dripArray.push(drip);
        this.addChild(drip);

        for (let i = 0; i < 1000; i++) {
            let y =
                -Math.sqrt(
                    this.R * this.R - (i - middleX - 50) * (i - middleX - 50)
                ) +
                (this.human.y + 180);

            let img = new engine.Sprite(
                getTexture("e4a82aee-4472-4b06-bd75-02fb64f1c8c8")
            );
            img.width = 2;
            img.height = 2;
            img.x = i;
            img.y = y;
            this.con.addChild(img);
        }
        this.addChild(this.con);

        // 位移
        // setInterval(()=>{
        //     for(let drip of this.dripArray){
        //         drip.y += 10;
        //         if(drip.y > this.stage.height){
        //             this.removeChild(drip);
        //             let index = this.dripArray.indexOf(drip)
        //             this.dripArray = this.dripArray.filter((ele,i)=>i != index)
        //         }
        //     }
        // },100)

        let point1 = new engine.Container();
        // point1.beginFill(0xff0000);
        // point1.drawRect(0,0,10,10);
        point1.y = 100;
        // point1.endFill();
        this.rightHand.addChild(point1);

        let point2 = new engine.Container();
        // point2.beginFill(0xff0000);
        // point2.drawRect(0,0,10,10);
        point2.x = 170;
        // point2.endFill();
        this.rightHand.addChild(point2);

        let point3 = new engine.Container();
        // point3.beginFill(0xff0000);
        // point3.drawRect(0,0,10,10);
        point3.x = 330;
        point3.y = 100;
        // point3.endFill();
        this.rightHand.addChild(point3);

        let ld = Date.now();

        let a = 0;
        this.addEventListener(engine.Event.ENTER_FRAME, () => {
            let dt = Date.now() - ld;

            a += dt;
            if (a > 200) {
                a = 0;
                let drip = new engine.Sprite(
                    getTexture("07f74bf6-416f-445e-98d5-021efe4c9fdc")
                );
                drip.x =
                    Math.random() * (this.human.x + 180) + (this.human.x - 180);
                drip.y = Math.random() * drip.height;
                this.dripArray.push(drip);
                this.addChild(drip);
            }

            // 位移
            for (let drip of this.dripArray) {
                drip.y += (300 * dt) / 1000;
                if (drip.y > this.stage.height) {
                    this.removeChild(drip);
                    let index = this.dripArray.indexOf(drip);
                    this.dripArray = this.dripArray.filter(
                        (ele, i) => i != index
                    );
                }
            }

            let wind = -40;
            this.rightHand.rotation += wind / 60;

            if (this.rightHand.rotation <= -90) {
                this.rightHand.rotation = -90;
            } else if (this.rightHand.rotation >= 90) {
                this.rightHand.rotation = 90;
            }
            ld = Date.now();

            // return;
            // 扇形的角度
            this.angle = Math.asin(this.D / 2 / this.R) * 57.18;

            // 伞的偏移角度距离水平位置
            let validMaxX;
            let validMinX;
            // 伞的圆函数
            let ambrellaFuncY;

        
            this.rightHand.name = 'aaa';

            // a.worldMatrix.tx

            // point1,2线性方程斜率
            let k12 = (point1.worldMatrix.ty - point2.worldMatrix.ty) / ((point1.worldMatrix.tx - point2.worldMatrix.tx))
            let k23 = (point2.worldMatrix.ty - point3.worldMatrix.ty) / ((point2.worldMatrix.tx - point3.worldMatrix.tx))
            


            if (this.rightHand.rotation >= 0) {
                // 右偏移且伞左端在右边
                // if (this.rightHand.rotation - this.angle >= 0) {
                    // let x =
                    //     this.R *
                    //     Math.sin((this.rightHand.rotation - this.angle) / 180);
                    // // 伞是否完全在人的右侧     大于人物的一半宽度
                    // validMaxX = middleX + x;

                    // // 距离圆最远的垂直切线距离
                    // let rx = this.R;

                    // for (let drip of this.dripArray) {
                    //     // 伞的范围内
                    //     if (
                    //         drip.x + drip.width >= validMaxX &&
                    //         drip.x + drip.width <= middleX + this.R
                    //     ) {
                    //         let ambrellaFuncY =
                    //             -Math.sqrt(
                    //                 this.R * this.R -
                    //                     (drip.x - middleX - 50) *
                    //                         (drip.x - middleX - 50)
                    //             ) +
                    //             (this.human.y + 160);
                    //         if (drip.y + drip.height >= ambrellaFuncY) {
                    //             this.removeChild(drip);
                    //             let index = this.dripArray.indexOf(drip);
                    //             this.dripArray = this.dripArray.filter(
                    //                 (ele, i) => i != index
                    //             );
                    //         }
                    //     }
                    //     if (x - this.human.width / 2 >= 0) {
                    //         validMaxX = validX2;
                    //     }
                    //     if (
                    //         (drip.x >= validX1 && drip.x <= validMaxX) ||
                    //         (drip.x + drip.width >= validX1 &&
                    //             drip.x + drip.width <= validMaxX)
                    //     ) {
                    //         // 是否发生碰撞
                    //         if (drip.y + drip.height >= this.human.y) {
                    //             this.removeChild(drip);
                    //             let index = this.dripArray.indexOf(drip);
                    //             this.dripArray = this.dripArray.filter(
                    //                 (ele, i) => i != index
                    //             );

                    //             // let upset = new engine.Sprite(getTexture('ff43517a-27bb-4f73-948c-d7a4fa7b4c69'))
                    //             // upset.x = 294;
                    //             // upset.y = 900;
                    //             // this.addChild(upset);
                    //             // setTimeout(()=>{
                    //             //     this.removeChild(upset)
                    //             // },1000)
                    //         }
                    //     }
                    // }

                    let px1 = point1.worldMatrix.tx;
                    let px2 = point2.worldMatrix.tx;
                    let px3 = point3.worldMatrix.tx;
                    let maxX = px1 > px2 ? (px1>px3?px1:(px3)) : (px2>px3?px2:px3); 
                    let minX = px1 > px2 ? (px2>px3?px3:px2) : (px1>px3?px3:(px1));
                    
                    // let x2 = (middleX + this.R) < maxX ? (middleX+this.R):maxX;
                    for(let drip of this.dripArray){
                        // point1,2线性方程
                        let b12 = point1.worldMatrix.ty - point1.worldMatrix.tx * k12; 
                        let b23 = point2.worldMatrix.ty - point2.worldMatrix.tx * k23;
                        let y12 = k12 * drip.x + b12;
                        let y23 = k23 * drip.x + b23;

                        if(minX >= validX1){
                            if(drip.x > minX && drip.x < maxX){
                                // 伞碰撞12
                                if(drip.x < px2){
                                    if(drip.y >= y12){
                                        this.removeChild(drip);
                                        let index = this.dripArray.indexOf(drip);
                                        this.dripArray = this.dripArray.filter(
                                            (ele, i) => i != index
                                        );
                                    }
                                }
                                // 23
                                if(drip.x >= px2){
                                    if(drip.y >= y23){
                                        this.removeChild(drip);
                                        let index = this.dripArray.indexOf(drip);
                                        this.dripArray = this.dripArray.filter(
                                            (ele, i) => i != index
                                        );
                                    }
                                }
                            }

                            // 碰人
                            if(drip.x < minX && drip.x >= validX1){
                                if(drip.y >= this.human.y){
                                    this.removeChild(drip);
                                    let index = this.dripArray.indexOf(drip);
                                    this.dripArray = this.dripArray.filter(
                                        (ele, i) => i != index
                                    );
                                }
                            }
                        }
                    }

                // } else {
                //     let x =
                //         this.R *
                //         Math.sin((this.angle - this.rightHand.rotation) / 180);
                //     // 右偏移且伞左端在左边
                //     validMinX = middleX - x;
                //     // 伞的右端
                //     let rx =
                //         this.R *
                //         Math.sin((this.angle + this.rightHand.rotation) / 180);
                //     // 伞是否完全包裹人物

                //     for (let drip of this.dripArray) {
                //         // 伞的范围内

                //         if (
                //             drip.x + drip.width >= validMinX &&
                //             drip.x + drip.width < middleX + rx
                //         ) {
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-this.rightHand.anchorX-200) * (drip.x-this.rightHand.anchorX-200)) + this.rightHand.anchorY;
                //             // let r = Math.sqrt((drip.x - middleX-50)*(drip.x - middleX-50) + ((drip.y+drip.height) - (this.human.y + 200)) * ((drip.y+drip.height) - (this.human.y + 200)))
                //             ambrellaFuncY =
                //                 -Math.sqrt(
                //                     this.R * this.R -
                //                         (drip.x - middleX - 50) *
                //                             (drip.x - middleX - 50)
                //                 ) +
                //                 (this.human.y + 160);
                //             // let y = Math.sqrt(this.R*this.R - (drip.x-middleX-50)*(drip.x-middleX-50)) + (this.human.y + 200);
                //             // console.log(ambrellaFuncY,2,y,r)

                //             if (drip.y + drip.height >= ambrellaFuncY) {
                //                 // if(r <= this.R){
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );
                //             }
                //         }

                //         if (
                //             (drip.x > validX1 && drip.x < validMinX) ||
                //             (drip.x + drip.width > validX1 &&
                //                 drip.x + drip.width < validMinX)
                //         ) {
                //             // 是否发生碰撞
                //             if (drip.y + drip.height >= this.human.y) {
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );

                //                 // let upset = new engine.Sprite(getTexture('ff43517a-27bb-4f73-948c-d7a4fa7b4c69'))
                //                 // upset.x = 294;
                //                 // upset.y = 900;
                //                 // this.addChild(upset);
                //                 // setTimeout(()=>{
                //                 //     this.removeChild(upset)
                //                 // },1000)
                //             }
                //         }
                //     }
                // }
            } else {

                let px1 = point1.worldMatrix.tx;
                let px2 = point2.worldMatrix.tx;
                let px3 = point3.worldMatrix.tx;
                let maxX = px1 > px2 ? (px1>px3?px1:(px3)) : (px2>px3?px2:px3); 
                let minX = px1 > px2 ? (px2>px3?px3:px2) : (px1>px3?px3:(px1));

                for(let drip of this.dripArray){
                    // point1,2线性方程
                    let b12 = point1.worldMatrix.ty - point1.worldMatrix.tx * k12; 
                    let b23 = point2.worldMatrix.ty - point2.worldMatrix.tx * k23;
                    let y12 = k12 * drip.x + b12;
                    let y23 = k23 * drip.x + b23;

                    if(maxX <= validX2){
                        if(drip.x > minX && drip.x < maxX){
                            // 伞碰撞23
                            if(drip.x > px2){
                                if(drip.y >= y23){
                                    this.removeChild(drip);
                                    let index = this.dripArray.indexOf(drip);
                                    this.dripArray = this.dripArray.filter(
                                        (ele, i) => i != index
                                    );
                                }
                            }
                            // 12
                            if(drip.x <= px2){
                                if(drip.y >= y12){
                                    this.removeChild(drip);
                                    let index = this.dripArray.indexOf(drip);
                                    this.dripArray = this.dripArray.filter(
                                        (ele, i) => i != index
                                    );
                                }
                            }
                        }

                        // 碰人
                        if(drip.x < px1 && drip.x >= validX1){
                            if(drip.y >= this.human.y){
                                this.removeChild(drip);
                                let index = this.dripArray.indexOf(drip);
                                this.dripArray = this.dripArray.filter(
                                    (ele, i) => i != index
                                );
                            }
                        }
                    }
                }
                // if (-this.rightHand.rotation - this.angle >= 0) {
                //     let x =
                //         this.R *
                //         Math.sin((-this.angle - this.rightHand.rotation) / 180);
                //     // 左偏且在左边
                //     validMinX = middleX - x;

                //     for (let drip of this.dripArray) {
                //         // 伞的范围内(可能包括人的部分)
                //         if (
                //             drip.x + drip.width < validMinX &&
                //             drip.x + drip.width > middleX - this.R
                //         ) {
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-this.rightHand.anchorX) * (drip.x-this.rightHand.anchorX)) + this.rightHand.anchorY;
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-this.rightHand.anchorX-200) * (drip.x-this.rightHand.anchorX-200)) + this.rightHand.anchorY + 500;
                //             // let r = Math.sqrt((drip.x - middleX-50)*(drip.x - middleX-50) + ((drip.y+drip.height) - (this.human.y + 200)) * ((drip.y+drip.height) - (this.human.y + 200)))
                //             ambrellaFuncY =
                //                 -Math.sqrt(
                //                     this.R * this.R -
                //                         (drip.x - middleX - 50) *
                //                             (drip.x - middleX - 50)
                //                 ) +
                //                 (this.human.y + 160);
                //             // let y = Math.sqrt(this.R*this.R - (drip.x-middleX-50)*(drip.x-middleX-50)) + (this.human.y + 200)
                //             // console.log(ambrellaFuncY,3,y,r)

                //             // if(r <= this.R){
                //             if (drip.y + drip.height >= ambrellaFuncY) {
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );
                //             }
                //         }
                //         if (x > this.human.width / 2) {
                //             validMinX = validX1;
                //         }
                //         if (
                //             (drip.x > validMinX && drip.x < validX2) ||
                //             (drip.x + drip.width > validMinX &&
                //                 drip.x + drip.width < validX2)
                //         ) {
                //             // 淋湿人物范围
                //             // 人物是否发生碰撞
                //             if (drip.y + drip.height >= this.human.y) {
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );

                //                 // let upset = new engine.Sprite(getTexture('ff43517a-27bb-4f73-948c-d7a4fa7b4c69'))
                //                 // upset.x = 294;
                //                 // upset.y = 900;
                //                 // this.addChild(upset);
                //                 // setTimeout(()=>{
                //                 //     this.removeChild(upset)
                //                 // },1000)
                //             }
                //         }
                //     }
                // } else {
                //     // 左偏移且在右边
                //     let x =
                //         this.R *
                //         Math.sin((this.angle + this.rightHand.rotation) / 180);
                //     let lx =
                //         this.R *
                //         Math.sin((this.angle - this.rightHand.rotation) / 180);
                //     validMinX = middleX + x;

                //     for (let drip of this.dripArray) {
                //         // 雨滴和伞碰撞
                //         if (
                //             drip.x + drip.width <= validMinX &&
                //             drip.x + drip.width > middleX - lx
                //         ) {
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-this.rightHand.anchorX) * (drip.x-this.rightHand.anchorX)) + this.rightHand.anchorY;
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-middleX) * (drip.x-middleX)) + (this.human.y );
                //             // let r = Math.sqrt((drip.x - middleX-50)*(drip.x - middleX-50) + ((drip.y+drip.height) - (this.human.y + 200)) * ((drip.y+drip.height) - (this.human.y + 200)))
                //             // ambrellaFuncY = Math.sqrt(this.R*this.R - (drip.x-middleX) * (drip.x- middleX)) + (this.human.y);
                //             let ambrellaFuncY =
                //                 -Math.sqrt(
                //                     this.R * this.R -
                //                         (drip.x - middleX - 50) *
                //                             (drip.x - middleX - 50)
                //                 ) +
                //                 (this.human.y + 160);
                //             // console.log(ambrellaFuncY,4,y,r)

                //             // if(r <= this.R){
                //             if (drip.y + drip.height >= ambrellaFuncY) {
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );
                //             }
                //         } else if (
                //             (drip.x > validMinX && drip.x < validX2) ||
                //             (drip.x + drip.width > validMinX &&
                //                 drip.x + drip.width < validX2)
                //         ) {
                //             // 人物是否发生碰撞
                //             if (drip.y + drip.height >= this.human.y) {
                //                 this.removeChild(drip);
                //                 let index = this.dripArray.indexOf(drip);
                //                 this.dripArray = this.dripArray.filter(
                //                     (ele, i) => i != index
                //                 );

                //                 // let upset = new engine.Sprite(getTexture('ff43517a-27bb-4f73-948c-d7a4fa7b4c69'))
                //                 // upset.x = 294;
                //                 // upset.y = 900;
                //                 // this.addChild(upset);
                //                 // setTimeout(()=>{
                //                 //     this.removeChild(upset)
                //                 // },1000)
                //             }
                //         }
                //     }
                // }
            }

            ld = Date.now();
        });

        this.playBtn.addEventListener(
            engine.MouseEvent.CLICK,
            this.onClick,
            this
        );
        this.rightHand.rotation = 0;
    }

    onClick() {
        this.clicknum += 1;
        this.rightHand.rotation += 10 * this.clicknum;
        this.clicknum = 0;
    }
    stop() {}
}