Cocos Creator 2.4.8 自动更新Spine多边形包围盒

export default class SpineAutoUpdatePolygonColliderListener extends cc.Component {

    private collision_event: cc.Component.EventHandler = new cc.Component.EventHandler();
    private my_collider: cc.PolygonCollider = null;
    public get Collider() { return this.my_collider; };
    private my_depend : cc.Node = null;

	/**
     * 
     * @param depend 
     * @param event 
     * @returns 
     */
    init(depend,event:{target:cc.Node,component:string,function_name:string}) {
        this.collision_event.target = event.target;
        this.collision_event.component = event.component;
        this.collision_event.handler = event.function_name;
        this.my_depend = depend;
        this.my_collider = this.node.addComponent(cc.PolygonCollider);
        this.my_collider.points = [];
        this.node.group = this.my_depend.group;
        return this;
    }

    onCollisionEnter(other) {
        //过滤同组无效碰撞
        if(other.node.group != this.node.group){
            this.collision_event && this.collision_event.emit([other]);
        }
    }

}
export default class SpineAutoUpdatePolygonCollider extends cc.Component {

    private spine: sp.Skeleton = null;
    private spine_collider_list = [];

    /**
     * 
     * @param depend 依赖节点
     * @param event 碰撞回调,ps:由于只传入了一个碰撞回调,所以收集到碰撞信息后,需要进行过滤无效碰撞
     */
    init_bone_message(depend,event:{target:cc.Node,component:string,function_name:string}) {
        this.spine = this.node.getComponent(sp.Skeleton);
        var bound = new sp.spine.SkeletonBounds();
        bound.update(this.spine._skeleton, true);
        var slots = this.spine._skeleton.slots;
        for (var i = 0; i < bound.boundingBoxes.length; ++i) {
            var attachment: sp.spine.BoundingBoxAttachment = bound.boundingBoxes[i];
            for (var j = 0; j < slots.length; ++j) {
                //这一步判断可以不用添加,添加判断只是为了二次确定数据是否匹配,理论上SkeletonBounds会根据动画师添加的BoundingBox自动获取数据
                if (attachment == slots[j].attachment) {
                    //附件匹配
                    this.spine_collider_list.push({ "attachment": attachment, "listener": this.create_bone_node(slots[j].bone.data.name,depend,event) });
                }
            }
        }
    }

    create_bone_node(bone_name,depend,event) {
        var item = new cc.Node(bone_name+"_collider");
        item.setParent(this.node);
        return item.addComponent(SpineAutoUpdatePolygonColliderListener).init(depend,event);
    }

    private update_vertex() {
        var bound = new sp.spine.SkeletonBounds();
        bound.update(this.spine._skeleton, true);
        for (var i = 0; i < bound.boundingBoxes.length; ++i) {
            var attachment: sp.spine.BoundingBoxAttachment = bound.boundingBoxes[i];
            attachment.computeWorldVertices
            for (var j = 0; j < this.spine_collider_list.length; j++) {
                if (this.spine_collider_list[j].attachment == attachment) {
                    var vertex_array = [];
                    for (var k = 0; k < bound.polygons[i].length; ++k) {
                        vertex_array.push(bound.polygons[i][k]);
                    }
                    for (var k = 0; k < vertex_array.length; ++k) {
                        if (!this.spine_collider_list[j].listener.Collider.points[k]) {
                            this.spine_collider_list[j].listener.Collider.points.push(cc.Vec2.ZERO);
                        }
                        this.spine_collider_list[j].listener.Collider.points[k].x = vertex_array[k * 2];
                        this.spine_collider_list[j].listener.Collider.points[k].y = vertex_array[k * 2 + 1];
                    }
                }
            }
        }
    }

    protected update(dt: number): void {
        this.update_vertex();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值