forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (37 loc) · 954 Bytes
/
index.js
File metadata and controls
42 lines (37 loc) · 954 Bytes
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
// Script example for ScriptAPI
// Author: Jayly#1397 <Jayly Discord>
// Project: https://github.com/JaylyDev/ScriptAPI
import { system } from "@minecraft/server";
let lastTick = NaN;
/**
* @typedef {{ currentTick: number, deltaTime: number }} TickEvent
*/
/**
* @type {((event: TickEvent) => void)[]}
*/
const callbacks = [];
system.runInterval(() => {
const { currentTick } = system;
const deltaTime = (Date.now() - lastTick) / 1000;
lastTick = Date.now();
for (const callback of callbacks) {
callback({ deltaTime, currentTick });
};
});
export class TickEventSignal {
/**
* @param {(event: TickEvent) => void} callback
*/
subscribe (callback) {
callbacks.push(callback);
return callback;
}
/**
* @param {(event: TickEvent) => void} callback
*/
unsubscribe (callback) {
const index = callbacks.indexOf(callback);
callbacks.splice(index, 1);
}
};
export const tick = new TickEventSignal();