-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (86 loc) · 3.19 KB
/
script.js
File metadata and controls
107 lines (86 loc) · 3.19 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
101
102
103
104
105
106
107
const menuWindow = document.getElementById('menuWindow');
const editorWindow = document.getElementById('editorWindow');
const grid = document.getElementById('mapGrid');
const tileTypes = ['grass', 'road', 'water'];
const gridSize = 20;
let mapData = [];
function init() {
mapData = new Array(gridSize * gridSize).fill(0);
}
function renderGrid() {
grid.innerHTML = '';
mapData.forEach((typeIndex, index) => {
const tile = document.createElement('div');
tile.className = `tile tile-${tileTypes[typeIndex]}`;
tile.dataset.index = index;
grid.appendChild(tile);
});
}
grid.addEventListener('click', (e) => {
if (e.target.classList.contains('tile')) {
const index = parseInt(e.target.dataset.index);
mapData[index] = (mapData[index] + 1) % tileTypes.length;
e.target.className = `tile tile-${tileTypes[mapData[index]]}`;
}
});
document.getElementById('NewMapBTN').addEventListener('click', () => {
init();
renderGrid();
menuWindow.classList.add('hidden');
editorWindow.classList.remove('hidden');
});
document.getElementById('backBTN').addEventListener('click', () => {
menuWindow.classList.remove('hidden');
editorWindow.classList.add('hidden');
});
function saveMap() {
const mapName = prompt("Zadejte název mapy:");
if (!mapName) return;
const existingMaps = JSON.parse(localStorage.getItem('trackBuilder_maps')) || [];
const newMap = {
name: mapName,
data: mapData,
timestamp: new Date().toISOString()
};
existingMaps.push(newMap);
localStorage.setItem('trackBuilder_maps', JSON.stringify(existingMaps));
alert("Mapa uložena!");
}
function showLoadMenu() {
const existingMaps = JSON.parse(localStorage.getItem('trackBuilder_maps')) || [];
savedMapsList.innerHTML = '';
if (existingMaps.length === 0) {
savedMapsList.innerHTML = '<p>Žádné uložené mapy.</p>';
} else {
existingMaps.forEach((map, index) => {
const mapEntry = document.createElement('div');
mapEntry.className = 'map-item';
mapEntry.innerHTML = `
<span>${map.name}</span>
<button class="btn btn-secondary" onclick="loadMap(${index})">Načíst</button>
`;
savedMapsList.appendChild(mapEntry);
});
}
menuWindow.classList.add('hidden');
loadModal.classList.remove('hidden');
}
window.loadMap = function(index) {
const existingMaps = JSON.parse(localStorage.getItem('trackBuilder_maps'));
mapData = existingMaps[index].data;
renderGrid();
loadModal.classList.add('hidden');
editorWindow.classList.remove('hidden');
};
document.getElementById('saveBTN').addEventListener('click', saveMap);
document.getElementById('LoadMapBTN').addEventListener('click', showLoadMenu);
document.getElementById('closeLoadBTN').addEventListener('click', () => {
loadModal.classList.add('hidden');
menuWindow.classList.remove('hidden');
});
document.getElementById('NewMapBTN').addEventListener('click', () => {
init();
renderGrid();
menuWindow.classList.add('hidden');
editorWindow.classList.remove('hidden');
});