Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions packages/core/src/application/application-options.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IApplicationOptions {
tickRate: number;
}
11 changes: 9 additions & 2 deletions packages/core/src/application/nanoforge-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import {

import { Core } from "../core/core";
import { ApplicationConfig } from "./application-config";
import type { IApplicationOptions } from "./application-options.type";

export abstract class NanoforgeApplication {
protected applicationConfig: ApplicationConfig;
private _core: Core;
private readonly _options: IApplicationOptions;

constructor() {
constructor(options?: Partial<IApplicationOptions>) {
this.applicationConfig = new ApplicationConfig();

this._options = {
tickRate: 60,
...(options ?? {}),
};
}

public use(sym: symbol, library: ILibrary): void {
Expand All @@ -36,7 +43,7 @@ export abstract class NanoforgeApplication {

public async init(options: IRunOptions): Promise<void> {
this._core = new Core(this.applicationConfig, new ApplicationContext());
await this._core.init(options);
await this._core.init(options, this._options);
}

public run() {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/application/nanoforge-factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { type IApplicationOptions } from "./application-options.type";
import { NanoforgeClient } from "./nanoforge-client";
import { NanoforgeServer } from "./nanoforge-server";

class NanoforgeFactoryStatic {
createClient(): NanoforgeClient {
return new NanoforgeClient();
createClient(options?: Partial<IApplicationOptions>): NanoforgeClient {
return new NanoforgeClient(options);
}

createServer(): NanoforgeServer {
return new NanoforgeServer();
createServer(options?: Partial<IApplicationOptions>): NanoforgeServer {
return new NanoforgeServer(options);
}
}

Expand Down
19 changes: 15 additions & 4 deletions packages/core/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,42 @@ import {
} from "@nanoforge/common";

import { type ApplicationConfig } from "../application/application-config";
import type { IApplicationOptions } from "../application/application-options.type";

export class Core {
private readonly config: ApplicationConfig;
private readonly context: ApplicationContext;
private options: IApplicationOptions;

constructor(config: ApplicationConfig, context: ApplicationContext) {
this.config = config;
this.context = context;
}

public async init(options: IRunOptions): Promise<void> {
public async init(options: IRunOptions, appOptions: IApplicationOptions): Promise<void> {
this.options = appOptions;
await this.runInit(this.getInitContext(options));
}

public async run(): Promise<void> {
const context = this.getExecutionContext();
const libraries = this.config.libraryManager.getRunnerLibraries();
let requestAnimationFrameHandle: number;

const runner = async () => {
await this.runExecute(context, libraries);
};

const render = () => {
if (!context.isRunning) {
clearInterval(intervalHandle);
return;
}
await this.runExecute(context, libraries);
requestAnimationFrame(runner);
cancelAnimationFrame(requestAnimationFrameHandle);
requestAnimationFrameHandle = requestAnimationFrame(runner);
};
requestAnimationFrame(runner);

const intervalHandle = setInterval(render, 1000 / this.options.tickRate);
}

private getInitContext(options: IRunOptions): InitContext {
Expand Down
Loading