-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
168 lines (147 loc) · 5.89 KB
/
custom.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
162
163
164
165
166
167
168
document.addEventListener("DOMContentLoaded", function () {
const gameContainer = document.getElementById("game-container");
document.getElementById("nw").addEventListener("change", map_async);
document.getElementById("nh").addEventListener("change", map_async);
document.getElementById("generate").addEventListener("click", generate_map_code);
document.getElementById("close-modal").addEventListener("click", close_modal);
// Pencil Selection --> need more pencils like brush
document.querySelectorAll('.pencil').forEach(function(pencil) {
pencil.addEventListener('click', function(event) {
document.getElementById('select-value').value = this.id;
document.querySelectorAll('.pencil').forEach(function(p) {
p.classList.remove('active-pencil');
p.parentElement.classList.remove('active-pencil');
});
this.classList.add('active-pencil');
});
});
window.onload = function() {
setTimeout(function() {
document.getElementById('modal').style.opacity = 1;
document.getElementById('modal-content').style.transform = 'translateX(0)';
}, 1500);
};
// Zoom Slider --> need to be improved, a bit laggy
let zoomTimeout;
document.getElementById('zoom-slider').addEventListener('input', function() {
clearTimeout(zoomTimeout);
zoomTimeout = setTimeout(() => {
gameContainer.style.transform = 'scale(' + this.value + ')';
}, 100);
});
let mapWidth = get_map_width();
let mapHeight = get_map_height();
let numberMap = generateNumberMap(mapWidth, mapHeight);
// generate a 2D Array with default values
function generateNumberMap(width, height) {
const numberMap = [];
for (let i = 0; i < height; i++) {
const row = [];
for (let j = 0; j < width; j++) {
row.push(0); // Default Value
}
numberMap.push(row);
}
return numberMap;
}
function close_modal() {
document.getElementById("modal").style.display = "none";
}
// clear all visual cells from the game container
function clearCells() {
const cells = document.querySelectorAll(".cell");
cells.forEach(cell => {
cell.parentNode.removeChild(cell);
});
}
function get_map_width() {
const nwidth = document.getElementById("nw").value;
return nwidth;
}
function get_map_height() {
const nheight = document.getElementById("nh").value;
return nheight;
}
// called when the width or height of the map is changed
function map_async() {
clearCells();
mapHeight = get_map_height();
mapWidth = get_map_width();
numberMap = generateNumberMap(mapWidth, mapHeight);
renderGame();
}
// output for the usear
function generate_map_code() {
let output_custom_map = "";
for (let y = 0; y < mapHeight; y++) {
for (let x = 0; x < mapWidth; x++) {
output_custom_map += numberMap[y][x];
}
output_custom_map += '\n';
}
let output_area = document.getElementById("output");
output_area.textContent = output_custom_map;
}
// click event for the cells
window.divClicked = function(y, x) {
const selectElement = document.getElementById("select-value");
let selectedOption = selectElement.value;
console.log(`Div an Position (${y}, ${x})`);
if (selectedOption === "pencil-player") {
numberMap[y][x] = 'P';
renderGame();
} else if (selectedOption === "pencil-floor") {
numberMap[y][x] = 0;
renderGame();
} else if (selectedOption === "pencil-wall") {
numberMap[y][x] = 1;
renderGame();
} else if (selectedOption === "pencil-collectible") {
numberMap[y][x] = 'C';
renderGame();
} else if (selectedOption === "pencil-goal") {
numberMap[y][x] = 'E';
renderGame();
} else if (selectedOption === "pencil-enemy" && document.getElementById("enemy-ident").value !== "" && document.getElementById("enemy-ident").value !== "6" ) {
numberMap[y][x] = document.getElementById("enemy-ident").value;
renderGame();
} else if (selectedOption === "pencil-delete") {
numberMap[y][x] = 6;
renderGame();
}
//renderGame(); perfomance besser wenn es bei jedem if steht
}
function renderGame() {
// setting grid template columns and rows
gameContainer.style.gridTemplateColumns = `repeat(${mapWidth}, 50px)`;
gameContainer.style.gridTemplateRows = `repeat(${mapHeight}, 50px)`;
gameContainer.innerHTML = "";
for (let y = 0; y < mapHeight; y++) {
for (let x = 0; x < mapWidth; x++) {
const cell = document.createElement("div");
const enemy_ident = document.getElementById("enemy-ident").value;
cell.classList.add("cell");
cell.innerHTML = `
<div style="width:100%; height:100%;" onclick="divClicked(${y}, ${x})"></div>
`;
if (numberMap[y][x] === 1) {
cell.style.backgroundImage = "url('img/wall.png')";
} else if (numberMap[y][x] === 'P') {
cell.classList.add("player");
} else if (numberMap[y][x] === 0) {
cell.style.backgroundImage = "url('img/floor.png')";
} else if (numberMap[y][x] === 'C') {
cell.classList.add("coin");
} else if (numberMap[y][x] === 'E') {
cell.classList.add("goal");
} else if (numberMap[y][x] === enemy_ident && document.getElementById("enemy-ident").value !== "" && document.getElementById("enemy-ident").value !== "6") {
cell.classList.add("enemy");
} else if (numberMap[y][x] === 6) {
cell.style.backgroundColor = "white";
}
gameContainer.appendChild(cell);
}
}
}
renderGame();
});