-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (135 loc) · 5.33 KB
/
script.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
const coverCard = document.getElementById("coverCard");
const cardFront = coverCard.querySelector(".card-front");
const cardBack = coverCard.querySelector(".card-back");
let cards = [];
let showingFront = true; // Track whether the front or back is being displayed
// Load cards from JSON
fetch("cards.json")
.then((response) => response.json())
.then((data) => {
cards = [...data];
preloadNextCard(); // Preload the first card on load
})
.catch((error) => console.error("Error loading cards:", error));
// Generate content for a card
function generateCardContent(card) {
const inverted = Math.random() < 0.15; // 15% chance to invert colours
return {
content: `
<div class="instructionText" style="color: ${inverted ? "#ffffff" : "#000000"};">
${card.Rule}
</div>
<div class="categoryText" style="color: ${card.Colour};">
${card.Category}
</div>
`,
backgroundColor: inverted ? "#000000" : "#ffffff",
textColor: inverted ? "#ffffff" : "#000000",
};
}
// HEADER BAR
// Toggle dropdown visibility
function toggleDropdown() {
const dropdown = document.querySelector('.dropdown');
dropdown.classList.toggle('show');
}
// Close dropdown when clicking outside
window.addEventListener('click', (event) => {
const dropdown = document.querySelector('.dropdown');
if (!dropdown.contains(event.target)) {
dropdown.classList.remove('show');
}
});
// Preload the content for the next card
function preloadNextCard() {
if (cards.length === 0) {
cardBack.innerHTML = `
<div class="instructionText" style="color: #000;">
No more cards!
</div>
`;
cardBack.style.backgroundColor = "#ffffff";
cardBack.style.color = "#000000";
return;
}
const randomIndex = Math.floor(Math.random() * cards.length);
const newCard = cards.splice(randomIndex, 1)[0];
const { content, backgroundColor, textColor } = generateCardContent(newCard);
// Store the content for later
cardBack.dataset.content = content;
cardBack.dataset.backgroundColor = backgroundColor;
cardBack.dataset.textColor = textColor;
cardBack.dataset.categoryColour = newCard.Colour; // Store the Category Colour
}
// Apply preloaded content to the visible card
function applyPreloadedContent(isFront = true) {
if (isFront) {
cardFront.innerHTML = cardBack.dataset.content || "";
cardFront.style.backgroundColor = cardBack.dataset.backgroundColor || "#ffffff";
cardFront.style.color = cardBack.dataset.textColor || "#000000";
} else {
cardBack.innerHTML = cardBack.dataset.content || "";
cardBack.style.backgroundColor = cardBack.dataset.backgroundColor || "#ffffff";
cardBack.style.color = cardBack.dataset.textColor || "#000000";
}
// Apply Category Text Colour after half of the flip animation
const categoryTextElements = document.querySelectorAll('.categoryText');
categoryTextElements.forEach(element => {
// Delay the colour change until halfway through the animation
setTimeout(() => {
element.style.color = cardBack.dataset.categoryColour || "#000000"; // Apply the Category Colour dynamically
}, 250); // Change the colour after 250ms (halfway through the 0.5s animation)
});
}
// Flip card function
function flipCard() {
const audio = new Audio("flip.mp3");
audio.play();
// Preload the next card before flipping
preloadNextCard();
// Apply content to the visible card
if (showingFront) {
applyPreloadedContent(false); // Apply to back
} else {
applyPreloadedContent(true); // Apply to front
}
// Toggle flip animation
coverCard.classList.toggle("flipped");
showingFront = !showingFront; // Switch between front and back
}
// Event listener for card flip animation end
coverCard.addEventListener("transitionend", () => {
if (!showingFront) {
// After the flip, update the content
applyPreloadedContent(false);
}
});
// Add click event to initiate flip
coverCard.addEventListener("click", flipCard);
// Modal functionality
const showRulesLink = document.getElementById("showRulesLink");
const rulesModal = document.getElementById("rulesModal");
const closeRulesButton = document.getElementById("closeRulesButton");
const rulesText = document.getElementById("rulesText");
// Rules embedded directly in the JavaScript
const embeddedRules = `
How to Play:
Take it in turns tapping the card to draw a new card.
If your drawn card is white, perform the instruction on the card.
If your drawn card is black, invert the instruction, perform the inverted instruction.
Some inverted cards make more sense than others. Don't worry if a card isn't obviously invertable, just try it, chill, and have fun.
`;
// Function to display the rules
function displayEmbeddedRules() {
rulesText.textContent = embeddedRules; // Display the rules text in the modal
rulesModal.style.display = "flex"; // Show the modal
}
// Event listener for the "View Game Rules" link
showRulesLink.addEventListener("click", (event) => {
event.preventDefault(); // Prevent default link behaviour
displayEmbeddedRules(); // Show the rules modal
});
// Event listener for closing the modal
closeRulesButton.addEventListener("click", () => {
rulesModal.style.display = "none"; // Hide the modal
});