forked from gear-foundation/dapps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 65 additions & 76 deletions
141
frontend/apps/vara-man/src/feature/single-game/Game.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,74 @@ | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useAtom } from 'jotai' | ||
import { useAtom } from 'jotai'; | ||
import { useState } from 'react'; | ||
import { ArrowUp, ArrowDown, ArrowLeft, ArrowRight } from 'lucide-react'; | ||
|
||
import { GameCanvas } from './GameCanvas' | ||
import { GameCanvas } from './GameCanvas'; | ||
import { useGame } from '@/app/context/ctx-game'; | ||
import { Icons } from '@/components/ui/icons' | ||
import { Icons } from '@/components/ui/icons'; | ||
import { GameTimer } from './components/timer'; | ||
|
||
import { IGameLevel } from '@/app/types/game'; | ||
import { calculatePoints } from '../game/utils/calculatePoints'; | ||
import { COINS, GAME_OVER } from '../game/consts'; | ||
|
||
export const Game = () => { | ||
const [searchParams] = useSearchParams() | ||
const [, setGameOver] = useAtom(GAME_OVER) | ||
const { configState } = useGame() | ||
const [coins] = useAtom(COINS); | ||
|
||
const score = configState && calculatePoints(coins, configState, searchParams.get("level") as IGameLevel) | ||
|
||
return ( | ||
<div> | ||
<div className="w-full flex flex-col justify-center items-center"> | ||
<div className="w-[588px] flex justify-between my-3"> | ||
<div className="flex gap-3 items-center"> | ||
<div className="flex gap-3 items-center font-semibold"> | ||
<Icons.statsTimer /> | ||
|
||
<GameTimer /> | ||
</div> | ||
|
||
<div className="flex gap-3 items-center font-semibold"> | ||
<Icons.statsCoins /> | ||
{score} | ||
</div> | ||
|
||
</div> | ||
<div className="flex gap-3 items-center font-semibold cursor-pointer" onClick={() => setGameOver(true)}> | ||
<Icons.exit /> | ||
Exit | ||
</div> | ||
|
||
</div> | ||
|
||
<GameCanvas /> | ||
|
||
<div className="flex gap-5 my-3"> | ||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowUp color='#767676' /> | ||
</div> | ||
|
||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowDown color='#767676' /> | ||
</div> | ||
|
||
<span>Use arrows to move</span> | ||
</div> | ||
|
||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowLeft color='#767676' /> | ||
</div> | ||
|
||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowRight color='#767676' /> | ||
</div> | ||
|
||
<span>Rotate</span> | ||
</div> | ||
|
||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1 px-3 font-bold text-[#726F6F]"> | ||
Shift | ||
</div> | ||
|
||
<span>Hold shift to run</span> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
</div> | ||
) | ||
} | ||
const [searchParams] = useSearchParams(); | ||
const [, setGameOver] = useAtom(GAME_OVER); | ||
const { configState } = useGame(); | ||
const [coins] = useAtom(COINS); | ||
const [gameRestarted, setGameRestarted] = useState(false); | ||
|
||
const score = configState && calculatePoints(coins, configState, searchParams.get('level') as IGameLevel); | ||
|
||
const handleGameRestart = () => { | ||
setGameRestarted((prev) => !prev); | ||
setGameOver(false); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className="w-full flex flex-col justify-center items-center"> | ||
<div className="w-[588px] flex justify-between my-3"> | ||
<div className="flex gap-3 items-center"> | ||
<div className="flex gap-3 items-center font-semibold"> | ||
<Icons.statsTimer /> | ||
<GameTimer gameRestarted={gameRestarted} /> | ||
</div> | ||
<div className="flex gap-3 items-center font-semibold"> | ||
<Icons.statsCoins /> | ||
{score} | ||
</div> | ||
</div> | ||
<div className="flex gap-3 items-center font-semibold cursor-pointer" onClick={() => setGameOver(true)}> | ||
<Icons.exit /> | ||
Exit | ||
</div> | ||
</div> | ||
<GameCanvas onRestart={handleGameRestart} /> | ||
<div className="flex gap-5 my-3"> | ||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowUp color="#767676" /> | ||
</div> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowDown color="#767676" /> | ||
</div> | ||
<span>Use arrows to move</span> | ||
</div> | ||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowLeft color="#767676" /> | ||
</div> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1"> | ||
<ArrowRight color="#767676" /> | ||
</div> | ||
<span>Rotate</span> | ||
</div> | ||
<div className="flex gap-3 items-center"> | ||
<div className="bg-[#DFDFDF] rounded-sm p-1 px-3 font-bold text-[#726F6F]">Shift</div> | ||
<span>Hold shift to run</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 30 additions & 30 deletions
60
frontend/apps/vara-man/src/feature/single-game/components/timer/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,41 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useEffect, useState } from 'react'; | ||
import { useAtom } from 'jotai'; | ||
|
||
import { GAME_OVER } from '@/feature/game/consts'; | ||
|
||
export const GameTimer = () => { | ||
const [, setGameOver] = useAtom(GAME_OVER); | ||
type Props = { | ||
gameRestarted: boolean; | ||
}; | ||
|
||
const [timeLeft, setTimeLeft] = useState(''); | ||
export const GameTimer = ({ gameRestarted }: Props) => { | ||
const [, setGameOver] = useAtom(GAME_OVER); | ||
const [timeLeft, setTimeLeft] = useState(''); | ||
const [startTime, setStartTime] = useState<number | null>(null); | ||
|
||
const [startTime, setStartTime] = useState<number | null>(null); | ||
useEffect(() => { | ||
const start = Date.now(); | ||
setStartTime(start); | ||
|
||
useEffect(() => { | ||
const start = Date.now(); | ||
setStartTime(start); | ||
const totalGameTime = 10 * 60 * 1000; | ||
|
||
const totalGameTime = 10 * 60 * 1000; | ||
const updateTimer = () => { | ||
const now = Date.now(); | ||
const elapsedTime = now - (startTime || now); | ||
const remainingTime = totalGameTime - elapsedTime; | ||
|
||
const updateTimer = () => { | ||
const now = Date.now(); | ||
const elapsedTime = now - (startTime || now); | ||
const remainingTime = totalGameTime - elapsedTime; | ||
if (remainingTime <= 0) { | ||
setTimeLeft('00:00'); | ||
setGameOver(true); | ||
} else { | ||
const minutes = Math.floor((remainingTime / (1000 * 60)) % 60); | ||
const seconds = Math.floor((remainingTime / 1000) % 60); | ||
setTimeLeft(`${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`); | ||
} | ||
}; | ||
|
||
if (remainingTime <= 0) { | ||
setTimeLeft(`00:00`); | ||
setGameOver(true); | ||
} else { | ||
const minutes = Math.floor((remainingTime / (1000 * 60)) % 60); | ||
const seconds = Math.floor((remainingTime / 1000) % 60); | ||
setTimeLeft(`${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`); | ||
} | ||
}; | ||
const timerId = setInterval(updateTimer, 1000); | ||
const timerId = setInterval(updateTimer, 1000); | ||
|
||
return () => clearInterval(timerId); | ||
}, [setGameOver, startTime]); | ||
return () => clearInterval(timerId); | ||
}, [setGameOver, startTime, gameRestarted]); | ||
|
||
return ( | ||
<>{timeLeft}</> | ||
); | ||
} | ||
return <>{timeLeft}</>; | ||
}; |