feat: games are no working on moblie devices
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { ArrowLeft, Trophy, Sparkles, Moon, Sun } from 'lucide-react';
|
||||
|
||||
// --- MODEL KOTA (Animacje CSS zostają, bo są czasowe, nie klatkowe) ---
|
||||
// --- MODEL KOTA (Bez zmian) ---
|
||||
interface DetailedKittyProps {
|
||||
isJumping: boolean;
|
||||
isNight: boolean;
|
||||
@@ -49,8 +49,13 @@ const DetailedKitty: React.FC<DetailedKittyProps> = ({ isJumping, isNight, isGam
|
||||
);
|
||||
};
|
||||
|
||||
// --- GŁÓWNY KOMPONENT Z DYNAMICZNYM DELTA TIME ---
|
||||
// --- STAŁE WYMIARY LOGICZNE ---
|
||||
const GAME_WIDTH = 650;
|
||||
const GAME_HEIGHT = 340;
|
||||
|
||||
// --- GŁÓWNY KOMPONENT ---
|
||||
export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
// Stany gry
|
||||
const [isPlaying, setIsPlaying] = useState(false);
|
||||
const [gameOver, setGameOver] = useState(false);
|
||||
const [score, setScore] = useState(0);
|
||||
@@ -60,25 +65,46 @@ export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
});
|
||||
|
||||
const [displayKittyY, setDisplayKittyY] = useState(0);
|
||||
const [displayObstacleX, setDisplayObstacleX] = useState(650);
|
||||
const [displayObstacleX, setDisplayObstacleX] = useState(GAME_WIDTH);
|
||||
|
||||
// RWD State: Skala
|
||||
const [scale, setScale] = useState(1);
|
||||
|
||||
const isNight = Math.floor(score / 10) % 2 === 1;
|
||||
|
||||
// Referencje fizyczne
|
||||
const kittyYRef = useRef(0);
|
||||
const obstacleXRef = useRef(650);
|
||||
const obstacleXRef = useRef(GAME_WIDTH);
|
||||
const velocityRef = useRef(0);
|
||||
const scoreRef = useRef(0);
|
||||
const requestRef = useRef<number>(0);
|
||||
const lastTimeRef = useRef<number>(0);
|
||||
|
||||
// STAŁE KONFIGURACYJNE (Wartości na sekundę - niezależne od Hz)
|
||||
const GRAVITY = 1800; // Kot spadnie o 1800px w ciągu sekundy (jeśli nie ma prędkości początkowej)
|
||||
const JUMP_FORCE = -550; // Prędkość startowa skoku
|
||||
const INITIAL_SPEED = 380; // Prędkość przeszkody w px/s
|
||||
const SPEED_INCREMENT = 12; // Przyspieszenie px/s na każdy punkt
|
||||
// STAŁE KONFIGURACYJNE
|
||||
const GRAVITY = 1800;
|
||||
const JUMP_FORCE = -550;
|
||||
const INITIAL_SPEED = 380;
|
||||
const SPEED_INCREMENT = 12;
|
||||
const GROUND_Y = 0;
|
||||
|
||||
// --- RWD LOGIC ---
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
const availableWidth = window.innerWidth - 32; // marginesy boczne
|
||||
const availableHeight = window.innerHeight - 100; // miejsce na nagłówek
|
||||
|
||||
const scaleX = availableWidth / GAME_WIDTH;
|
||||
const scaleY = availableHeight / GAME_HEIGHT;
|
||||
|
||||
// Skalujemy w dół jeśli ekran jest mały, ale max 1 (nie powiększamy na dużych ekranach)
|
||||
setScale(Math.min(scaleX, scaleY, 1));
|
||||
};
|
||||
|
||||
window.addEventListener('resize', handleResize);
|
||||
handleResize();
|
||||
return () => window.removeEventListener('resize', handleResize);
|
||||
}, []);
|
||||
|
||||
const endGame = useCallback(() => {
|
||||
setGameOver(true);
|
||||
setIsPlaying(false);
|
||||
@@ -95,12 +121,11 @@ export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
setScore(0);
|
||||
scoreRef.current = 0;
|
||||
kittyYRef.current = 0;
|
||||
obstacleXRef.current = 700;
|
||||
obstacleXRef.current = GAME_WIDTH + 50;
|
||||
velocityRef.current = 0;
|
||||
// Kluczowe: inicjalizacja czasu startu
|
||||
lastTimeRef.current = performance.now();
|
||||
setDisplayKittyY(0);
|
||||
setDisplayObstacleX(700);
|
||||
setDisplayObstacleX(GAME_WIDTH + 50);
|
||||
}, []);
|
||||
|
||||
const jump = useCallback(() => {
|
||||
@@ -109,19 +134,23 @@ export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
}
|
||||
}, [gameOver, isPlaying]);
|
||||
|
||||
// --- OBSŁUGA WEJŚCIA (Touch & Mouse) ---
|
||||
const handleAction = useCallback((e?: React.SyntheticEvent) => {
|
||||
// Nie używamy e.preventDefault() tutaj, bo CSS touch-action załatwia sprawę
|
||||
if (e) e.stopPropagation();
|
||||
|
||||
if (!isPlaying || gameOver) startGame(); else jump();
|
||||
}, [isPlaying, gameOver, startGame, jump]);
|
||||
|
||||
useEffect(() => {
|
||||
const update = (currentTime: number) => {
|
||||
if (gameOver || !isPlaying) return;
|
||||
|
||||
// --- OBLICZANIE DELTA TIME (Dynamiczna szybkość) ---
|
||||
// dt to czas w sekundach, jaki upłynął od ostatniej klatki (np. 0.016 dla 60Hz, 0.007 dla 144Hz)
|
||||
const dt = (currentTime - lastTimeRef.current) / 1000;
|
||||
lastTimeRef.current = currentTime;
|
||||
|
||||
// Zabezpieczenie przed "skokiem" (np. gdy użytkownik zmieni kartę w przeglądarce)
|
||||
const frameTime = Math.min(dt, 0.1);
|
||||
|
||||
// Fizyka kota (jednostki * czas)
|
||||
// Fizyka
|
||||
velocityRef.current += GRAVITY * frameTime;
|
||||
kittyYRef.current -= velocityRef.current * frameTime;
|
||||
|
||||
@@ -130,24 +159,22 @@ export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
velocityRef.current = 0;
|
||||
}
|
||||
|
||||
// Ruch przeszkody (prędkość * czas)
|
||||
// Przeszkoda
|
||||
const currentSpeed = INITIAL_SPEED + (scoreRef.current * SPEED_INCREMENT);
|
||||
obstacleXRef.current -= currentSpeed * frameTime;
|
||||
|
||||
// Reset przeszkody
|
||||
if (obstacleXRef.current < -80) {
|
||||
obstacleXRef.current = 750;
|
||||
obstacleXRef.current = GAME_WIDTH + 50;
|
||||
scoreRef.current += 1;
|
||||
setScore(scoreRef.current);
|
||||
}
|
||||
|
||||
// Kolizja (strefa trafienia dopasowana do czasu)
|
||||
// Kolizja
|
||||
if (obstacleXRef.current < 100 && obstacleXRef.current > 20 && kittyYRef.current < 45) {
|
||||
endGame();
|
||||
return;
|
||||
}
|
||||
|
||||
// Renderowanie wizualne
|
||||
setDisplayKittyY(kittyYRef.current);
|
||||
setDisplayObstacleX(obstacleXRef.current);
|
||||
|
||||
@@ -164,86 +191,111 @@ export const KittyGame: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
||||
const handleKey = (e: KeyboardEvent) => {
|
||||
if (e.code === 'Space') {
|
||||
e.preventDefault();
|
||||
if (!isPlaying || gameOver) startGame(); else jump();
|
||||
handleAction();
|
||||
}
|
||||
};
|
||||
window.addEventListener('keydown', handleKey);
|
||||
return () => window.removeEventListener('keydown', handleKey);
|
||||
}, [isPlaying, gameOver, startGame, jump]);
|
||||
}, [handleAction]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center min-h-[70vh] p-4 font-sans select-none">
|
||||
<button onClick={onBack} className="mb-6 flex items-center gap-2 text-pink-500 font-bold hover:scale-105 transition-all outline-none">
|
||||
<div className="flex flex-col items-center justify-start pt-8 sm:justify-center min-h-[80vh] w-full font-sans select-none overflow-hidden">
|
||||
|
||||
<button
|
||||
onClick={onBack}
|
||||
className="z-50 mb-6 flex items-center gap-2 text-pink-500 font-bold hover:scale-105 transition-all outline-none bg-white/50 px-4 py-2 rounded-full cursor-pointer"
|
||||
>
|
||||
<ArrowLeft size={20} /> Back
|
||||
</button>
|
||||
|
||||
{/* KONTENER SKALOWANIA */}
|
||||
<div
|
||||
className={`relative w-full max-w-[650px] h-[340px] rounded-[3.5rem] shadow-2xl border-4 transition-colors duration-1000 overflow-hidden cursor-pointer
|
||||
${isNight ? 'bg-slate-950 border-indigo-500 shadow-indigo-500/20' : 'bg-white border-pink-100 shadow-pink-200/50'}`}
|
||||
onClick={() => { if (!isPlaying || gameOver) startGame(); else jump(); }}
|
||||
style={{
|
||||
width: GAME_WIDTH,
|
||||
height: GAME_HEIGHT,
|
||||
transform: `scale(${scale})`,
|
||||
transformOrigin: 'top center',
|
||||
touchAction: 'none' // Zapobiega scrollowaniu na mobile
|
||||
}}
|
||||
className="relative shrink-0"
|
||||
>
|
||||
{/* Niebo */}
|
||||
<div className={`absolute top-10 left-12 transition-all duration-1000 ${isNight ? 'translate-y-0 opacity-100' : '-translate-y-20 opacity-0'}`}>
|
||||
<Moon className="text-indigo-200 fill-indigo-100" size={48} />
|
||||
</div>
|
||||
<div className={`absolute top-10 left-12 transition-all duration-1000 ${isNight ? '-translate-y-20 opacity-0' : 'translate-y-0 opacity-100'}`}>
|
||||
<Sun className="text-yellow-400 fill-yellow-200" size={48} />
|
||||
</div>
|
||||
|
||||
{/* UI Score */}
|
||||
<div className={`absolute top-6 right-8 text-right z-10 font-black transition-colors duration-1000 ${isNight ? 'text-indigo-100' : 'text-pink-500'}`}>
|
||||
<div className="text-3xl tracking-tighter">Score: {score}</div>
|
||||
<div className={`text-sm flex items-center justify-end gap-1 ${isNight ? 'text-indigo-400' : 'text-pink-300'}`}>
|
||||
<Trophy size={14} /> High: {highScore}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* KOTEK */}
|
||||
<div className="absolute left-10" style={{ bottom: `${displayKittyY + 48}px` }}>
|
||||
<DetailedKitty isJumping={displayKittyY > 2} isNight={isNight} isGameOver={gameOver} />
|
||||
</div>
|
||||
|
||||
{/* PRZESZKODA */}
|
||||
<div
|
||||
className={`absolute bottom-12 w-14 h-14 rounded-full border-2 flex items-center justify-center animate-spin transition-colors duration-1000
|
||||
${isNight ? 'bg-indigo-900 border-indigo-400 shadow-[0_0_15px_rgba(129,140,248,0.3)]' : 'bg-pink-100 border-pink-300'}`}
|
||||
style={{ left: `${displayObstacleX}px` }}
|
||||
className={`relative w-full h-full rounded-[3.5rem] shadow-2xl border-4 transition-colors duration-1000 overflow-hidden cursor-pointer
|
||||
${isNight ? 'bg-slate-950 border-indigo-500 shadow-indigo-500/20' : 'bg-white border-pink-100 shadow-pink-200/50'}`}
|
||||
// Używamy onPointerDown zamiast onClick/onTouchStart
|
||||
onPointerDown={handleAction}
|
||||
>
|
||||
<div className={`w-10 h-1.5 rounded-full ${isNight ? 'bg-indigo-300' : 'bg-pink-400'} rotate-45`} />
|
||||
<div className={`absolute w-10 h-1.5 rounded-full ${isNight ? 'bg-indigo-300' : 'bg-pink-400'} -rotate-45`} />
|
||||
</div>
|
||||
{/* Niebo */}
|
||||
<div className={`absolute top-10 left-12 transition-all duration-1000 ${isNight ? 'translate-y-0 opacity-100' : '-translate-y-20 opacity-0'}`}>
|
||||
<Moon className="text-indigo-200 fill-indigo-100" size={48} />
|
||||
</div>
|
||||
<div className={`absolute top-10 left-12 transition-all duration-1000 ${isNight ? '-translate-y-20 opacity-0' : 'translate-y-0 opacity-100'}`}>
|
||||
<Sun className="text-yellow-400 fill-yellow-200" size={48} />
|
||||
</div>
|
||||
|
||||
{/* Ziemia */}
|
||||
<div className={`absolute bottom-0 w-full h-12 border-t-4 transition-colors duration-1000 flex items-center justify-around
|
||||
${isNight ? 'bg-slate-900 border-indigo-900 text-indigo-950' : 'bg-pink-50 border-pink-100 text-pink-100'}`}>
|
||||
{[...Array(10)].map((_, i) => <span key={i} className="text-2xl grayscale opacity-50">🐾</span>)}
|
||||
</div>
|
||||
|
||||
{/* Ekrany start/stop */}
|
||||
{!isPlaying && !gameOver && (
|
||||
<div className="absolute inset-0 bg-white/20 backdrop-blur-[4px] flex items-center justify-center z-20">
|
||||
<div className="bg-white p-12 rounded-[3.5rem] shadow-2xl flex flex-col items-center border-4 border-pink-100 transform hover:scale-105 transition-transform">
|
||||
<Sparkles className="text-yellow-400 mb-4 animate-pulse" size={50} />
|
||||
<button className="bg-pink-500 text-white px-12 py-5 rounded-2xl font-black text-2xl shadow-lg hover:bg-pink-600 transition-colors">
|
||||
START
|
||||
</button>
|
||||
<p className="mt-4 text-slate-400 font-bold text-sm uppercase tracking-widest">Click or Space</p>
|
||||
{/* UI Score */}
|
||||
<div className={`absolute top-6 right-8 text-right z-10 font-black transition-colors duration-1000 ${isNight ? 'text-indigo-100' : 'text-pink-500'}`}>
|
||||
<div className="text-3xl tracking-tighter">Score: {score}</div>
|
||||
<div className={`text-sm flex items-center justify-end gap-1 ${isNight ? 'text-indigo-400' : 'text-pink-300'}`}>
|
||||
<Trophy size={14} /> High: {highScore}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{gameOver && (
|
||||
<div className={`absolute inset-0 backdrop-blur-md flex flex-col items-center justify-center text-center p-6 z-30 transition-colors duration-1000
|
||||
${isNight ? 'bg-slate-950/90' : 'bg-pink-50/90'}`}>
|
||||
<h3 className={`text-6xl font-black mb-4 ${isNight ? 'text-white' : 'text-pink-600'}`}>Oh No! 😿</h3>
|
||||
<div className={`text-3xl font-bold mb-8 ${isNight ? 'text-indigo-300' : 'text-pink-400'}`}>Score: {score}</div>
|
||||
<button className="bg-pink-500 text-white border-4 border-white px-14 py-5 rounded-2xl font-black text-2xl hover:scale-110 transition-all shadow-xl active:scale-95">
|
||||
TRY AGAIN
|
||||
</button>
|
||||
{/* KOTEK */}
|
||||
<div className="absolute left-10 pointer-events-none" style={{ bottom: `${displayKittyY + 48}px` }}>
|
||||
<DetailedKitty isJumping={displayKittyY > 2} isNight={isNight} isGameOver={gameOver} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* PRZESZKODA */}
|
||||
<div
|
||||
className={`absolute bottom-12 w-14 h-14 rounded-full border-2 flex items-center justify-center animate-spin transition-colors duration-1000 pointer-events-none
|
||||
${isNight ? 'bg-indigo-900 border-indigo-400 shadow-[0_0_15px_rgba(129,140,248,0.3)]' : 'bg-pink-100 border-pink-300'}`}
|
||||
style={{ left: `${displayObstacleX}px` }}
|
||||
>
|
||||
<div className={`w-10 h-1.5 rounded-full ${isNight ? 'bg-indigo-300' : 'bg-pink-400'} rotate-45`} />
|
||||
<div className={`absolute w-10 h-1.5 rounded-full ${isNight ? 'bg-indigo-300' : 'bg-pink-400'} -rotate-45`} />
|
||||
</div>
|
||||
|
||||
{/* Ziemia */}
|
||||
<div className={`absolute bottom-0 w-full h-12 border-t-4 transition-colors duration-1000 flex items-center justify-around pointer-events-none
|
||||
${isNight ? 'bg-slate-900 border-indigo-900 text-indigo-950' : 'bg-pink-50 border-pink-100 text-pink-100'}`}>
|
||||
{[...Array(10)].map((_, i) => <span key={i} className="text-2xl grayscale opacity-50">🐾</span>)}
|
||||
</div>
|
||||
|
||||
{/* EKRAN STARTOWY */}
|
||||
{!isPlaying && !gameOver && (
|
||||
<div className="absolute inset-0 bg-white/20 backdrop-blur-[4px] flex items-center justify-center z-20">
|
||||
<div className="bg-white p-12 rounded-[3.5rem] shadow-2xl flex flex-col items-center border-4 border-pink-100 transform hover:scale-105 transition-transform">
|
||||
<Sparkles className="text-yellow-400 mb-4 animate-pulse" size={50} />
|
||||
<button className="bg-pink-500 text-white px-12 py-5 rounded-2xl font-black text-2xl shadow-lg hover:bg-pink-600 transition-colors pointer-events-none">
|
||||
START
|
||||
</button>
|
||||
<p className="mt-4 text-slate-400 font-bold text-sm uppercase tracking-widest">Tap or Space</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* GAME OVER */}
|
||||
{gameOver && (
|
||||
<div className={`absolute inset-0 backdrop-blur-md flex flex-col items-center justify-center text-center p-6 z-30 transition-colors duration-1000
|
||||
${isNight ? 'bg-slate-950/90' : 'bg-pink-50/90'}`}>
|
||||
<h3 className={`text-6xl font-black mb-4 ${isNight ? 'text-white' : 'text-pink-600'}`}>Oh No! 😿</h3>
|
||||
<div className={`text-3xl font-bold mb-8 ${isNight ? 'text-indigo-300' : 'text-pink-400'}`}>Score: {score}</div>
|
||||
<button
|
||||
className="bg-pink-500 text-white border-4 border-white px-14 py-5 rounded-2xl font-black text-2xl hover:scale-110 transition-all shadow-xl active:scale-95 pointer-events-auto"
|
||||
onPointerDown={(e) => { e.stopPropagation(); startGame(); }}
|
||||
>
|
||||
TRY AGAIN
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Informacja dla mobile */}
|
||||
<div className="mt-4 text-xs text-slate-400 sm:hidden">
|
||||
Tap anywhere to jump
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user