class LootBoxCarousel extends HTMLElement {
constructor() {
super();
this.boxes = [
{ rarity: 'common', value: 5, color: 'green' },
{ rarity: 'common', value: 5, color: 'green' },
{ rarity: 'common', value: 10, color: 'green' },
{ rarity: 'rare', value: 15, color: 'blue' },
{ rarity: 'rare', value: 20, color: 'blue' },
{ rarity: 'epic', value: 30, color: 'purple' },
{ rarity: 'legendary', value: 50, color: 'gold' },
{ rarity: 'epic', value: 25, color: 'purple' },
{ rarity: 'rare', value: 15, color: 'blue' },
{ rarity: 'common', value: 10, color: 'green' }
];
this.selectedIndex = 0;
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
}
render() {
this.shadowRoot.innerHTML = `
${this.boxes.map((box, index) => `
${box.rarity}
`).join('')}
`;
// Initialize feather icons
feather.replace();
}
startSpin() {
const container = this.shadowRoot.querySelector('.boxes-container');
const boxes = this.shadowRoot.querySelectorAll('.loot-box');
// Reset selection
boxes.forEach(box => box.classList.remove('selected'));
// Animate spin
let currentPosition = 0;
let speed = 0;
const maxSpeed = 30;
const deceleration = 0.5;
const targetIndex = Math.floor(Math.random() * this.boxes.length);
const animate = () => {
currentPosition += speed;
speed = Math.max(0, speed - deceleration);
// Apply movement
container.style.transform = `translateX(${currentPosition}px)`;
// Highlight box in center
const centerPosition = -currentPosition - container.offsetWidth / 2 + this.offsetWidth / 2;
const boxWidth = 120; // box width + gap
const centerIndex = Math.round(centerPosition / boxWidth) % this.boxes.length;
const selectedIndex = (centerIndex + this.boxes.length) % this.boxes.length;
boxes.forEach((box, i) => {
box.classList.toggle('selected', i === selectedIndex);
});
// Continue animation until stopped
if (speed > 0) {
requestAnimationFrame(animate);
} else {
// Final selection
this.selectedIndex = targetIndex;
this.render();
}
};
// Initial acceleration
speed = maxSpeed;
animate();
}
getSelectedBox() {
return this.boxes[this.selectedIndex];
}
}
customElements.define('loot-box-carousel', LootBoxCarousel);