-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (143 loc) · 3.9 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
const WIDTH = 50; // Breite des Spielfelds
const HEIGHT = 50; // Höhe des Spielfelds
const TILE_SIZE = 16; // Größe einer Kachel in Pixeln
const Direction = {
UP: 0,
RIGHT: 1,
DOWN: 2,
LEFT: 3,
};
const buffers = [];
const songs = [];
window.addEventListener("keydown", logKey);
function logKey(e) {
if (e) {
const press = e.keyCode;
if (press === 38) {
setDirection(Direction.UP);
}
if (press === 40) {
setDirection(Direction.DOWN);
}
if (press === 37) {
setDirection(Direction.LEFT);
}
if (press === 39) {
setDirection(Direction.RIGHT);
}
}
}
// Wenn der Netzwerk-Client verbunden hat
addEventListener("connect", (event) => {
try {
addElement(event.detail.rooms);
} catch (err) {}
if (ctx) {
const roomId = sessionStorage.getItem("roomId");
login(roomId);
document.getElementById("title").textContent += ' (Raum "' + roomId + '")';
}
});
addEventListener("music", (event) => {
const musicList = document.getElementById("music-list");
if (musicList) {
XMPlayer.init();
for (var index = 0; index < event.detail.arrayBuffer.length; index++) {
var binary_string = window.atob(event.detail.arrayBuffer[index]);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
buffers.push(bytes.buffer);
const song = document.createElement("button");
musicList.append(song);
song.className = "song";
song.innerHTML = "Song " + (index + 1);
songs.push(song);
}
for (let index = 0; index < songs.length; index++) {
songs[index].addEventListener("click", function () {
if (XMPlayer) {
XMPlayer.stop();
}
XMPlayer.load(buffers[index]);
XMPlayer.play();
const pause = document.getElementById("pause");
pause.innerHTML = "Pause";
pause.style.display = "block";
});
}
}
});
// Wenn sich der Zustand des Spiels ändert
addEventListener("update", (event) => {
if (ctx === null) {
return;
}
ctx.font = "30px Arial";
clearCanvas();
for (i = 0; i < event.detail.players.length; i++) {
ctx.fillStyle = event.detail.players[i].color;
ctx.fillText(
`${event.detail.players[i].name} (${event.detail.players[i].score})`,
event.detail.players[i].x * TILE_SIZE,
event.detail.players[i].y * TILE_SIZE
);
fillTile(
event.detail.players[i].x,
event.detail.players[i].y,
event.detail.players[i].color
);
for (z = 0; z < event.detail.players[i].tail.length; z++) {
fillTile(
event.detail.players[i].tail[z].x,
event.detail.players[i].tail[z].y,
event.detail.players[i].color
);
}
}
fillTile(event.detail.room.apple.x, event.detail.room.apple.y, "green");
});
let ctx;
// Wenn die Webseite fertig geladen wurde
window.onload = () => {
// Mit Name verbinden
const user = sessionStorage.getItem("user");
if (user) {
init(user);
}
// Canvas
const canvas = document.getElementById("canvas");
if (canvas) {
ctx = canvas.getContext("2d");
canvas.style.backgroundColor = "lightgrey";
ctx.canvas.width = WIDTH * TILE_SIZE;
ctx.canvas.height = HEIGHT * TILE_SIZE;
const pause = document.getElementById("pause");
pause.addEventListener("click", stopMusic);
}
};
function stopMusic() {
if (XMPlayer) {
const pause = document.getElementById("pause");
if (!XMPlayer.playing) {
XMPlayer.play();
pause.innerHTML = "Pause";
} else {
XMPlayer.pause();
pause.innerHTML = "Play";
}
}
}
// Funktionen zum Zeichnen
function fillTile(x, y, color) {
ctx.beginPath();
ctx.rect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}