-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.js
145 lines (121 loc) · 4.73 KB
/
tokens.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const bouncyness = 0.007;
(function() {
AFRAME.registerComponent( 'tokens', {
init: function() {
},
tick: function (time, delta) {
var self = this;
const tokens = window.tokens;
const initialSetup = !tokens.find(token => token.obj && window.BATscene.contains(token.obj));
for(let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if(token.dead) {
if(token.obj && token.obj.parentNode) {
token.obj.parentNode.removeChild(token.obj);
}
continue;
}
if(!token.obj || !window.BATscene.contains(token.obj)) {
// token genesis
const obj = document.createElement('a-sphere');
obj.setAttribute('color', 'hsl('+~~(Math.random()*255)+', 70%, 50%)');
obj.setAttribute('radius', '0.5');
window.BATscene.appendChild(obj);
const latestStage = token.life[initialSetup ? token.life.length -1 : 0];
const modelElement = window.BATViewer.get('elementRegistry').get(latestStage.activityId);
const position = {
x: modelElement.y * globalScaleFactor + modelElement.height / 2 * globalScaleFactor,
y: .75,
z: -modelElement.x * globalScaleFactor - modelElement.width / 2 * globalScaleFactor
};
const pos = obj.getAttribute('position');
pos.x = position.x;
pos.y = position.y;
pos.z = position.z;
obj.setAttribute('position', pos);
token.obj = obj;
token.speed = 0.03;
token.currentStage = initialSetup ? token.life.length -1 : 0;
token.currentResidence = modelElement;
token.targetPosition = [{
x: modelElement.y * globalScaleFactor + Math.random() * modelElement.height * globalScaleFactor,
z: -modelElement.x * globalScaleFactor - Math.random() * modelElement.width * globalScaleFactor
}];
token.bounceCycle = Math.random() * 2 * Math.PI;
}
while(token.currentStage < token.life.length - 1) {
if(token.speed === 0.03) {
token.targetPosition.length = 0;
}
if(token.newPositionTimeout) {
window.clearTimeout(token.newPositionTimeout);
token.newPositionTimeout = null;
}
token.speed = .7;
token.currentStage++;
const targetActivity = token.life[token.currentStage].activityId;
const sequenceFlow = token.currentResidence.outgoing.find(connection => {
return connection.businessObject.targetRef.id === targetActivity;
});
if(sequenceFlow) {
token.targetPosition.push({
x: token.currentResidence.y * globalScaleFactor + .5 * token.currentResidence.height * globalScaleFactor,
z: -token.currentResidence.x * globalScaleFactor - .5 * token.currentResidence.width * globalScaleFactor
});
sequenceFlow.waypoints.forEach(({x,y}) => {
token.targetPosition.push({
x: y * globalScaleFactor,
z: -x * globalScaleFactor
});
});
token.currentResidence = window.BATViewer.get('elementRegistry').get(targetActivity);
token.targetPosition.push({
x: token.currentResidence.y * globalScaleFactor + .5 * token.currentResidence.height * globalScaleFactor,
z: -token.currentResidence.x * globalScaleFactor - .5 * token.currentResidence.width * globalScaleFactor
});
} else {
console.log('could not find connection to next activity');
debugger;
}
}
const pos = token.obj.getAttribute('position');
if(pos) {
pos.y = .75 + .5 * Math.sin(token.bounceCycle);
token.bounceCycle += delta * bouncyness;
token.bounceCycle %= Math.PI * 2;
// move token closer to its target position
if(token.targetPosition[0]) {
const vector = [
token.targetPosition[0].x - pos.x,
token.targetPosition[0].z - pos.z
];
// normalize and apply movement speed
const vectorLength = Math.sqrt(vector[0] ** 2 + vector[1] ** 2);
vector[0] *= token.speed / vectorLength;
vector[1] *= token.speed / vectorLength;
pos.x += vector[0];
pos.z += vector[1];
if(vectorLength < token.speed) {
// targetPosition reached
pos.x = token.targetPosition[0].x;
pos.z = token.targetPosition[0].z;
token.targetPosition.shift();
}
}
token.obj.setAttribute('position', pos);
if(token.targetPosition.length === 0 && !token.newPositionTimeout) {
// set a new targetposition to avoid idling around
token.newPositionTimeout = setTimeout(() => {
token.speed = 0.03;
token.targetPosition.push({
x: token.currentResidence.y * globalScaleFactor + Math.random() * token.currentResidence.height * globalScaleFactor,
z: -token.currentResidence.x * globalScaleFactor - Math.random() * token.currentResidence.width * globalScaleFactor
});
token.newPositionTimeout = null;
}, 1500);
}
}
}
}
});
})();