Skip to content

Commit 4dd8479

Browse files
Merge pull request #74 from ut-code/fix/Emulator2
2 parents 635bf45 + 7946760 commit 4dd8479

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

packages/web/src/component/Emulator.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ export interface Status {
1717
weapon: "ファイヤ" | "なし";
1818
}
1919

20-
export default function Emulator(props: {
20+
type EmulatorProps = {
2121
width: number;
2222
height: number;
2323
users: User[];
2424
hasGameStarted: boolean;
2525
isPaused: boolean;
2626
executionId: number; // エミュレーターそのものを更新するためのId
2727
handleStatuses: (statuses: Status[]) => void;
28-
onGameCompleted: (result: Result) => void;
29-
}) {
28+
onGameCompleted?: (result: Result) => void;
29+
};
30+
31+
export default function Emulator(props: EmulatorProps) {
3032
const {
3133
width,
3234
height,
@@ -52,7 +54,7 @@ export default function Emulator(props: {
5254
useEffect(() => {
5355
if (!gameRef.current) throw new Error();
5456
gameRef.current.onCompleted = (result: Result) => {
55-
onGameCompleted(result);
57+
onGameCompleted?.(result);
5658
};
5759
if (hasGameStarted) {
5860
if (!isPaused) {
@@ -69,3 +71,9 @@ export default function Emulator(props: {
6971
/>
7072
);
7173
}
74+
75+
Emulator.defaultProps = {
76+
onGameCompleted: (result: Result) => {
77+
result.keys();
78+
},
79+
};

packages/web/src/component/TestPlay.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Blockly from "blockly";
2-
import { useState, useEffect, useCallback, useMemo } from "react";
2+
import { useState, useEffect, useMemo } from "react";
33
import {
44
Accordion,
55
AccordionDetails,
@@ -27,7 +27,7 @@ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
2727
import { grey } from "@mui/material/colors";
2828
import { HiOutlineScale } from "react-icons/hi";
2929
import Emulator, { Status } from "./Emulator";
30-
import type { Result, User } from "./game";
30+
import type { User } from "./game";
3131
import { getUsers } from "../fetchAPI";
3232

3333
// const sampleUsers: [User, User, User, User] = [
@@ -301,8 +301,6 @@ export default function TestPlay(props: TestPlayProps) {
301301
() => (enemyUsers ? [currentUser].concat(enemyUsers) : null),
302302
[currentUser, enemyUsers]
303303
);
304-
305-
const onGameCompleted = useCallback((result: Result) => result, []);
306304
const [submitted, setSubmitted] = useState(false);
307305

308306
return (
@@ -322,7 +320,6 @@ export default function TestPlay(props: TestPlayProps) {
322320
isPaused={isPaused}
323321
executionId={executionId}
324322
handleStatuses={setStatuses}
325-
onGameCompleted={onGameCompleted}
326323
/>
327324
) : (
328325
<Skeleton width="100%" height="auto" />

packages/web/src/component/game.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,22 @@ class WorldRenderer {
889889
}
890890
}
891891

892+
showResult(result: Result, users: User[]) {
893+
let rank = 1;
894+
for (const id of result) {
895+
const user = users.find((eachUser) => eachUser.id === id);
896+
if (!user) throw new Error();
897+
const resultText = new PIXI.Text(`${rank}位: ${user.name}`, {
898+
fontFamily: "Arial",
899+
fontStyle: "italic",
900+
});
901+
resultText.anchor.set(0.5);
902+
resultText.position.set(400, 100 * rank);
903+
this.#pixi.stage.addChild(resultText);
904+
rank += 1;
905+
}
906+
}
907+
892908
destroy() {
893909
this.#pixi.destroy();
894910
}
@@ -1123,6 +1139,7 @@ export default class Game {
11231139
this.world.clear();
11241140
this.isEnded = true;
11251141
this.onCompleted?.(this.result);
1142+
this.worldRenderer.showResult(result, this.users);
11261143
}
11271144

11281145
destroy() {

0 commit comments

Comments
 (0)