{"size": 1676, "ext": "js", "max_stars_repo_path": "src/engine/Engine.js", "max_stars_repo_name": "doughtnerd/TranquilityEngine", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/engine/Engine.js", "max_issues_repo_name": "doughtnerd/TranquilityEngine", "max_issues_repo_issues_event_min_datetime": "2021-01-28T22:59:59.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T03:51:56.000Z", "max_forks_repo_path": "src/engine/Engine.js", "max_forks_repo_name": "doughtnerd/TranquilityEngine", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 22.0526315789, "max_line_length": 93, "alphanum_fraction": 0.6640811456}
import Input from "./Input";
import PhysicsEngine from "./PhysicsEngine";
import Camera from "./rendering/Camera";
import SceneManager from "./SceneManager";
import Time from "./Time";

export default class Engine {
  static updateInterval;
  static fixedUpdateInterval;
  static timestep = 20;
  static targetFrameRate = 200;
  static frameRate = 0;
  static startTime;

  static play() {
    if (Engine.updateInterval) {
      clearInterval(Engine.updateInterval);
    }

    Engine.startTime = Date.now();

    Engine.start();

    Engine.loop();
    Engine.updateInterval = setInterval(Engine.loop, (1 / Engine.targetFrameRate) * 1000);

    Engine.fixedUpdateInterval = setInterval(() => {
      const fixedStart = Date.now();
      PhysicsEngine.fixedUpdate();
      Time.fixedDeltaTime = (20 + Date.now() - fixedStart) * 0.001;
    }, Engine.timestep);
  }

  static loop() {
    const frameStart = Date.now();

    Engine.processInput();

    Engine.update();
    Engine.lateUpdate();
    Engine.render();

    Time.deltaTime = ((1 / Engine.targetFrameRate) * 1000 + Date.now() - frameStart) * 0.001;

    Engine.frameRate = 1 / Time.deltaTime;
    Time.time += Time.deltaTime;
  }

  static start() {
    SceneManager.activeScene.start();
  }

  static processInput() {
    Input.processKeyboardInput();
    Input.processMouseInput();
  }

  static update() {
    SceneManager.activeScene.update();
  }

  static lateUpdate() {
    SceneManager.activeScene.lateUpdate();
  }

  static render() {
    Camera.allCameras.forEach((cam) => cam.render());
  }

  static stop() {
    clearInterval(Engine.updateInterval)
    clearInterval(Engine.fixedUpdateInterval)
  }
}

