Update framework

This commit is contained in:
2022-11-26 02:22:02 +01:00
parent 9a6ce1f832
commit ae057940af
508 changed files with 26011 additions and 14248 deletions

12
framework/scheduler/index.d.ts vendored Normal file
View File

@@ -0,0 +1,12 @@
import { EventBus } from '../event-bus';
import { RAFTimer } from './raf';
import { Timer } from './timer';
export declare class Scheduler extends EventBus {
logicPerSecond: number;
logicTimer: Timer;
drawTimer: RAFTimer;
constructor(logicPerSecond: number);
init(): void;
start(): void;
stop(): void;
}

View File

@@ -0,0 +1,37 @@
import { EventBus } from '../event-bus';
import { RAFTimer } from './raf';
import { Timer } from './timer';
export class Scheduler extends EventBus {
constructor(logicPerSecond) {
super();
this.logicPerSecond = logicPerSecond;
this.init();
}
init() {
const interval = 1000 / this.logicPerSecond;
this.logicTimer = new Timer(interval, {
id: 0,
func: (dt) => {
this.emit('preupdate.logic');
this.emit('update.logic', dt);
this.emit('postupdate.logic');
}
});
this.drawTimer = new RAFTimer({
id: 1,
func: (dt) => {
this.emit('preupdate.draw');
this.emit('update.draw', dt);
this.emit('postupdate.draw');
}
});
}
start() {
this.logicTimer.start();
this.drawTimer.start();
}
stop() {
this.logicTimer.stop();
this.drawTimer.stop();
}
}

4
framework/scheduler/node.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
export interface SchedulerNode {
id: number;
func: Function;
}

View File

@@ -0,0 +1 @@
export {};

10
framework/scheduler/raf.d.ts vendored Normal file
View File

@@ -0,0 +1,10 @@
import { SchedulerNode } from './node';
export declare class RAFTimer {
isStarted: boolean;
node: SchedulerNode;
constructor(node: SchedulerNode);
start(): void;
stop(): void;
schedule(): void;
handleResolve(): void;
}

View File

@@ -0,0 +1,24 @@
export class RAFTimer {
constructor(node) {
this.isStarted = false;
this.node = node;
}
start() {
this.isStarted = true;
this.schedule();
}
stop() {
this.isStarted = false;
}
schedule() {
window.requestAnimationFrame(this.handleResolve.bind(this));
}
handleResolve() {
if (this.node) {
this.node.func(1);
if (this.isStarted) {
this.schedule();
}
}
}
}

View File

View File

14
framework/scheduler/timer.d.ts vendored Normal file
View File

@@ -0,0 +1,14 @@
import { SchedulerNode } from './node';
export declare class Timer {
time: number;
lastTime: number;
fluctuation: number;
node: SchedulerNode;
isStarted: boolean;
intervalID: number;
constructor(time: number, node: SchedulerNode);
start(): void;
stop(): void;
schedule(): void;
handleResolve(): void;
}

View File

@@ -0,0 +1,39 @@
export class Timer {
constructor(time, node) {
this.time = time;
this.node = node;
this.isStarted = false;
}
start() {
this.isStarted = true;
this.schedule();
}
stop() {
if (this.isStarted) {
if (this.intervalID) {
clearTimeout(this.intervalID);
this.intervalID = null;
this.isStarted = false;
}
}
}
schedule() {
let toWait = this.time;
if (this.lastTime) {
const fluc = Date.now() - this.lastTime;
this.fluctuation = fluc;
toWait -= fluc;
}
this.lastTime = Date.now();
this.intervalID = setTimeout(this.handleResolve.bind(this), toWait);
}
handleResolve() {
this.lastTime = Date.now();
if (this.node) {
this.node.func(this.time / this.lastTime);
}
if (this.isStarted) {
this.schedule();
}
}
}