recent damage counter

This commit is contained in:
Viktor Rådberg
2023-07-06 23:50:03 +02:00
parent 1ee3d801c3
commit 1f9320f938
3 changed files with 55 additions and 10 deletions

View File

@@ -63,6 +63,7 @@ type CommanderDamageBarProps = {
opponents: Player[]; opponents: Player[];
player: Player; player: Player;
onPlayerChange: (updatedPlayer: Player) => void; onPlayerChange: (updatedPlayer: Player) => void;
setLifeTotal: (lifeTotal: number) => void;
}; };
const CommanderDamageBar = ({ const CommanderDamageBar = ({
@@ -70,16 +71,12 @@ const CommanderDamageBar = ({
lifeTotal, lifeTotal,
player, player,
onPlayerChange, onPlayerChange,
setLifeTotal,
}: CommanderDamageBarProps) => { }: CommanderDamageBarProps) => {
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined); const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
const [timeoutFinished, setTimeoutFinished] = useState(false); const [timeoutFinished, setTimeoutFinished] = useState(false);
const [hasPressedDown, setHasPressedDown] = useState(false); const [hasPressedDown, setHasPressedDown] = useState(false);
const handleLifeChange = (updatedLifeTotal: number) => {
const updatedPlayer = { ...player, lifeTotal: updatedLifeTotal };
onPlayerChange(updatedPlayer);
};
const handleCommanderDamageChange = ( const handleCommanderDamageChange = (
index: number, index: number,
increment: number, increment: number,
@@ -99,7 +96,7 @@ const CommanderDamageBar = ({
commanderDamage: updatedCommanderDamage, commanderDamage: updatedCommanderDamage,
}; };
onPlayerChange(updatedPlayer); onPlayerChange(updatedPlayer);
handleLifeChange(lifeTotal - increment); setLifeTotal(lifeTotal - increment);
return; return;
} }
if (currentCommanderDamage.damageTotal === 0 && increment === -1) { if (currentCommanderDamage.damageTotal === 0 && increment === -1) {
@@ -114,7 +111,7 @@ const CommanderDamageBar = ({
commanderDamage: updatedCommanderDamage, commanderDamage: updatedCommanderDamage,
}; };
onPlayerChange(updatedPlayer); onPlayerChange(updatedPlayer);
handleLifeChange(lifeTotal - increment); setLifeTotal(lifeTotal - increment);
}; };
const handleDownInput = (index: number) => { const handleDownInput = (index: number) => {

View File

@@ -1,4 +1,4 @@
import styled from 'styled-components'; import styled, { keyframes } from 'styled-components';
export const LifeCounterWrapper = styled.div<{ backgroundColor?: string }>` export const LifeCounterWrapper = styled.div<{ backgroundColor?: string }>`
position: relative; position: relative;
@@ -51,3 +51,29 @@ export const LifeCounterText = styled.p`
-webkit-user-select: none; -webkit-user-select: none;
-ms-user-select: none; -ms-user-select: none;
`; `;
const fadeOut = keyframes`
0% {
opacity: 1;
}
33% {
opacity: 0.6;
}
100% {
opacity: 0;
}
`;
export const RecentDifference = styled.span`
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
text-shadow: none;
background-color: rgba(255, 255, 255, 0.6);
border-radius: 50%;
padding: 5px 10px;
font-size: 5vmin;
color: #333333;
animation: ${fadeOut} 3s 1s ease-out forwards;
`;

View File

@@ -1,4 +1,4 @@
import { useState } from 'react'; import { useState, useEffect } from 'react';
import * as S from './LifeCounter.style'; import * as S from './LifeCounter.style';
import { Player } from '../../Types/Player'; import { Player } from '../../Types/Player';
import { useSwipeable } from 'react-swipeable'; import { useSwipeable } from 'react-swipeable';
@@ -23,11 +23,24 @@ const LifeCounter = ({
onPlayerChange, onPlayerChange,
}: LifeCounterProps) => { }: LifeCounterProps) => {
const handleLifeChange = (updatedLifeTotal: number) => { const handleLifeChange = (updatedLifeTotal: number) => {
const difference = updatedLifeTotal - player.lifeTotal;
const updatedPlayer = { ...player, lifeTotal: updatedLifeTotal }; const updatedPlayer = { ...player, lifeTotal: updatedLifeTotal };
setRecentDifference(recentDifference + difference);
onPlayerChange(updatedPlayer); onPlayerChange(updatedPlayer);
setKey(Date.now());
}; };
const [showPlayerMenu, setShowPlayerMenu] = useState(false); const [showPlayerMenu, setShowPlayerMenu] = useState(false);
const [recentDifference, setRecentDifference] = useState(0);
const [key, setKey] = useState(Date.now());
useEffect(() => {
const timer = setTimeout(() => {
setRecentDifference(0);
}, 3000); // Adjust the duration as needed (3000ms = 3 seconds)
return () => clearTimeout(timer);
}, [recentDifference]);
const swipeHandlers = useSwipeable({ const swipeHandlers = useSwipeable({
onSwipedUp: () => onSwipedUp: () =>
@@ -44,6 +57,7 @@ const LifeCounter = ({
opponents={opponents} opponents={opponents}
player={player} player={player}
onPlayerChange={onPlayerChange} onPlayerChange={onPlayerChange}
setLifeTotal={handleLifeChange}
/> />
<SettingsButton <SettingsButton
onClick={() => { onClick={() => {
@@ -55,7 +69,15 @@ const LifeCounter = ({
lifeTotal={player.lifeTotal} lifeTotal={player.lifeTotal}
setLifeTotal={handleLifeChange} setLifeTotal={handleLifeChange}
/> />
<S.LifeCounterText>{player.lifeTotal}</S.LifeCounterText> <S.LifeCounterText>
{player.lifeTotal}
{recentDifference !== 0 && (
<S.RecentDifference key={key}>
{recentDifference > 0 ? '+' : ''}
{recentDifference}
</S.RecentDifference>
)}
</S.LifeCounterText>
<AddLifeButton <AddLifeButton
lifeTotal={player.lifeTotal} lifeTotal={player.lifeTotal}
setLifeTotal={handleLifeChange} setLifeTotal={handleLifeChange}