forked from Lukas-Bloom/N-igma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameCreation.js
151 lines (134 loc) · 4.29 KB
/
gameCreation.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { game } from "./scenes.js";
import { socket } from "./socket.js";
socket.on("reLoad", () => {
location.reload();
});
export const spawnPlayers = (p1, p2) => {
let startPlayers = get("startPlayer");
if (startPlayers.length === 2) {
p1.moveTo(startPlayers[0].pos);
p2.moveTo(startPlayers[1].pos);
}
};
export const createGame = () => {
let playerNumber = getData("playerNumber");
if (!getData("lvlIndex")) {
setData("lvlIndex", 0);
setData("roooom", getData("roooom") || "changeMe");
}
hideCanvas();
showMenu();
let codeInput;
let joinCode;
let newGameBtn;
let joinGameBtn;
let mainMenuDiv;
let gamecodeDiv;
if (!playerNumber) {
mainMenuDiv = document.getElementById("mainMenuDiv");
gamecodeDiv = document.getElementById("gamecodeDiv");
codeInput = document.getElementById("codeInput");
joinCode = document.getElementById("joinCode");
newGameBtn = document.getElementById("newGameButton");
joinGameBtn = document.getElementById("joinGameButton");
joinGameBtn.addEventListener("click", joinGame);
newGameBtn.addEventListener("click", newGame);
}
if (playerNumber === 1) newGame();
if (playerNumber === 2) {
hideMenu();
setTimeout(() => {
joinGame();
}, 1000);
}
const rlBtn = document.getElementById("rlBtn");
const rgBtn = document.getElementById("rgBtn");
rlBtn.addEventListener("click", rLvl);
rgBtn.addEventListener("click", rGame);
function rLvl() {
location.reload();
}
function rGame() {
localStorage.clear();
location.reload();
}
function newGame() {
socket.emit("startGame", getData("roooom"));
socket.on("joinCode", (c) => {
setData("roooom", c);
let codeDiv = document.createElement("span");
codeDiv.innerHTML = c;
codeDiv.style.color = "red";
codeDiv.style.fontSize = "25px";
if (joinCode)
joinCode.append("Give this code to your friend: ", codeDiv);
});
socket.on("init", () => {
const otherPlayer = 2;
const p = 1;
let playerNumber = 1;
setData("playerNumber", playerNumber);
game(p, otherPlayer);
hideMenu();
showGamecode();
showCanvas();
});
}
let code;
function joinGame() {
if (document.getElementById("codeInput").value === "Gamecode") {
code = getData("roooom");
} else {
code = document.getElementById("codeInput").value;
}
setData("roooom", code);
socket.emit("joinGame", code);
socket.on("wrongCode", (z) => {
});
socket.on("tooManyP", () => {
});
socket.on("init2", () => {
const otherPlayer = 1;
const p = 2;
let playerNumber = 2;
setData("playerNumber", playerNumber);
game(p, otherPlayer);
hideMenu();
showGamecode();
showCanvas();
});
}
function showCanvas() {
document.getElementsByTagName("canvas")[0].style.display = "block";
document.getElementsByTagName("canvas")[0].style.margin = "0 auto";
}
function hideCanvas() {
document.getElementsByTagName("canvas")[0].style.display = "none";
}
function hideMenu() {
document.getElementById("mainMenuDiv").style.display = "none";
}
function showMenu() {
document.getElementById("mainMenuDiv").style.display = "block";
}
function showGamecode() {
document.getElementById("gamecodeDiv").style.display = "block";
}
};
export const addTutorialText = () => {
if (getData("lvlIndex") === 0) {
add([sprite("tutorialTxt1"), pos(25, 220), scale(0.5), z(-2)]);
add([sprite("tutorialTxt2"), pos(25, 305), scale(0.5), z(2)]);
add([sprite("tutorialTxt3"), pos(135, 290), scale(0.5), z(2)]);
add([sprite("tutorialTxt4"), pos(270, 180), scale(0.5), z(-2)]);
add([sprite("tutorialTxt5"), pos(470, 310), scale(0.5), z(2)]);
add([sprite("tutorialTxt6"), pos(520, 155), scale(0.5), z(-2)]);
add([sprite("tutorialTxt7"), pos(680, 195), scale(0.5), z(-2)]);
add([sprite("tutorialTxt8"), pos(640, 310), scale(0.5), z(2)]);
add([sprite("tutorialTxt9"), pos(740, 310), scale(0.5), z(4)]);
add([sprite("tutorialTxt10"), pos(870, 220), scale(0.5), z(-2)]);
add([sprite("tutorialTxt11"), pos(870, 310), scale(0.5), z(20)]);
add([sprite("tutorialTxt12"), pos(1035, 230), scale(0.5), z(-2)]);
add([sprite("tutorialTxt13"), pos(1200, 185), scale(0.5), z(-2)]);
}
};