Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix[engine]: pause engine if current tab is not active
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Feb 8, 2022
1 parent da7201e commit c4bea52
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
"build": "npm run clear && rollup -c",
"test": "node test/spec",
"pretest": "npm run build",
"beta": "npm run build && npm publish --tag beta",
"patch": "npm run build && npm version patch --force && npm publish",
"minor": "npm run build && npm version minor --force && npm publish",
"major": "npm run build && npm version major --force && npm publish",
"bump": "np",
"package": "npm run build && npm publish"
},
"dependencies": {},
Expand Down
8 changes: 4 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import babel from "rollup-plugin-babel";
const umd = {
input: "src/Impacto.js",
output: {
name: "impacto",
name: "Impacto",
file: "dist/Impacto.umd.js", // Universal Module Definition
format: "umd",
},
Expand All @@ -19,12 +19,12 @@ const umd = {
};

export default [
// umd,
umd,
{
input: "src/Impacto.js",
output: [ // https://betterprogramming.pub/what-are-cjs-amd-umd-esm-system-and-iife-3633a112db62
{ file: "dist/Impacto.esm.js", format: "es" }, // ES Module
{ file: "dist/Impacto.cjs.js", format: "cjs" }, // CommonJS (Node)
{ file: "dist/Impacto.esm.js", format: "esm" }, // ES Module
{ file: "dist/Impacto.cjs.js", format: "cjs" }, // CommonJS
// { file: "dist/Impacto.amd.js", format: "amd" }, // Asynchronous Module Definition
// { file: "dist/Impacto.system.js", format: "system" }, //
// { file: "dist/Impacto.iife.js", format: "iife" }, // IIFE (vanilla) probably useless
Expand Down
5 changes: 5 additions & 0 deletions src/Scenes/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export default class Scene {
this.collisions = {
layer1: [],
};

this.time = {
delta: 0,
gameTime: 0,
}
}

addChild(child) {
Expand Down
16 changes: 12 additions & 4 deletions src/Scenes/SceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export default class SceneManager {
this.deltaTime = 0;
this.isPaused = false;

window.requestAnimationFrame(() => this.step());
window.requestAnimationFrame(this.step.bind(this));

document.addEventListener("visibilitychange", (event) => {
this.tabActive = document.hidden;
this.lastDeltaUpdate = Date.now();
});
}

addScene(scene) {
Expand All @@ -27,20 +32,23 @@ export default class SceneManager {
}

updateDeltaTime() {
if (this.tabActive) return;
const now = Date.now();
const deltaTime = (now - this.lastDeltaUpdate) * 0.01;
this.lastDeltaUpdate = now;
this.deltaTime = deltaTime;
return deltaTime;
}

step() {
window.requestAnimationFrame(() => this.step());
step(gameTime) {
window.requestAnimationFrame(this.step.bind(this));
if (this.isPaused) return;

this.updateDeltaTime();
const delta = this.updateDeltaTime();
console.log(delta, gameTime);

if (this.currentScene) {
this.currentScene.time = { delta, gameTime, };
// Collision
const layersKeys = Object.keys(this.currentScene.collisions);
layersKeys.forEach(layerKey => {
Expand Down

0 comments on commit c4bea52

Please sign in to comment.