-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from z-korp/dev
Fix + add rank + animation for bonuses
- Loading branch information
Showing
21 changed files
with
422 additions
and
90 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
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useDojo } from "@/dojo/useDojo"; | ||
import { useEffect, useState } from "react"; | ||
import { useEntityQuery } from "@dojoengine/react"; | ||
import { Has, HasValue, getComponentValue } from "@dojoengine/recs"; | ||
import { Game } from "@/dojo/game/models/game"; | ||
|
||
export const useGamesFromTournament = ({ | ||
tournamentId, | ||
}: { | ||
tournamentId: number; | ||
}): { games: Game[] } => { | ||
const [games, setGames] = useState<Game[]>([]); | ||
|
||
const { | ||
setup: { | ||
clientModels: { | ||
models: { Game }, | ||
classes: { Game: GameClass }, | ||
}, | ||
}, | ||
} = useDojo(); | ||
|
||
const gameKeys = useEntityQuery([ | ||
Has(Game), | ||
HasValue(Game, { tournament_id: tournamentId }), | ||
]); | ||
console.log("useGamesFromTournament", tournamentId, gameKeys.length); | ||
|
||
useEffect(() => { | ||
const components = gameKeys | ||
.map((entity) => { | ||
const component = getComponentValue(Game, entity); | ||
if (!component) { | ||
return undefined; | ||
} | ||
return new GameClass(component); | ||
}) | ||
.filter((e) => e !== undefined); | ||
|
||
if (components.length > 0) { | ||
setGames( | ||
components.sort( | ||
(a: Game, b: Game) => b.score_in_tournament - a.score_in_tournament, | ||
), | ||
); | ||
} | ||
}, [gameKeys]); | ||
|
||
return { games }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useGamesFromTournament } from "./useGamesFromTournament"; | ||
|
||
const useRank = ({ | ||
tournamentId, | ||
gameId, | ||
}: { | ||
tournamentId: number; | ||
gameId: string; | ||
}) => { | ||
const [rank, setRank] = useState(0); | ||
const [suffix, setSuffix] = useState("th"); | ||
|
||
const { games } = useGamesFromTournament({ tournamentId }); | ||
|
||
useEffect(() => { | ||
const playerGameIndex = games.findIndex((game) => game.id === gameId); | ||
if (playerGameIndex !== -1) { | ||
const r = playerGameIndex + 1; | ||
setRank(r); | ||
setSuffix(getOrdinalSuffix(r)); | ||
} | ||
}, [games, gameId]); | ||
|
||
function getOrdinalSuffix(rank: number) { | ||
const j = rank % 10, | ||
k = rank % 100; | ||
|
||
let suffix = "th"; // Default suffix | ||
|
||
if (j === 1 && k !== 11) { | ||
suffix = "st"; | ||
} else if (j === 2 && k !== 12) { | ||
suffix = "nd"; | ||
} else if (j === 3 && k !== 13) { | ||
suffix = "rd"; | ||
} | ||
|
||
return suffix; | ||
} | ||
|
||
return { rank, suffix }; | ||
}; | ||
|
||
export default useRank; |
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
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
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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import { motion, AnimatePresence } from "framer-motion"; | ||
import { BonusType, Bonus } from "@/dojo/game/types/bonus"; | ||
|
||
interface BonusAnimationProps { | ||
optimisticScore: number; | ||
optimisticCombo: number; | ||
optimisticMaxCombo: number; | ||
isMdOrLarger: boolean; | ||
} | ||
|
||
const BonusAnimation: React.FC<BonusAnimationProps> = ({ | ||
optimisticScore, | ||
optimisticCombo, | ||
optimisticMaxCombo, | ||
isMdOrLarger, | ||
}) => { | ||
const prevScoreRef = useRef(optimisticScore); | ||
const prevComboRef = useRef(optimisticCombo); | ||
const prevMaxComboRef = useRef(optimisticMaxCombo); | ||
|
||
const [wonBonus, setWonBonus] = useState<BonusType | null>(null); | ||
|
||
const [unlockedBonuses, setUnlockedBonuses] = useState({ | ||
Hammer: false, | ||
Totem: false, | ||
Wave: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (wonBonus) { | ||
const timer = setTimeout(() => { | ||
setWonBonus(null); | ||
}, 1200); | ||
return () => clearTimeout(timer); | ||
} | ||
}, [wonBonus]); | ||
|
||
const getBonusImage = (bonusType: BonusType) => { | ||
const bonus = new Bonus(bonusType); | ||
return bonus.getIcon(); | ||
}; | ||
|
||
// Effect to check and set score-based bonuses | ||
useEffect(() => { | ||
if (!wonBonus) { | ||
if ( | ||
optimisticScore >= 120 && | ||
prevScoreRef.current < 120 && | ||
!unlockedBonuses.Hammer | ||
) { | ||
setWonBonus(BonusType.Hammer); | ||
setUnlockedBonuses((prev) => ({ ...prev, Hammer: true })); | ||
} else if ( | ||
optimisticScore >= 80 && | ||
prevScoreRef.current < 80 && | ||
!unlockedBonuses.Hammer | ||
) { | ||
setWonBonus(BonusType.Hammer); | ||
setUnlockedBonuses((prev) => ({ ...prev, Hammer: true })); | ||
} else if ( | ||
optimisticScore >= 40 && | ||
prevScoreRef.current < 40 && | ||
!unlockedBonuses.Hammer | ||
) { | ||
setWonBonus(BonusType.Hammer); | ||
setUnlockedBonuses((prev) => ({ ...prev, Hammer: true })); | ||
} | ||
} | ||
|
||
prevScoreRef.current = optimisticScore; | ||
}, [optimisticScore, wonBonus, unlockedBonuses]); | ||
|
||
// Effect to check and set combo-based bonuses | ||
useEffect(() => { | ||
if (!wonBonus) { | ||
if ( | ||
optimisticCombo >= 24 && | ||
prevComboRef.current < 24 && | ||
!unlockedBonuses.Wave | ||
) { | ||
setWonBonus(BonusType.Wave); | ||
setUnlockedBonuses((prev) => ({ ...prev, Wave: true })); | ||
} else if ( | ||
optimisticCombo >= 16 && | ||
prevComboRef.current < 16 && | ||
!unlockedBonuses.Wave | ||
) { | ||
setWonBonus(BonusType.Wave); | ||
setUnlockedBonuses((prev) => ({ ...prev, Wave: true })); | ||
} else if ( | ||
optimisticCombo >= 8 && | ||
prevComboRef.current < 8 && | ||
!unlockedBonuses.Wave | ||
) { | ||
setWonBonus(BonusType.Wave); | ||
setUnlockedBonuses((prev) => ({ ...prev, Wave: true })); | ||
} | ||
} | ||
|
||
prevComboRef.current = optimisticCombo; | ||
}, [optimisticCombo, wonBonus, unlockedBonuses]); | ||
|
||
// Effect to check and set maxCombo-based bonuses | ||
useEffect(() => { | ||
if (!wonBonus) { | ||
if ( | ||
optimisticMaxCombo >= 6 && | ||
prevMaxComboRef.current < 6 && | ||
!unlockedBonuses.Totem | ||
) { | ||
setWonBonus(BonusType.Totem); | ||
setUnlockedBonuses((prev) => ({ ...prev, Totem: true })); | ||
} else if ( | ||
optimisticMaxCombo >= 4 && | ||
prevMaxComboRef.current < 4 && | ||
!unlockedBonuses.Totem | ||
) { | ||
setWonBonus(BonusType.Totem); | ||
setUnlockedBonuses((prev) => ({ ...prev, Totem: true })); | ||
} else if ( | ||
optimisticMaxCombo >= 2 && | ||
prevMaxComboRef.current < 2 && | ||
!unlockedBonuses.Totem | ||
) { | ||
setWonBonus(BonusType.Totem); | ||
setUnlockedBonuses((prev) => ({ ...prev, Totem: true })); | ||
} | ||
} | ||
|
||
prevMaxComboRef.current = optimisticMaxCombo; | ||
}, [optimisticMaxCombo, wonBonus, unlockedBonuses]); | ||
|
||
return ( | ||
<AnimatePresence> | ||
{wonBonus && ( | ||
<motion.div | ||
key="bonus-animation" | ||
initial={{ | ||
id: "init", | ||
scale: 0, | ||
rotate: 360, | ||
top: isMdOrLarger ? 200 : 150, | ||
left: "50%", | ||
x: "-50%", | ||
y: "-50%", | ||
opacity: 0, | ||
}} | ||
animate={{ | ||
id: "animate", | ||
scale: isMdOrLarger ? 1 : 0.7, | ||
rotate: 360, | ||
top: isMdOrLarger ? 200 : 150, | ||
left: "50%", | ||
x: "-50%", | ||
y: "-50%", | ||
opacity: 1, | ||
}} | ||
exit={{ | ||
id: "exit", | ||
scale: isMdOrLarger ? 0.27 : 0.23, | ||
rotate: 0, | ||
top: isMdOrLarger ? 40 : 35, | ||
left: | ||
wonBonus === BonusType.Hammer | ||
? "9%" | ||
: wonBonus === BonusType.Wave | ||
? "23%" | ||
: "37%", | ||
x: "-50%", | ||
y: "-50%", | ||
transition: { | ||
type: "spring", | ||
duration: 1, | ||
}, | ||
}} | ||
transition={{ | ||
type: "spring", | ||
stiffness: 150, | ||
damping: 20, | ||
}} | ||
className="absolute flex items-center justify-center z-50" | ||
> | ||
<motion.img | ||
src={getBonusImage(wonBonus)} | ||
alt="Bonus" | ||
className="relative z-10 w-32 h-32" | ||
/> | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
); | ||
}; | ||
|
||
export default BonusAnimation; |
Oops, something went wrong.