-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
70 lines (59 loc) · 2.3 KB
/
index.ts
File metadata and controls
70 lines (59 loc) · 2.3 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
import { Game } from "./models/index.ts";
import * as _ from "./models/items/index.ts";
const game = new Game();
const init = () => {
const buttonContainer = document.querySelector(".buttons");
if (buttonContainer) {
const hostButton = document.createElement("button");
hostButton.textContent = "Host";
hostButton.dataset.dir = "c";
hostButton.addEventListener("click", host);
const joinButton = document.createElement("button");
joinButton.textContent = "Join";
joinButton.dataset.dir = "c";
joinButton.addEventListener("click", join);
location.pathname.includes("host")
? buttonContainer.append(hostButton)
: buttonContainer.append(joinButton);
}
};
const join = () => {
game.joinGame();
const name = localStorage.getItem("playerName") ||
prompt("What name would you like to use?") || "Treasure Hunter";
if (name !== "Treasure Hunter") {
localStorage.setItem("playerName", name);
}
game.createCharacter(name);
document.querySelector(".buttons")!.innerHTML = `
<button class="movement" data-dir="north">North</button>
<button class="movement" data-dir="south">South</button>
<button class="movement" data-dir="east">East</button>
<button class="movement" data-dir="west">West</button>
<button class="movement" data-dir="up">Up</button>
<button class="movement" data-dir="down">Down</button>
<button class="movement" data-dir="c">Search</button>
<button class="movement" data-dir="b">Use Item</button>
<button class="movement" data-dir="d">SECRET TUNNEL</button>
`;
};
const host = () => {
game.hostGame();
const container = document.querySelector(".buttons");
if (container) {
const startButton = document.createElement("button");
startButton.textContent = "Start Game";
startButton.dataset.dir = "north";
startButton.addEventListener("click", game.startGame);
const upButton = document.createElement("button");
upButton.textContent = "Up";
upButton.dataset.dir = "up";
upButton.addEventListener("click", () => game.changeFloor("up"));
const downButton = document.createElement("button");
downButton.textContent = "Down";
downButton.dataset.dir = "down";
downButton.addEventListener("click", () => game.changeFloor("down"));
container.append(startButton, upButton, downButton);
}
};
init();