-
Notifications
You must be signed in to change notification settings - Fork 1
/
Powerups.tsx
93 lines (85 loc) · 2.65 KB
/
Powerups.tsx
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
import React, { useEffect } from "react";
import revealBonus from "../assets/reveal bonus.wav";
import shuffle from "../assets/shuffle.wav";
import spellWaves from "../assets/spell waves.wav";
import type { PowerUpsProps, Player } from "../types/types";
import { PowerUpButton } from "./PowerUpButton";
import { useSound } from "../hooks/useSound";
import attractImg from "../assets/attract.png";
import autoLimbImg from "../assets/auto-limb.png";
import shuffleImg from "../assets/shuffle.png";
export const Powerups: React.FC<PowerUpsProps> = ({
game,
activeCardIndex,
player,
}) => {
const { controlsOrder: oldControlsOrder } = game.oldGame.players.find(
(player: Player) => player.playerId === game.yourPlayerId,
);
const { correctStreak, controlsOrder } = player;
const shuffleAudio = useSound(shuffle);
const spellWavesAudio = useSound(spellWaves);
const revealBonusAudio = useSound(revealBonus);
const runPowerup = (powerup: string, cost: number) => {
if (correctStreak >= cost) {
switch (powerup) {
case "shuffle":
shuffleAudio.play();
Rune.actions.shuffleEnemyControls();
break;
case "attract":
revealBonusAudio.play();
Rune.actions.toggleAttract();
break;
case "autoLimb":
revealBonusAudio.play();
Rune.actions.toggleAutoLimb({
activeCardIndex: activeCardIndex,
});
break;
}
Rune.actions.resetStreak();
}
};
useEffect(() => {
const playerControlsShuffled =
oldControlsOrder[0] !== controlsOrder[0] ||
oldControlsOrder[1] !== controlsOrder[1] ||
oldControlsOrder[2] !== controlsOrder[2] ||
oldControlsOrder[3] !== controlsOrder[3];
if (playerControlsShuffled) {
shuffleAudio.play();
}
}, [controlsOrder, oldControlsOrder]);
useEffect(() => {
correctStreak >= 1 || (correctStreak >= 2 && spellWavesAudio.play());
}, [correctStreak]);
return (
<div className="flex h-fit gap-2 bg-black/0">
<PowerUpButton
powerUp="attract"
imageSource={attractImg}
soundEffect={revealBonus}
correctStreak={correctStreak}
cost={1}
onClickHandler={runPowerup}
/>
<PowerUpButton
powerUp="autoLimb"
imageSource={autoLimbImg}
soundEffect={revealBonus}
correctStreak={correctStreak}
cost={2}
onClickHandler={runPowerup}
/>
<PowerUpButton
powerUp="shuffle"
imageSource={shuffleImg}
soundEffect={shuffle}
correctStreak={correctStreak}
cost={3}
onClickHandler={runPowerup}
/>
</div>
);
};