"use strict";
cc._RF.push(module, 'd2addiRqXVKQ4YEp6sK5d6R', 'lq_collide');
// Script/lq_collide_system/lq_collide.ts

"use strict";
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 });
exports.LQCollide = void 0;
var ccclass = cc._decorator.ccclass;
var property = cc._decorator.property;
var requireComponent = cc._decorator.requireComponent;
var menu = cc._decorator.menu;
var Component = cc.Component;
var Enum = cc.Enum;
var Size = cc.Size;
var Vec2 = cc.Vec2;
var Graphics = cc.Graphics;
var Node = cc.Node;
var macro = cc.macro;
var Color = cc.Color;
var lq_const_1 = require("../lq_base/data/lq_const");
var lq_collide_config_1 = require("./lq_collide_config");
var lq_collide_system_1 = require("./lq_collide_system");
var lq_data_1 = require("../lq_base/data/lq_data");
var lq_collide_base_1 = require("./lq_collide_base");
var lq_game_util_1 = require("../lq_base/util/lq_game_util");
var Map_1 = require("../Map");
var LQCollide = /** @class */ (function (_super) {
    __extends(LQCollide, _super);
    function LQCollide() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._draw_collide = true;
        _this.can_move = true;
        _this._collide_shape = lq_const_1.LQCollideShape.Rect;
        _this._collide_group_index = -1;
        _this.collide_group_id = 0;
        _this.collide_scle = 1;
        _this._radius = 50;
        _this._size = new Size(100, 100);
        _this._polygon_points = [new Vec2(-45, -45), new Vec2(45, -45), new Vec2(60, 40), new Vec2(0, 70), new Vec2(-60, 40)];
        //collide碰撞位置偏移
        _this._offset = new Vec2(0, 0);
        _this.data_string = '';
        //每个collide的id唯一
        _this.collide_id = 0;
        //状态
        _this.collide_status = lq_const_1.LQCollideStatus.Idle;
        //是否可碰撞
        _this.is_enable = true;
        //是否开启碰撞前后的函数
        _this.is_open_func = true;
        //碰撞类别
        _this.collide_category = 0;
        //碰撞筛选
        _this.collide_mask = 0;
        _this.collide_map = {};
        return _this;
    }
    LQCollide_1 = LQCollide;
    Object.defineProperty(LQCollide.prototype, "draw_collide", {
        get: function () {
            return this._draw_collide;
        },
        set: function (value) {
            this._draw_collide = value;
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "collide_shape", {
        get: function () {
            return this._collide_shape;
        },
        set: function (value) {
            this._collide_shape = value;
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "collide_group_index", {
        get: function () {
            if (this._collide_group_index === -1) {
                this._collide_group_index = lq_collide_system_1.LQCollideSystem.get_info_by_id(this.collide_group_id).index;
            }
            return this._collide_group_index;
        },
        set: function (value) {
            if (this._collide_group_index === value) {
                return;
            }
            this._collide_group_index = value;
            this.collide_group_id = lq_collide_system_1.LQCollideSystem.get_group_by_index(value).id;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "radius", {
        get: function () {
            return this._radius;
        },
        set: function (value) {
            this._radius = value;
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "size", {
        // 物体速度
        // public velocity: Vec2 = new Vec2(0, 0);
        get: function () {
            return this._size;
        },
        set: function (value) {
            this._size = value;
            if (this.world_rect) {
                this.world_rect.width = value.width;
                this.world_rect.height = value.height;
                this.world_rect.half_width = value.width * 0.5;
                this.world_rect.half_height = value.height * 0.5;
            }
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "offset", {
        get: function () {
            return this._offset;
        },
        set: function (value) {
            this._offset = value;
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(LQCollide.prototype, "polygon_points", {
        get: function () {
            return this._polygon_points;
        },
        set: function (value) {
            this._polygon_points = value;
            this.draw_shape();
        },
        enumerable: false,
        configurable: true
    });
    //检测绘制组件是否添加
    LQCollide.prototype.checkDebugDrawValid = function () {
        if (!this._debugDrawer || !this._debugDrawer.isValid) {
            var node = this.node.getChildByName('Collide');
            if (!node) {
                node = new Node('Collide');
                node.zIndex = macro.MAX_ZINDEX;
                this.node.addChild(node);
                // @ts-ignore
                node._objFlags = 1096;
                this._debugDrawer = node.addComponent(Graphics);
                this._debugDrawer.lineWidth = 3;
                this._debugDrawer.strokeColor = new Color(255, 0, 0);
                this._debugDrawer.fillColor = new Color(255, 0, 0);
            }
            else {
                this._debugDrawer = node.getComponent(Graphics);
            }
        }
    };
    //绘制形状
    LQCollide.prototype.draw_shape = function () {
        if (!this._draw_collide) {
            if (this._debugDrawer) {
                this._debugDrawer.clear();
            }
            return;
        }
        this.checkDebugDrawValid();
        this._debugDrawer.clear();
        var o1 = { key: 'scaleX', value: this.node.scale };
        var o2 = { key: 'scaleY', value: this.node.scale };
        lq_game_util_1.LQGameUtil.recursion_node_property(this.node, o1);
        lq_game_util_1.LQGameUtil.recursion_node_property(this.node, o2);
        if (o1.value === 0 || o2.value === 0) {
            return;
        }
        this._debugDrawer.node.scaleX = 1 / o1.value;
        this._debugDrawer.node.scaleY = 1 / o2.value;
        switch (this._collide_shape) {
            case lq_const_1.LQCollideShape.Circle:
                this._debugDrawer.circle(+this._offset.x, +this._offset.y, this._radius);
                this._debugDrawer.stroke();
                break;
            case lq_const_1.LQCollideShape.Rect:
                this._debugDrawer.moveTo(-this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
                this._debugDrawer.lineTo(-this._size.width * 0.5 + this._offset.x, +this._size.height * 0.5 + this._offset.y);
                this._debugDrawer.lineTo(this._size.width * 0.5 + this._offset.x, +this._size.height * 0.5 + this._offset.y);
                this._debugDrawer.lineTo(this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
                this._debugDrawer.lineTo(-this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
                this._debugDrawer.stroke();
                break;
            case lq_const_1.LQCollideShape.Polygon:
                this._debugDrawer.moveTo(this._polygon_points[0].x + this._offset.x, this._polygon_points[0].y + this._offset.y);
                for (var i = 1; i < this._polygon_points.length; i++) {
                    this._debugDrawer.lineTo(this._polygon_points[i].x + this._offset.x, this._polygon_points[i].y + this._offset.y);
                }
                this._debugDrawer.lineTo(this._polygon_points[0].x + this._offset.x, this._polygon_points[0].y + this._offset.y);
                this._debugDrawer.stroke();
                break;
        }
    };
    //仅用于矩形
    LQCollide.prototype.update_size = function (width, height) {
        this._size.width = width;
        this.world_rect.width = width;
        this.world_rect.half_width = width * 0.5;
        this._size.height = height;
        this.world_rect.height = height;
        this.world_rect.half_height = height * 0.5;
        this.draw_shape();
    };
    LQCollide.prototype.init_lq_collide = function () {
        this.world_rect = new lq_data_1.LQRect(0, 0, this._size.width, this._size.height);
        this.draw_shape();
        var info = lq_collide_system_1.LQCollideSystem.get_info_by_id(this.collide_group_id);
        this.collide_mask = info.mask;
        this.collide_category = info.category;
        this.collide_id = LQCollide_1.id_maker++;
        this.updateCollisionArea();
    };
    LQCollide.prototype.updateCollisionArea = function () {
        if (this._size.width == 0 || this._size.height == 0 || Map_1.default._instance == undefined) {
            return;
        }
        if (Map_1.default._instance.node.scale) {
            var scaleX_1 = Map_1.default._instance.node.scale;
            var scaleY_1 = Map_1.default._instance.node.scale;
            if (scaleX_1 === 0 || scaleX_1 == undefined || scaleX_1 == null || scaleX_1 == 1) {
                return;
            }
            switch (this._collide_shape) {
                case lq_const_1.LQCollideShape.Circle:
                    // 圆形碰撞区域半径更新
                    this._radius = this._radius * Math.max(scaleX_1, scaleY_1);
                    break;
                case lq_const_1.LQCollideShape.Rect:
                    // 矩形碰撞区域尺寸更新
                    // console.log(this.data_string);
                    if (this._size.width !== 10 && this.data_string != "-1") {
                        // if(this._size.width == 105 || this._size.height == 105){}
                        this._size.width = this._size.width * scaleX_1;
                        //  console.log("放大倍数",scaleX);
                    }
                    if (this._size.height !== 10 && this.data_string != "-1") {
                        // if(this._size.width == 105 || this._size.height == 105){}
                        this._size.height = this._size.height * scaleY_1;
                        // console.log("放大倍数",scaleY);
                    }
                    // this._size.width = this._size.width * scaleX;
                    // this._size.height = this._size.height * scaleY;
                    if (this.world_rect) {
                        this.world_rect.width = this._size.width;
                        this.world_rect.height = this._size.height;
                        this.world_rect.half_width = this._size.width * 0.5;
                        this.world_rect.half_height = this._size.height * 0.5;
                    }
                    break;
                case lq_const_1.LQCollideShape.Polygon:
                    // 多边形碰撞点坐标更新
                    this._polygon_points = this._polygon_points.map(function (point) {
                        return new Vec2(point.x * scaleX_1, point.y * scaleY_1);
                    });
                    break;
            }
            this.draw_shape();
        }
    };
    LQCollide.prototype.enable_lq_collide = function () {
        if (this.collide_status === lq_const_1.LQCollideStatus.Live) {
            console.warn(this.node.name + '重复添加');
            return;
        }
        this.is_enable = true;
        this.collide_status = lq_const_1.LQCollideStatus.Live;
        lq_collide_system_1.LQCollideSystem.add_collide(this);
    };
    LQCollide.prototype.disable_lq_collide = function () {
        if (this.collide_status !== lq_const_1.LQCollideStatus.Live) {
            return;
        }
        this.is_enable = false;
        this.collide_status = lq_const_1.LQCollideStatus.Idle;
        lq_collide_system_1.LQCollideSystem.remove_collide(this);
    };
    LQCollide.prototype.update_lq_collide = function () {
    };
    // @ts-ignore
    // public on_collide(collide: LQCollide): void {
    //     if(collide.node.parent.uuid == this.node.parent.uuid){
    //         return;
    //     }
    //     if (LQCollideConfig.switch_print_log) {
    //         if(this.node.parent.getComponent("Block")){
    //             let block = this.node.parent.getComponent("Block");
    //             if(block.isTouch){
    //                 block.checkCollision = true;
    //                 // console.log("碰撞",this.node.name);
    //                 if(this.node.name == "top" ){
    //                     console.log("碰到上边缘");
    //                     block.moveUp = false;
    //                     if(block.touchPointY > (this.node.parent.y+this.node.height)){
    //                         block.moveY = 1;
    //                     }
    //                 }
    //                 if(this.node.name === "down"){
    //                     block.moveDown = false;
    //                     console.log("碰到下边缘");
    //                     if(block.touchPointY < (this.node.parent.y )){
    //                         block.moveY = -1;
    //                     }
    //                 }
    //                 if(this.node.name === "left" ){
    //                     block.moveLeft = false;
    //                     console.log("碰到左边缘");
    //                     if(block.touchPointX < (this.node.parent.x - this.node.parent.width)){
    //                         block.moveX = -1;
    //                     }
    //                 }
    //                 if(this.node.name === "right"){
    //                     block.moveRight = false;
    //                     console.log("碰到右边缘");
    //                     if(block.touchPointX > (this.node.parent.x)){
    //                         block.moveX = 1;
    //                     }
    //                 }
    //             }
    //         }
    //     }
    // }
    //@ts-ignore
    LQCollide.prototype.on_enter = function (collide) {
        if (this.disableCollider(collide)) {
            return;
        }
        // if (LQCollideConfig.switch_print_log) {
        //     console.log(this.node.name + ' on_enter');
        // }
    };
    //@ts-ignore
    LQCollide.prototype.on_exit = function (collide) {
        if (this.disableCollider(collide)) {
            return;
        }
        if (lq_collide_config_1.LQCollideConfig.switch_print_log) {
            if (this.node.parent.getComponent("Block")) {
                var block = this.node.parent.getComponent("Block");
                // if(!block.isTouch){
                //     return;
                // }
                block.checkCollision = false;
                if (this.node.name == "top") {
                    block.moveUp = true;
                }
                if (this.node.name === "down") {
                    block.moveDown = true;
                }
                if (this.node.name === "left") {
                    block.moveLeft = true;
                }
                if (this.node.name === "right") {
                    block.moveRight = true;
                }
            }
        }
    };
    LQCollide.prototype.on_collide = function (collide) {
        if (this.disableCollider(collide)) {
            return;
        }
        if (lq_collide_config_1.LQCollideConfig.switch_print_log) {
            if (this.node.parent.getComponent("Block")) {
                var block = this.node.parent.getComponent("Block");
                var jg = false;
                if (block.isTouch) {
                    block.checkCollision = true;
                    // console.log("碰撞",this.node.name);
                    if (this.node.name == "top") {
                        // console.log(collide.node.parent.name);
                        block.moveUp = false;
                        if (block.touchPointY > (this.node.parent.y + this.node.parent.height)) {
                            block.moveY = 1;
                        }
                        // console.log("碰到上边缘");
                    }
                    if (this.node.name === "down") {
                        block.moveDown = false;
                        if (block.touchPointY < (this.node.parent.y)) {
                            block.moveY = -1;
                        }
                        // console.log("碰到下边缘");
                    }
                    if (this.node.name === "left") {
                        block.moveLeft = false;
                        if (block.touchPointX < (this.node.parent.x - this.node.parent.width)) {
                            block.moveX = -1;
                        }
                    }
                    if (this.node.name === "right") {
                        block.moveRight = false;
                        if (block.touchPointX > (this.node.parent.x)) {
                            block.moveX = 1;
                        }
                    }
                }
            }
        }
    };
    LQCollide.prototype.disableCollider = function (collide) {
        var jg = false;
        if (collide.node && this.node) {
            if (collide.node.parent && this.node.parent) {
                if (collide.node.parent.uuid == this.node.parent.uuid) {
                    jg = true;
                }
                if (this.node.parent.getComponent("Block")) {
                    var block = this.node.parent.getComponent("Block");
                    if (block.type == 1 || block.type == 10 || block.type == 9) {
                        if (collide.node.parent == block.block_Info.node) {
                            jg = true;
                        }
                    }
                }
            }
        }
        return jg;
    };
    var LQCollide_1;
    LQCollide.id_maker = 1;
    __decorate([
        property({ displayName: '绘制形状' })
    ], LQCollide.prototype, "draw_collide", null);
    __decorate([
        property
    ], LQCollide.prototype, "_draw_collide", void 0);
    __decorate([
        property({ tooltip: '能否移动' })
    ], LQCollide.prototype, "can_move", void 0);
    __decorate([
        property({
            tooltip: '碰撞形状，None就是无敌，不参与碰撞',
            type: Enum(lq_const_1.LQCollideShape),
            displayName: '碰撞形状'
        })
    ], LQCollide.prototype, "collide_shape", null);
    __decorate([
        property()
    ], LQCollide.prototype, "_collide_shape", void 0);
    __decorate([
        property({
            type: Enum(lq_collide_config_1.LQCollideInfoList), tooltip: '碰撞类别',
            displayName: '碰撞类别'
        })
    ], LQCollide.prototype, "collide_group_index", null);
    __decorate([
        property({ serializable: false })
    ], LQCollide.prototype, "_collide_group_index", void 0);
    __decorate([
        property({ visible: false })
    ], LQCollide.prototype, "collide_group_id", void 0);
    __decorate([
        property({ visible: false })
    ], LQCollide.prototype, "collide_scle", void 0);
    __decorate([
        property({
            tooltip: 'collide半径',
            visible: function () {
                // @ts-ignore
                return this._collide_shape === lq_const_1.LQCollideShape.Circle;
            },
            displayName: '半径'
        })
    ], LQCollide.prototype, "radius", null);
    __decorate([
        property()
    ], LQCollide.prototype, "_radius", void 0);
    __decorate([
        property({
            tooltip: 'collide长宽',
            visible: function () {
                // @ts-ignore
                return this._collide_shape === lq_const_1.LQCollideShape.Rect;
            },
            displayName: '长宽'
        })
    ], LQCollide.prototype, "size", null);
    __decorate([
        property()
    ], LQCollide.prototype, "_size", void 0);
    __decorate([
        property({ displayName: '位置偏移' })
    ], LQCollide.prototype, "offset", null);
    __decorate([
        property({
            type: Vec2,
            visible: function () {
                // @ts-ignore
                return this._collide_shape === lq_const_1.LQCollideShape.Polygon;
            },
            displayName: '多边形碰撞点'
        })
    ], LQCollide.prototype, "polygon_points", null);
    __decorate([
        property()
    ], LQCollide.prototype, "_polygon_points", void 0);
    __decorate([
        property()
    ], LQCollide.prototype, "_offset", void 0);
    __decorate([
        property({ displayName: '自定义字符串' })
    ], LQCollide.prototype, "data_string", void 0);
    LQCollide = LQCollide_1 = __decorate([
        ccclass,
        requireComponent(lq_collide_base_1.LQCollideBase),
        menu("lq/collide")
    ], LQCollide);
    return LQCollide;
}(Component));
exports.LQCollide = LQCollide;

cc._RF.pop();