-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.js
91 lines (83 loc) · 2.87 KB
/
context.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
import TWEEN from 'tween.js';
const ifUtils = require('./components/if-utils');
const getNextTag = (currentTag) => {
switch(currentTag) {
case 'intro':
return 'second';
}
}
let valueElements = {};
module.exports = (ctx) => {
// Wait for the context to initialize
ctx.onInitialize(() => {
// Inject the animation() function
// for use in Idyll expressions
ctx.update({
reset: () => {
ifUtils.reset();
},
getNextDay: () => {
const { money, selfActualization } = ctx.data();
if (money >= 100) {
return "high-money";
} else if (money <= 0) {
return "low-money";
} else if (selfActualization <= 0) {
return "low-self-actualization";
} else if (selfActualization >= 100) {
return "high-self-actualization";
}
return "start-day";
},
nextTag: (next) => {
// console.log('calling nextTag');
const currentTag = ctx.data().tag;
const updated = { currentPrompt: null };
updated.tag = next || getNextTag(currentTag);
// console.log(updated);
ctx.update(updated);
},
set: (key, value, time) => {
const oldVal = ctx.data()[key]
let _tween = { value : oldVal };
console.log('tweening! ', key, value);
valueElements[key].classList.add(value > oldVal ? 'parametric-value-increased' : 'parametric-value-decreased' );
setTimeout(() => {
valueElements[key].classList.remove(value > oldVal ? 'parametric-value-increased' : 'parametric-value-decreased' );
}, 1000);
new TWEEN.Tween(_tween)
.to({value: value}, time === undefined ? 750 : time)
.easing(TWEEN.Easing.Quadratic.InOut)
.onUpdate(() => {
const updated = {};
updated[key] = _tween.value;
ctx.update(updated);
}).start();
}
})
})
ctx.onUpdate((newData) => {
// console.log('context updated', newData);
})
ctx.onMount(() => {
window.IDYLL_CONTEXT = ctx;
valueElements.selfActualization = document.getElementsByClassName("parametric-scoreboard-value self-actualization")[0];
valueElements.day = document.getElementsByClassName("parametric-scoreboard-value day")[0];
valueElements.money = document.getElementsByClassName("parametric-scoreboard-value money")[0];
// Tell TWEEN to start listening for animations
const listenForAnimations = () => {
const update = TWEEN.update();
requestAnimationFrame(listenForAnimations);
};
listenForAnimations();
})
// // Wait for the context to load in a browser
// ctx.onMount(() => {
// // Tell TWEEN to start listening for animations
// const listenForAnimations = () => {
// const update = TWEEN.update();
// requestAnimationFrame(listenForAnimations);
// };
// listenForAnimations();
// })
}