"use strict";
cc._RF.push(module, 'f1883pXzMVIp5/ARDAWfZ7j', 'CollisionDetection');
// Script/CollisionDetection.ts

"use strict";
// 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
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property, requireComponent = _a.requireComponent;
var CollisionDetection = /** @class */ (function (_super) {
    __extends(CollisionDetection, _super);
    function CollisionDetection() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CollisionDetection_1 = CollisionDetection;
    CollisionDetection.prototype.onLoad = function () {
        if (CollisionDetection_1._instance == null) {
            CollisionDetection_1._instance = this;
            // cc.game.addPersistRootNode(this.node);
        }
        else {
            return;
        }
    };
    //碰撞检测函数
    CollisionDetection.prototype.isColliding = function (rect1, rect2) {
        // 快速排除：检查边界框是否相交
        if (rect1.xMax < rect2.xMin || rect2.xMax < rect1.xMin || rect1.yMax < rect2.yMin || rect2.yMax < rect1.yMin) {
            return { isColliding: false };
        }
        // 调用原始的 intersects 方法
        var isColliding = rect1.intersects(rect2);
        return { isColliding: isColliding };
    };
    // 获取多边形的所有边的法线
    CollisionDetection.prototype.getNormals = function (polygon) {
        var normals = [];
        var length = polygon.length;
        for (var i = 0; i < length; i++) {
            var p1 = polygon[i];
            var p2 = polygon[(i + 1) % length];
            var edge = new cc.Vec2(p2.x - p1.x, p2.y - p1.y);
            var normal = new cc.Vec2(-edge.y, edge.x);
            normal.normalize();
            normals.push(normal);
        }
        return normals;
    };
    // 将多边形投影到轴上
    CollisionDetection.prototype.project = function (polygon, axis) {
        var min = cc.Vec2.dot(polygon[0], axis);
        var max = min;
        for (var i = 1; i < polygon.length; i++) {
            var dotProduct = cc.Vec2.dot(polygon[i], axis);
            if (dotProduct < min) {
                min = dotProduct;
            }
            else if (dotProduct > max) {
                max = dotProduct;
            }
        }
        return { min: min, max: max };
    };
    // 检查两个投影是否重叠
    CollisionDetection.prototype.overlap = function (projection1, projection2) {
        return !(projection1.max < projection2.min || projection2.max < projection1.min);
    };
    var CollisionDetection_1;
    CollisionDetection = CollisionDetection_1 = __decorate([
        ccclass
    ], CollisionDetection);
    return CollisionDetection;
}(cc.Component));
exports.default = CollisionDetection;

cc._RF.pop();