File size: 6,613 Bytes
0dfe679 |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
document.addEventListener('DOMContentLoaded', function() {
// Initialize game state
let coins = 5000;
let currentPrize = 0;
let lootBoxes = [];
let multipliers = [];
// DOM elements
const lootBoxTrack = document.getElementById('lootBoxTrack');
const multiplierTrack = document.getElementById('multiplierTrack');
const spinLootBtn = document.getElementById('spinLootBtn');
const spinMultiplierBtn = document.getElementById('spinMultiplierBtn');
const prizeDisplay = document.getElementById('prizeDisplay');
const prizeDetails = document.getElementById('prizeDetails');
const claimBtn = document.getElementById('claimBtn');
const winModal = document.getElementById('winModal');
const modalPrize = document.getElementById('modalPrize');
const modalCloseBtn = document.getElementById('modalCloseBtn');
// Initialize loot boxes
function initLootBoxes() {
const boxTypes = [
{ type: 'common', value: 10, color: 'gray-500' },
{ type: 'common', value: 15, color: 'gray-500' },
{ type: 'common', value: 20, color: 'gray-500' },
{ type: 'rare', value: 30, color: 'blue-500' },
{ type: 'rare', value: 40, color: 'blue-500' },
{ type: 'epic', value: 60, color: 'purple-500' },
{ type: 'epic', value: 80, color: 'purple-500' },
{ type: 'legendary', value: 120, color: 'yellow-500' },
{ type: 'legendary', value: 150, color: 'yellow-500' },
{ type: 'jackpot', value: 300, color: 'orange-500' }
];
// Create enough boxes for smooth scrolling
for (let i = 0; i < 30; i++) {
const box = boxTypes[i % boxTypes.length];
lootBoxes.push(box);
const boxElement = document.createElement('div');
boxElement.className = `loot-box ${box.type} flex-shrink-0 mx-2`;
boxElement.innerHTML = `
<div class="text-${box.color} text-2xl mb-1">
${box.type === 'common' ? 'π¦' :
box.type === 'rare' ? 'π' :
box.type === 'epic' ? 'π' :
box.type === 'legendary' ? 'π' : 'π°'}
</div>
<div class="text-white text-sm">${box.value} SC</div>
`;
lootBoxTrack.appendChild(boxElement);
}
}
// Initialize multipliers
function initMultipliers() {
const multiplierValues = [1, 1.25, 1.5, 2, 3, 5, 1, 1.25, 1.5, 2, 3, 5];
// Create enough multipliers for smooth scrolling
for (let i = 0; i < 24; i++) {
const value = multiplierValues[i % multiplierValues.length];
multipliers.push(value);
const chip = document.createElement('div');
chip.className = 'multiplier-chip flex-shrink-0 mx-1';
chip.textContent = `Γ${value}`;
chip.dataset.value = value;
multiplierTrack.appendChild(chip);
}
}
// Spin animation
function spin(element, items, spinTime, callback) {
const startTime = Date.now();
const speed = 0.5; // pixels per millisecond
function animate() {
const elapsed = Date.now() - startTime;
const progress = Math.min(elapsed / spinTime, 1);
// Ease-out function
const easeProgress = 1 - Math.pow(1 - progress, 3);
// Calculate distance (more at start, less at end)
const distance = speed * spinTime * (1 - easeProgress * 0.9);
element.style.transform = `translateX(-${distance}px)`;
if (progress < 1) {
requestAnimationFrame(animate);
} else {
// Snap to nearest item
const itemWidth = items[0].offsetWidth + 8; // 8px for margin
const totalDistance = parseInt(element.style.transform.replace('translateX(-', '').replace('px)', ''));
const itemIndex = Math.round(totalDistance / itemWidth) % items.length;
const snapDistance = itemIndex * itemWidth;
element.style.transition = 'transform 0.5s ease-out';
element.style.transform = `translateX(-${snapDistance}px)`;
setTimeout(() => {
element.style.transition = '';
if (callback) callback(itemIndex);
}, 500);
}
}
animate();
}
// Spin loot box
spinLootBtn.addEventListener('click', function() {
if (coins < 10) {
alert('Not enough coins!');
return;
}
coins -= 10;
updateCoinDisplay();
spinLootBtn.disabled = true;
spin(lootBoxTrack, lootBoxTrack.children, 3000, function(index) {
const selectedBox = lootBoxes[index % lootBoxes.length];
currentPrize = selectedBox.value;
prizeDisplay.textContent = currentPrize;
prizeDetails.innerHTML = `
You got a <span class="text-${selectedBox.color} font-bold">${selectedBox.type}</span> loot box!
`;
spinLootBtn.disabled = false;
claimBtn.classList.remove('hidden');
});
});
// Spin multiplier
spinMultiplierBtn.addEventListener('click', function() {
if (coins < 5) {
alert('Not enough coins!');
return;
}
if (currentPrize === 0) {
alert('Spin a loot box first!');
return;
}
coins -= 5;
updateCoinDisplay();
spinMultiplierBtn.disabled = true;
spin(multiplierTrack, multiplierTrack.children, 2500, function(index) {
const selectedMultiplier = multipliers[index % multipliers.length];
const totalPrize = Math.round(currentPrize * selectedMultiplier);
prizeDisplay.textContent = totalPrize;
prizeDetails.innerHTML += `
<div class="mt-2">Multiplier: <span class="text-yellow-400 font-bold">Γ${selectedMultiplier}</span></div>
`;
currentPrize = totalPrize;
spinMultiplierBtn.disabled = false;
// Show win modal for big prizes
if (totalPrize >= 100) {
modalPrize.textContent = totalPrize;
winModal.classList.remove('hidden');
prizeDisplay.classList.add('winner-animation');
}
});
});
// Claim prize
claimBtn.addEventListener('click', function() {
coins += currentPrize;
currentPrize = 0;
updateCoinDisplay();
prizeDisplay.textContent = '0';
prizeDetails.textContent = 'Spin to win amazing prizes!';
prizeDisplay.classList.remove('winner-animation');
claimBtn.classList.add('hidden');
});
// Close modal
modalCloseBtn.addEventListener('click', function() {
winModal.classList.add('hidden');
});
// Update coin display
function updateCoinDisplay() {
document.querySelector('.bg-gray-700 span').textContent = coins.toLocaleString();
}
// Initialize the game
initLootBoxes();
initMultipliers();
}); |