1- import React , { useState } from 'react' ; // useState를 import 합니다.
1+ import { useEffect , useRef , useState } from 'react' ; // useState를 import 합니다.
22import { UnityWebGL } from '../../components/UnityWebGL' ;
33import useMediaQueries from "@/hooks/useMediaQueries" ;
4+ import useTallPage from "@/hooks/useTallPage" ;
45import { GameContainer , StartButton , StartContainer , LandingHero , UpButton , UpButtonImage } from './index.styled.ts' ;
56
67
78const Game : React . FC = ( ) => {
89 const [ isGameStarted , setIsGameStarted ] = useState ( false ) ;
9- const [ isLanding , setIsLanding ] = useState ( true ) ;
1010 const { isMobile } = useMediaQueries ( ) ;
1111 const landingImage = 'https://farmsystem-bucket.s3.ap-northeast-2.amazonaws.com/game/DetailGameLanding.png' ;
1212 const upButtonImage = 'https://farmsystem-bucket.s3.ap-northeast-2.amazonaws.com/game/UpGameButton.png' ;
13+ const isTallPage = useTallPage ( 3000 ) ;
14+ const gameContainerRef = useRef < HTMLDivElement | null > ( null ) ;
15+ const [ isGameContainerInView , setIsGameContainerInView ] = useState ( false ) ;
1316 const handleStartGame = ( ) => {
1417 setIsGameStarted ( true ) ;
1518 } ;
1619 const handleScrollTop = ( ) => {
1720 window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
1821 } ;
1922
23+ useEffect ( ( ) => {
24+ if ( isMobile ) return ;
25+ if ( ! gameContainerRef . current ) return ;
26+
27+ const observer = new IntersectionObserver (
28+ ( entries ) => {
29+ const entry = entries [ 0 ] ;
30+ setIsGameContainerInView ( entry . isIntersecting ) ;
31+ } ,
32+ {
33+ root : null ,
34+ rootMargin : '0px' ,
35+ threshold : 0.1 ,
36+ }
37+ ) ;
38+
39+ observer . observe ( gameContainerRef . current ) ;
40+ return ( ) => observer . disconnect ( ) ;
41+ } , [ isMobile ] ) ;
42+
2043 return (
2144 < div
2245 style = { { backgroundColor : '#46D77C' } }
2346 >
24- < GameContainer >
47+ < GameContainer ref = { gameContainerRef } >
2548
2649
2750 { /** isGameStarted 값에 따라 조건부로 렌더링. */ }
@@ -39,16 +62,15 @@ const Game: React.FC = () => {
3962 ) }
4063 </ GameContainer >
4164
42- { ! isMobile && (
65+ { ( ! isMobile ) && (
4366 < LandingHero
4467 $bgDesktop = { landingImage }
4568 $bgMobile = { landingImage }
46- onClick = { ( ) => setIsLanding ( false ) }
4769 >
4870 </ LandingHero >
4971 ) }
50- { ( ! isMobile && isLanding &&
51- < UpButton onClick = { handleScrollTop } title = "맨 위로" >
72+ { ( ! isMobile && isTallPage && ! isGameContainerInView ) && (
73+ < UpButton onClick = { handleScrollTop } title = "맨 위로" >
5274 < UpButtonImage src = { upButtonImage } alt = "Up Button" />
5375 </ UpButton > ) }
5476
0 commit comments