// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


const {ccclass, property, requireComponent} = cc._decorator;

@ccclass
export default class CollisionDetection extends cc.Component {
    static _instance: any;      onLoad() {
        if (CollisionDetection._instance == null) {
            CollisionDetection._instance = this;
            // cc.game.addPersistRootNode(this.node);
        }
        else {
            return;
        }

    }
    //碰撞检测函数
    public isColliding(rect1: cc.Rect, rect2: cc.Rect): { isColliding: boolean } {
        // 快速排除：检查边界框是否相交
        if (rect1.xMax < rect2.xMin || rect2.xMax < rect1.xMin || rect1.yMax < rect2.yMin || rect2.yMax < rect1.yMin) {
            return { isColliding: false };
        }

        // 调用原始的 intersects 方法
        const isColliding = rect1.intersects(rect2);

        return { isColliding };
    }


    // 获取多边形的所有边的法线
    private getNormals(polygon: cc.Vec2[]): cc.Vec2[] {
        const normals: cc.Vec2[] = [];
        const length = polygon.length;
        for (let i = 0; i < length; i++) {
            const p1 = polygon[i];
            const p2 = polygon[(i + 1) % length];
            const edge = new cc.Vec2(p2.x - p1.x, p2.y - p1.y);
            const normal = new cc.Vec2(-edge.y, edge.x);
            normal.normalize();
            normals.push(normal);
        }
        return normals;
    }

    // 将多边形投影到轴上
    private project(polygon: cc.Vec2[], axis: cc.Vec2): { min: number; max: number } {
        let min = cc.Vec2.dot(polygon[0], axis);
        let max = min;
        for (let i = 1; i < polygon.length; i++) {
            const dotProduct = cc.Vec2.dot(polygon[i], axis);
            if (dotProduct < min) {
                min = dotProduct;
            } else if (dotProduct > max) {
                max = dotProduct;
            }
        }
        return { min, max };
    }

    // 检查两个投影是否重叠
    private overlap(projection1: { min: number; max: number }, projection2: { min: number; max: number }): boolean {
        return !(projection1.max < projection2.min || projection2.max < projection1.min);
    }


}
