forked from akaziki/cocos_dragonBone_collider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragonBoneCollider.js
64 lines (56 loc) · 1.75 KB
/
dragonBoneCollider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
cc.Class({
extends: cc.Component,
properties: {},
onLoad() {
this._armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
this._armature = this._armatureDisplay.armature();
this.slots = this._armature.getSlots().filter(slot => {
return !!slot.boundingBoxData;
});
this.colliders = {};
},
start() {},
update(dt) {
this.updateColliders(this.slots, this.colliders);
},
updateColliders(slots, colliders) {
slots.map(slot => {
let [slotName, collider, node] = [slot.name, ,];
if (colliders[slotName]) {
collider = colliders[slotName];
node = collider.node;
node.parent = this.node;
node.active && (node.active = false);
} else {
node = new cc.Node(slotName);
node.groupIndex = this.node.groupIndex;
colliders[slotName] = collider = node.addComponent(cc.PolygonCollider);
}
let currentBone = slot.parent;
let transform = currentBone.global;
!node.active && (node.active = true);
node.x = transform.x;
node.y = -transform.y;
let rotation = 0;
let scaleX = 1;
let scaleY = 1;
while (currentBone) {
rotation += (currentBone.global.rotation * 180) / Math.PI;
scaleX *= currentBone.global.scaleX;
scaleY *= currentBone.global.scaleY;
currentBone = currentBone.parent;
}
node.rotation = rotation;
node.scaleX = scaleX;
node.scaleY = scaleY;
while (collider.points.length > slot.boundingBoxData.vertices.length / 2)
collider.points.pop();
for (let i = 0; i < slot.boundingBoxData.vertices.length / 2; ++i) {
collider.points[i] = cc.v2(
slot.boundingBoxData.vertices[i * 2] + slot.origin.x,
-(slot.boundingBoxData.vertices[i * 2 + 1] + slot.origin.y)
);
}
});
}
});