Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
114 lines (107 loc) · 4 KB
/
Copy pathmain.js
File metadata and controls
114 lines (107 loc) · 4 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
108
109
110
111
112
113
114
//document.getElementById('debug').innerText = "Debug: Game initialized"
// 4 main regions
const buttons = ["button-1", "button-2", "button-3", "button-4"]
const sections = ["section-1", "section-2"]
for (let i = 0; i < buttons.length; i++) {
const elems = document.querySelectorAll('.' + buttons[i]);
elems.forEach(el => {
el.addEventListener('click', function(e) {
e.preventDefault();
const targetIndex = i + 1;
// if board is closed, open it and scroll to the section
if (!isBoardVisible()) {
openBoard();
scrollToSection(targetIndex);
return;
}
// if board is open and the section is already aligned, flash the button
if (isSectionAligned(targetIndex)) {
flickerPlay(e.currentTarget || el);
return;
}
// otherwise scroll to the section
scrollToSection(targetIndex);
});
});
}
function isSectionAligned(sectionIndex, threshold = 6) {
const board = document.getElementById('board');
const target = document.getElementById('section-' + sectionIndex);
if (!board || !target) return false;
const boardRect = board.getBoundingClientRect();
const targetRect = target.getBoundingClientRect();
const delta = Math.abs(targetRect.top - boardRect.top);
return delta <= threshold;
}
function scrollToSection(sectionIndex){
const container = document.getElementById('board');
const target = document.getElementById('section-' + sectionIndex);
if (!container || !target) return;
container.scrollTo({
top: target.offsetTop- container.offsetTop,
behavior: 'smooth'
});
}
function closeBoard(){
document.getElementById('board').classList.add('boardHidden');
const items = document.getElementById('items-container');
const itemShop = document.getElementById('item-shop');
// Close shop when board closes
if (itemShop) itemShop.classList.add('itemShopHidden');
// Clear shop focus state
if (window.upgradeManager) {
window.upgradeManager.shopInFocus = null;
}
// Show items bar when the board is closed
if (items) items.classList.remove('itemsHidden');
}
function openBoard(){
document.getElementById('board').classList.remove('boardHidden');
const items = document.getElementById('items-container');
const itemShop = document.getElementById('item-shop');
if (itemShop) itemShop.classList.add('itemShopHidden');
// Clear shop focus state when board becomes visible
if (window.upgradeManager) {
window.upgradeManager.shopInFocus = null;
}
// Auto-close shop after 500ms to prevent async timing issues
setTimeout(() => {
if (itemShop && !itemShop.classList.contains('itemShopHidden')) {
itemShop.classList.add('itemShopHidden');
}
if (window.upgradeManager) {
window.upgradeManager.shopInFocus = null;
}
}, 500);
// Hide items bar when the board is open
if (items) items.classList.add('itemsHidden');
}
function isBoardVisible(){
return !document.getElementById('board').classList.contains('boardHidden');
}
// Play button
document.querySelectorAll('.play').forEach(el => {
el.addEventListener('click', function(e) {
e.preventDefault();
if (isBoardVisible()) {
closeBoard();
} else {
flickerPlay(e.currentTarget);
}
});
});
function flickerPlay(el, interval = 150) {
el.classList.add('alert');
setTimeout(() => el.classList.remove('alert'), interval);
}
// Ensure the items container and item shop match the initial board visibility on load
document.addEventListener('DOMContentLoaded', () => {
const items = document.getElementById('items-container');
if (!items) return;
// Items should be visible when the board is NOT visible
if (isBoardVisible()) {
items.classList.add('itemsHidden');
} else {
items.classList.remove('itemsHidden');
}
});