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

Add argument RunnerStartParameters of Runner#start() #272

Merged
merged 13 commits into from
Aug 12, 2021
21 changes: 13 additions & 8 deletions packages/headless-driver-runner-v1/src/RunnerV1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { akashicEngine as g, gameDriver as gdr, pdi } from "@akashic/engine-files";
import { Runner, RunnerPointEvent } from "@akashic/headless-driver-runner";
import { Runner, RunnerPointEvent, RunnerStartParameters } from "@akashic/headless-driver-runner";
import { PlatformV1 } from "./platform/PlatformV1";

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -16,12 +16,15 @@ export class RunnerV1 extends Runner {
private fps: number | null = null;
private running: boolean = false;

async start(): Promise<RunnerV1Game | null> {
async start(params?: RunnerStartParameters): Promise<RunnerV1Game | null> {
let game: RunnerV1Game | null = null;
const paused = !!params?.paused;

try {
game = await this.initGameDriver();
this.running = true;
if (!paused) {
this.resume();
}
} catch (e) {
this.onError(e);
}
Expand Down Expand Up @@ -180,6 +183,7 @@ export class RunnerV1 extends Runner {
errorHandler: (e) => this.onError(e),
loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
});
this.pause();

const driver = new gdr.GameDriver({
platform: this.platform,
Expand All @@ -188,7 +192,7 @@ export class RunnerV1 extends Runner {
});

this.driver = driver;

let result: RunnerV1Game | null = null;
// TODO: パラメータを外部から変更可能にする
driver.initialize(
{
Expand All @@ -210,7 +214,11 @@ export class RunnerV1 extends Runner {
reject(e);
return;
}
if (!result) {
return reject(new Error("Game is null."));
}
driver.startGame();
resolve(result);
}
);

Expand All @@ -221,10 +229,7 @@ export class RunnerV1 extends Runner {
});
}
this.fps = game.fps;
game._started.handle(() => {
resolve(game);
return true;
});
result = game;
return true;
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/headless-driver-runner-v1/src/platform/PlatformV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class PlatformV1 extends Platform implements pdi.Platform {
private primarySurface: g.Surface | null = null;
private eventHandler: pdi.PlatformEventHandler | null = null;
private loopers: Looper[] = [];
private isLooperPaused: boolean = false;

constructor(param: PlatformParameters) {
super(param);
Expand Down Expand Up @@ -40,6 +41,9 @@ export class PlatformV1 extends Platform implements pdi.Platform {

createLooper(fun: (deltaTime: number) => number): Looper {
const looper = new Looper(fun, (e: Error) => this.errorHandler(e));
if (this.isLooperPaused) {
looper.debugStop();
}
this.loopers.push(looper);
return looper;
}
Expand All @@ -51,12 +55,14 @@ export class PlatformV1 extends Platform implements pdi.Platform {
}

pauseLoopers(): void {
this.isLooperPaused = true;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStop();
}
}

resumeLoopers(): void {
this.isLooperPaused = false;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStart();
}
Expand Down
18 changes: 13 additions & 5 deletions packages/headless-driver-runner-v2/src/RunnerV2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { akashicEngine as g, gameDriver as gdr, pdi } from "@akashic/engine-files";
import { Runner, RunnerPointEvent } from "@akashic/headless-driver-runner";
import { Runner, RunnerPointEvent, RunnerStartParameters } from "@akashic/headless-driver-runner";
import { PlatformV2 } from "./platform/PlatformV2";

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -16,12 +16,15 @@ export class RunnerV2 extends Runner {
private fps: number | null = null;
private running: boolean = false;

async start(): Promise<RunnerV2Game | null> {
async start(params?: RunnerStartParameters): Promise<RunnerV2Game | null> {
let game: RunnerV2Game | null = null;
const paused = !!params?.paused;

try {
game = await this.initGameDriver();
this.running = true;
if (!paused) {
this.resume();
}
} catch (e) {
this.onError(e);
}
Expand Down Expand Up @@ -180,6 +183,7 @@ export class RunnerV2 extends Runner {
errorHandler: (e) => this.onError(e),
loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
});
this.pause();

const driver = new gdr.GameDriver({
platform: this.platform,
Expand All @@ -188,7 +192,7 @@ export class RunnerV2 extends Runner {
});

this.driver = driver;

let result: RunnerV2Game | null = null;
// TODO: パラメータを外部から変更可能にする
driver.initialize(
{
Expand All @@ -210,7 +214,11 @@ export class RunnerV2 extends Runner {
reject(e);
return;
}
if (!result) {
return reject(new Error("Game is null."));
}
driver.startGame();
resolve(result);
}
);

Expand All @@ -221,7 +229,7 @@ export class RunnerV2 extends Runner {
});
}
this.fps = game.fps;
game._started.addOnce(() => resolve(game));
result = game;
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/headless-driver-runner-v2/src/platform/PlatformV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class PlatformV2 extends Platform implements pdi.Platform {
private primarySurface: g.Surface | null = null;
private eventHandler: pdi.PlatformEventHandler | null = null;
private loopers: Looper[] = [];
private isLooperPaused: boolean = false;

constructor(param: PlatformParameters) {
super(param);
Expand Down Expand Up @@ -44,6 +45,9 @@ export class PlatformV2 extends Platform implements pdi.Platform {

createLooper(fun: (deltaTime: number) => number): Looper {
const looper = new Looper(fun, (e: Error) => this.errorHandler(e));
if (this.isLooperPaused) {
looper.debugStop();
}
this.loopers.push(looper);
return looper;
}
Expand All @@ -55,12 +59,14 @@ export class PlatformV2 extends Platform implements pdi.Platform {
}

pauseLoopers(): void {
this.isLooperPaused = true;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStop();
}
}

resumeLoopers(): void {
this.isLooperPaused = false;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStart();
}
Expand Down
18 changes: 13 additions & 5 deletions packages/headless-driver-runner-v3/src/RunnerV3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Runner, RunnerPointEvent } from "@akashic/headless-driver-runner";
import { Runner, RunnerPointEvent, RunnerStartParameters } from "@akashic/headless-driver-runner";
import type { Canvas } from "canvas";
import { akashicEngine as g, gameDriver as gdr } from "./engineFiles";
import type { NodeCanvasSurface } from "./platform/graphics/canvas/NodeCanvasSurface";
Expand All @@ -19,12 +19,15 @@ export class RunnerV3 extends Runner {
private fps: number | null = null;
private running: boolean = false;

async start(): Promise<RunnerV3Game | null> {
async start(params?: RunnerStartParameters): Promise<RunnerV3Game | null> {
let game: RunnerV3Game | null = null;
const paused = !!params?.paused;

try {
game = await this.initGameDriver();
this.running = true;
if (!paused) {
this.resume();
}
} catch (e) {
this.onError(e);
}
Expand Down Expand Up @@ -203,6 +206,7 @@ export class RunnerV3 extends Runner {
errorHandler: (e) => this.onError(e),
loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
});
this.pause();

const driver = new gdr.GameDriver({
platform: this.platform,
Expand All @@ -211,7 +215,7 @@ export class RunnerV3 extends Runner {
});

this.driver = driver;

let result: RunnerV3Game | null = null;
// TODO: パラメータを外部から変更可能にする
driver.initialize(
{
Expand All @@ -233,7 +237,11 @@ export class RunnerV3 extends Runner {
reject(e);
return;
}
if (!result) {
return reject(new Error("Game is null."));
Copy link
Member

@xnv xnv Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内部動作的には、 configurationUrl を渡しているのでこのパスに来ることはないはずです。「 GameDriver の内部実装上ここに来ることはないはずだが念のため確認」というコメントをつけておきたいです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメント追加しました。

}
driver.startGame();
resolve(result);
}
);

Expand All @@ -244,7 +252,7 @@ export class RunnerV3 extends Runner {
});
}
this.fps = game.fps;
game._onStart.addOnce(() => resolve(game));
result = game;
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/headless-driver-runner-v3/src/platform/PlatformV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class PlatformV3 extends Platform implements pdi.Platform {
private primarySurface: g.Surface | null = null;
private eventHandler: pdi.PlatformEventHandler | null = null;
private loopers: Looper[] = [];
private isLooperPaused: boolean = false;

constructor(param: PlatformParameters) {
super(param);
Expand Down Expand Up @@ -65,6 +66,9 @@ export class PlatformV3 extends Platform implements pdi.Platform {

createLooper(fun: (deltaTime: number) => number): Looper {
const looper = new Looper(fun, (e: Error) => this.errorHandler(e));
if (this.isLooperPaused) {
looper.debugStop();
}
this.loopers.push(looper);
return looper;
}
Expand All @@ -76,12 +80,14 @@ export class PlatformV3 extends Platform implements pdi.Platform {
}

pauseLoopers(): void {
this.isLooperPaused = true;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStop();
}
}

resumeLoopers(): void {
this.isLooperPaused = false;
for (let i = 0; i < this.loopers.length; i++) {
this.loopers[i].debugStart();
}
Expand Down
11 changes: 10 additions & 1 deletion packages/headless-driver-runner/src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface RunnerParameters {
externalValue?: { [key: string]: any };
}

export interface RunnerStartParameters {
/**
* コンテンツの進行を停止するかどうか。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/akashic-games/headless-driver/pull/272/files#diff-774b124f6346a73abd8982e4cb5c517120b3e4b1c87f71cc47f7bf8a941a0430R123-R130

「停止」は stop() に対応しているので、 pause() に合わせて「一時停止」にしたいです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"一時停止" に修正しました。

* 初期値は `false` 。
*/
paused?: boolean;
}

export abstract class Runner {
abstract readonly engineVersion: string;

Expand Down Expand Up @@ -108,9 +116,10 @@ export abstract class Runner {

/**
* Runner を開始する。
* @param params 起動時に必要なパラメータ。
* @returns `g.game` のインスタンス。起動に失敗した場合は `null` 。
*/
abstract start(): any;
abstract start(params?: RunnerStartParameters): any;
/**
* Runner を停止する。
*/
Expand Down
5 changes: 2 additions & 3 deletions packages/headless-driver/src/__tests__/renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("コンテンツのレンダリングテスト", () => {
* このテストによる実際の描画結果は <game.jsonのディレクトリ>/actual 以下に保存される。
* 正解データとテスト結果の描画差異は <game.jsonのディレクトリ>/diff 以下に保存される。
*/
xit("content-v3 のレンダリング結果が正解データと正しいことを確認", async () => {
it("content-v3 のレンダリング結果が正解データと正しいことを確認", async () => {
// TODO: ポイントダウンイベントなどを利用しているようなもう少し本格的なコンテンツで動作を確認するように変更
const contentPath = path.resolve(__dirname, "fixtures", "content-v3");
const playManager = new PlayManager();
Expand Down Expand Up @@ -53,8 +53,7 @@ describe("コンテンツのレンダリングテスト", () => {
return `frame_${("000" + index).slice(-3)}.png`;
};

const game = (await runner.start()) as RunnerV3Game;
runner.pause();
const game = (await runner.start({ paused: true })) as RunnerV3Game;
await runner.advanceUntil(() => game.scene()!.name === "content-v3-entry-scene");

const width = game.width;
Expand Down
Loading