generated from latticexyz/mud-template-react
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathEncounterScreen.tsx
More file actions
100 lines (94 loc) · 3.16 KB
/
EncounterScreen.tsx
File metadata and controls
100 lines (94 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { useEffect, useState } from "react";
import { twMerge } from "tailwind-merge";
import { toast } from "react-toastify";
import { useMUD } from "./MUDContext";
import { MonsterCatchResult } from "./monsterCatchResult";
type Props = {
monsterName: string;
monsterEmoji: string;
};
// Throw errors if we are somehow disconnected on the encounter screen.
const noopActions = {
throwBall: async () => { throw Error("no user"); },
fleeEncounter: async () => { throw Error("no user"); },
};
export const EncounterScreen = ({ monsterName, monsterEmoji }: Props) => {
const {
systemCalls: { throwBall, fleeEncounter } = noopActions,
} = useMUD();
const [appear, setAppear] = useState(false);
useEffect(() => {
// sometimes the fade-in transition doesn't play, so a timeout is a hacky fix
const timer = setTimeout(() => setAppear(true), 100);
return () => clearTimeout(timer);
}, []);
return (
<div
className={twMerge(
"flex flex-col gap-10 items-center justify-center bg-black text-white transition-opacity duration-1000",
appear ? "opacity-100" : "opacity-0"
)}
>
<div className="text-8xl animate-bounce">{monsterEmoji}</div>
<div>A wild {monsterName} appears!</div>
<div className="flex gap-2">
<button
type="button"
className="bg-stone-600 hover:ring rounded-lg px-4 py-2"
onClick={async () => {
const toastId = toast.loading("Throwing emojiball…");
const result = await throwBall();
if (result === MonsterCatchResult.Caught) {
toast.update(toastId, {
isLoading: false,
type: "success",
render: `You caught the ${monsterName}!`,
autoClose: 5000,
closeButton: true,
});
} else if (result === MonsterCatchResult.Fled) {
toast.update(toastId, {
isLoading: false,
type: "default",
render: `Oh no, the ${monsterName} fled!`,
autoClose: 5000,
closeButton: true,
});
} else if (result === MonsterCatchResult.Missed) {
toast.update(toastId, {
isLoading: false,
type: "error",
render: "You missed!",
autoClose: 5000,
closeButton: true,
});
} else {
throw new Error(
`Unexpected catch attempt result: ${MonsterCatchResult[result]}`
);
}
}}
>
☄️ Throw
</button>
<button
type="button"
className="bg-stone-800 hover:ring rounded-lg px-4 py-2"
onClick={async () => {
const toastId = toast.loading("Running away…");
await fleeEncounter();
toast.update(toastId, {
isLoading: false,
type: "default",
render: `You ran away!`,
autoClose: 5000,
closeButton: true,
});
}}
>
🏃♂️ Run
</button>
</div>
</div>
);
};