Advertisement
Guest User

Untitled

a guest
May 29th, 2024
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const copperE1 = document.querySelector('.copperE1');
  2.  
  3. const STOPPED = Symbol.for("@@gameloop/stopped");
  4. const PAUSED = Symbol.for("@@gameloop/paused");
  5. const RUNNING = Symbol.for("@@gameloop/running");
  6.  
  7. class GameLoop {
  8.   constructor(options = {}) {
  9.     this.state = STOPPED;
  10.         this.options = {
  11.       step: 1000 / 60,
  12.       maxUpdates: 300,
  13.       ...options
  14.     };
  15.    
  16.     this.tick = this.tick.bind(this);
  17.   }
  18.   get isStopped() {
  19.     return this.state === STOPPED;
  20.   }
  21.   get isPaused() {
  22.     return this.state === PAUSED;
  23.   }
  24.   get isRunning() {
  25.     return this.state === RUNNING;
  26.   }
  27.   start() {
  28.     if (this.isStopped) {
  29.       this.state = RUNNING;
  30.      
  31.       const lag = 0;
  32.       const delta = 0;
  33.       const total = 0;
  34.       const last = null;
  35.      
  36.       this.timing = { last, total, delta, lag };
  37.       this.frame = requestAnimationFrame(this.tick);
  38.     }
  39.   }
  40.   stop() {
  41.     if (this.isRunning || this.isPaused) {
  42.       this.state = STOPPED;
  43.       cancelAnimationFrame(this.frame);
  44.     }
  45.   }
  46.   pause() {
  47.     if (this.isRunning) {
  48.       this.state = PAUSED;
  49.       cancelAnimationFrame(this.frame);
  50.     }
  51.   }
  52.   resume() {
  53.     if (this.isPaused) {
  54.       this.state = RUNNING;
  55.       this.frame = requestAnimationFrame(this.tick);
  56.     }
  57.   }
  58.   tick(time) {
  59.     if (this.timing.last === null) this.timing.last = time;
  60.     this.timing.delta = time - this.timing.last;
  61.     this.timing.total += this.timing.delta;
  62.     this.timing.lag += this.timing.delta;
  63.     this.timing.last = time;
  64.    
  65.     let numberOfUpdates = 0;
  66.    
  67.     while (this.timing.lag >= this.options.step) {
  68.       this.timing.lag -= this.options.step;
  69.       this.onUpdate(this.options.step, this.timing.total);
  70.       this.numberOfUpdates++;
  71.       if (this.numberOfUpdates >= this.options.maxUpdates) {
  72.         this.onPanic();
  73.         break;
  74.       }
  75.     }
  76.    
  77.     this.onRender(this.timing.lag / this.options.step);
  78.    
  79.     this.frame = requestAnimationFrame(this.tick);
  80.   }
  81. }
  82.  
  83. const loop = new GameLoop();
  84.  
  85. let copper = 0;
  86. let copper_per_millisecond = 0.0002;
  87.  
  88. loop.onUpdate = function(dt, t) {
  89.   copper += copper_per_millisecond * dt;
  90. };
  91.  
  92. loop.onRender = function(i) {
  93.     copperE1.textContent = copper.toFixed(2);
  94.    
  95. };
  96.  
  97. loop.onPanic = function() {
  98.   // discard any accumulated lag time and hope for the best
  99.   this.timing.lag = 50000;
  100. };
  101.  
  102. loop.start();
  103.  
  104. function mainSave()
  105. {
  106.     var mainSave = {
  107.  
  108.         copper: copper,
  109.     }
  110.  
  111.     localStorage.setItem("mainSave",JSON.stringify(mainSave));
  112. }
  113.  
  114. function mainLoad()
  115. {
  116.     var mainSaveGame = JSON.parse(localStorage.getItem("mainSave"));
  117.     if (typeof mainSaveGame.copper !== "undefined") copper = mainSaveGame.copper;
  118.     copperE1.textContent = copper.toFixed(2);
  119.    
  120. }
  121.  
  122. function mainReset()
  123. {
  124.     localStorage.removeItem("mainSave");
  125.     document.getElementById('copper').innerHTML = copper;
  126.     location.reload();
  127. }
  128.  
  129.  
  130. function openTab(tabId) {
  131.     // Hide all tab contents
  132.     var tabContents = document.getElementsByClassName('tab-content');
  133.     for (var i = 0; i < tabContents.length; i++) {
  134.         tabContents[i].style.display = 'none';
  135.     }
  136.  
  137.     // Remove 'active-tab' class from all tabs
  138.     var tabs = document.getElementsByClassName('tab');
  139.     for (var i = 0; i < tabs.length; i++) {
  140.         tabs[i].classList.remove('active-tab');
  141.     }
  142.  
  143.     // Show the selected tab content and mark it as active
  144.     document.getElementById(tabId).style.display = 'block';
  145.     document.querySelector('[onclick="openTab(\'' + tabId + '\')"]').classList.add('active-tab');
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement