Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game Loop refactoring #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/engine/gameLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import IOC from './ioc/ioc';
import MessageBus from './messageBus/messageBus';

import { SCENE_PROVIDER_KEY_NAME, SECTIONS } from 'engine/consts/global';
const MS_PER_UPDATE = 1000 / 60;
const MS_PER_UPDATE = 1000 / 50;

class GameLoop {
constructor() {
this.gameLoopId = null;

this.sceneProvider = IOC.resolve(SCENE_PROVIDER_KEY_NAME);
this.messageBus = new MessageBus();

this.bindedTick = this.tick.bind(this);
}

_processSection(sectionName, options) {
Expand Down Expand Up @@ -45,28 +47,31 @@ class GameLoop {
this.messageBus.restore();
}

run() {
this.previous = undefined;
this.lag = MS_PER_UPDATE;
tick() {
const current = performance.now();

const that = this;
this.gameLoopId = requestAnimationFrame(function tick(current) {
that.previous = that.previous || current;
const elapsed = current - this.previous;
this.lag += elapsed;

const elapsed = current - that.previous;
that.previous = current;
that.lag += elapsed;
this.messageBus.restore();
this.messageBus.sendDelayed();

that.messageBus.restore();
that.messageBus.sendDelayed();
this._processSection(SECTIONS.EVENT_PROCESS_SECTION_NAME);
this._gameStateUpdate();
this._processSection(SECTIONS.RENDERING_SECTION_NAME, { deltaTime: elapsed });

that._processSection(SECTIONS.EVENT_PROCESS_SECTION_NAME);
that._gameStateUpdate();
that._processSection(SECTIONS.RENDERING_SECTION_NAME, { deltaTime: elapsed });
this.messageBus.clear();

that.messageBus.clear();
that.gameLoopId = requestAnimationFrame(tick);
});
this.previous = current;

this.gameLoopId = requestAnimationFrame(this.bindedTick);
}

run() {
this.previous = performance.now();
this.lag = 0;

this.gameLoopId = requestAnimationFrame(this.bindedTick);
}

stop() {
Expand Down