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;
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 = [];
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) {
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();
}
}