Advertisement
PokemonMaster124

Dreadful Game Loop

Dec 12th, 2022
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SLGFW.Game = class {
  2.     /**
  3.      * @type {SLGFW.Scene}
  4.      * @public
  5.      */
  6.     currentScene;
  7.  
  8.     /**
  9.      * @type {number}
  10.      * @public
  11.      */
  12.     lastTick;
  13.  
  14.     /**
  15.      *
  16.      * @param {object} config
  17.      * @param {number|string} [config.backgroundColor="black"]
  18.      * @param {HTMLCanvasElement} [config.canvas=undefined]
  19.      * @param {SLGFW.Scene} [config.scene=undefined]
  20.      */
  21.     constructor(config) {
  22.         config = config ? config : {
  23.             backgroundColor: "black",
  24.             canvas: undefined,
  25.             scene: undefined
  26.         };
  27.  
  28.         SLGFW.current_game = this;
  29.  
  30.         // Canvas stuff
  31.         if (config.canvas) {
  32.             this.canvas = config.canvas;
  33.         } else {
  34.             this.canvas = document.createElement("canvas");
  35.             this.canvas.width = 640;
  36.             this.canvas.height = 480;
  37.             document.body.appendChild(this.canvas);
  38.         }
  39.         this.ctx = this.canvas.getContext("2d");
  40.  
  41.         // Set Background Color
  42.         this.setBackgroundColor(config.backgroundColor);
  43.  
  44.         if (config.scene) {
  45.             this.switchScene(config.scene);
  46.         } else {
  47.             throw new Error("You must have a scene to start in!")
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * @param {SLGFW.Scene} scene
  53.      * @public
  54.      */
  55.     switchScene(scene) {
  56.         this.currentScene = new scene();
  57.         this.lastTick = Date.now();
  58.         this.tick(this.lastTick, this.currentScene);
  59.     }
  60.  
  61.     /**
  62.      * @public
  63.      */
  64.     tick(lastTick, scene) {
  65.         const now = Date.now();
  66.         const dt = (now - lastTick) / 1000;
  67.         scene.update(dt);
  68.         scene.draw();
  69.         requestAnimationFrame(this.tick);
  70.     }
  71.  
  72.     /**
  73.      * @param {number|string} backgroundColor
  74.      * @public
  75.      */
  76.     setBackgroundColor(backgroundColor = "black") {
  77.         switch (typeof backgroundColor) {
  78.             case "string":
  79.                 this.canvas.style.backgroundColor = backgroundColor;
  80.                 break;
  81.             case "number":
  82.                 backgroundColor = SLGFW.clamp(backgroundColor, 0, 16777215);
  83.                 const stringBG = backgroundColor.toString(16);
  84.                 this.canvas.style.backgroundColor = "#" + stringBG;
  85.                 break;
  86.             default:
  87.                 break;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement