Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. class Ticker {
  2. private ticksPerSecond: { [id: string]: number; }
  3. private lastTick: { [id: string]: number; }
  4.  
  5. constructor() {
  6. this.ticksPerSecond = {};
  7. this.lastTick = {};
  8. }
  9.  
  10. private now() {
  11. return performance.now() / 1000;
  12. }
  13.  
  14. public tick(id: string) {
  15. const smoothing = 0.99;
  16. const now = this.now();
  17. if (!(id in this.ticksPerSecond)) {
  18. this.ticksPerSecond[id] = 0;
  19. }
  20. if (id in this.lastTick) {
  21. const tps = 1 / (now - this.lastTick[id]);
  22. this.ticksPerSecond[id] = this.ticksPerSecond[id] * smoothing + (tps * (1 - smoothing));
  23. }
  24. this.lastTick[id] = now;
  25. }
  26.  
  27. public dump() {
  28. for (let id in this.ticksPerSecond) {
  29. console.log(id + ": " + this.ticksPerSecond[id]);
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement