File size: 7,537 Bytes
1e8f4c6 ddf672c 1e8f4c6 ad593d1 1e8f4c6 ad593d1 1e8f4c6 f717bcd 1e8f4c6 ddf672c ad593d1 1e8f4c6 ddf672c ad593d1 1e8f4c6 ad593d1 1e8f4c6 ad593d1 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 a290185 1e8f4c6 a290185 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 ddf672c 1e8f4c6 78b81a5 1e8f4c6 b239fb1 1e8f4c6 b239fb1 1e8f4c6 |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
import React, { useEffect, useRef, useState } from "react";
import { Box, Typography, useTheme, useMediaQuery } from "@mui/material";
import { motion, useAnimation } from "framer-motion";
import { useSoundSystem } from "../contexts/SoundContext";
// Animation timing configuration
const SLOT_ANIMATION_DURATION = 2; // Duration of each slot animation
const SLOT_SPEED = 1; // Base speed of the slot animation (higher = faster)
const TOTAL_ANIMATION_DURATION = 1; // Total duration for each slot reel in seconds
const SLOT_START_DELAY = 0.75; // Delay between each slot start in seconds
// Random words for each category
const RANDOM_STYLES = [
"Manga",
"Comics",
"Franco-Belge",
"Steampunk",
"Cyberpunk",
];
const RANDOM_GENRES = ["Action", "Fantasy", "Sci-Fi", "Mystery", "Horror"];
const RANDOM_EPOCHS = ["Future", "Medieval", "Modern", "Ancient", "Victorian"];
const SlotReel = ({ words, isActive, finalValue, onComplete, delay = 0 }) => {
const containerRef = useRef(null);
const controls = useAnimation();
const [reelItems, setReelItems] = useState([]);
const [isVisible, setIsVisible] = useState(false);
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
const { playSound } = useSoundSystem();
const lastPositionRef = useRef(0);
const itemHeightRef = useRef(0);
useEffect(() => {
if (isActive) {
const repeatedWords = Array(20)
.fill([...words])
.flat()
.map((word) => ({ word, id: Math.random() }));
repeatedWords.push({ word: finalValue, id: "final" });
setReelItems(repeatedWords);
const itemHeight = isMobile ? 60 : 80;
itemHeightRef.current = itemHeight;
const totalHeight = repeatedWords.length * itemHeight;
setTimeout(() => {
setIsVisible(true);
controls
.start({
y: [-itemHeight, -totalHeight + itemHeight],
transition: {
duration: TOTAL_ANIMATION_DURATION / SLOT_SPEED,
ease: [0.25, 0.1, 0.25, 1.0],
times: [0, 1],
onUpdate: (latest) => {
// Calculer l'index du mot actuel basé sur la position
const currentPosition = Math.abs(latest);
const currentIndex = Math.floor(currentPosition / itemHeight);
// Si on a changé de mot, jouer le son
if (
Math.floor(lastPositionRef.current / itemHeight) !==
currentIndex
) {
// Vérifier si c'est le dernier mot (final)
const isFinalWord = currentIndex === repeatedWords.length - 1;
// Jouer le son approprié
if (isFinalWord) {
playSound("lock");
} else {
playSound("tick", "normal");
}
}
lastPositionRef.current = currentPosition;
},
},
})
.then(() => {
onComplete?.();
});
}, delay * SLOT_START_DELAY * 1000);
}
return () => {
lastPositionRef.current = 0;
};
}, [isActive, finalValue, words, delay, isMobile, playSound]);
return (
<Box
ref={containerRef}
sx={{
height: isMobile ? "60px" : "80px",
overflow: "hidden",
position: "relative",
borderRadius: 2,
border: "1px solid rgba(255,255,255,0.1)",
"&::before, &::after": {
content: '""',
position: "absolute",
left: 0,
right: 0,
height: isMobile ? "30px" : "40px",
zIndex: 2,
pointerEvents: "none",
},
"&::before": {
top: 0,
background:
"linear-gradient(to bottom, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0) 100%)",
},
"&::after": {
bottom: 0,
background:
"linear-gradient(to top, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0) 100%)",
},
}}
>
<motion.div
animate={controls}
style={{
position: "absolute",
width: "100%",
opacity: isVisible ? 1 : 0,
transition: "opacity 0.3s ease-in-out",
}}
>
{reelItems.map(({ word, id }) => (
<Box
key={id}
sx={{
height: isMobile ? "60px" : "80px",
display: "flex",
alignItems: "center",
justifyContent: "center",
color: id === "final" ? "primary.main" : "#fff",
fontSize: isMobile ? "1.2rem" : "1.5rem",
fontWeight: "bold",
fontFamily: "'Inter', sans-serif",
transform: id === "final" ? "scale(1.1)" : "scale(1)",
}}
>
{word}
</Box>
))}
</motion.div>
</Box>
);
};
const SlotSection = ({ label, value, delay, isActive, onComplete, words }) => {
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
return (
<Box
sx={{
width: "100%",
marginBottom: isMobile ? "12px" : "20px",
opacity: 1,
}}
>
<Typography
variant="caption"
sx={{
display: "block",
textAlign: "center",
mb: isMobile ? 0.5 : 1,
color: "rgba(255,255,255,0.5)",
fontSize: isMobile ? "0.7rem" : "0.8rem",
letterSpacing: "0.1em",
textTransform: "uppercase",
}}
>
{label}
</Typography>
<SlotReel
words={words}
isActive={isActive}
finalValue={value}
onComplete={onComplete}
delay={delay}
/>
</Box>
);
};
export const UniverseSlotMachine = ({
style,
genre,
epoch,
activeIndex = 0,
onComplete,
}) => {
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
const handleSlotComplete = (index) => {
if (index === 2 && activeIndex >= 2) {
setTimeout(() => {
onComplete?.();
}, SLOT_ANIMATION_DURATION * 1000);
}
};
return (
<Box
sx={{
height: "100vh",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
p: isMobile ? 2 : 3,
}}
>
<Typography
variant="h5"
sx={{
mb: isMobile ? 2 : 3,
color: "#fff",
textAlign: "center",
fontWeight: 300,
letterSpacing: "0.1em",
fontSize: isMobile ? "1.2rem" : "1.5rem",
}}
>
Finding a universe
</Typography>
<Box
sx={{
maxWidth: "500px",
width: "100%",
p: isMobile ? 2 : 4,
}}
>
<SlotSection
label=""
value={style}
words={RANDOM_STYLES}
delay={0}
isActive={activeIndex >= 0}
onComplete={() => handleSlotComplete(0)}
/>
<SlotSection
label="exploring"
value={genre}
words={RANDOM_GENRES}
delay={1}
isActive={activeIndex >= 1}
onComplete={() => handleSlotComplete(1)}
/>
<SlotSection
label="set in"
value={epoch}
words={RANDOM_EPOCHS}
delay={2}
isActive={activeIndex >= 2}
onComplete={() => handleSlotComplete(2)}
/>
</Box>
</Box>
);
};
|