silphius/app/react/components/dom/damage.jsx

53 lines
1.2 KiB
React
Raw Normal View History

2024-07-27 09:47:17 -05:00
import {memo, useEffect, useState} from 'react';
2024-07-26 18:05:24 -05:00
2024-07-27 10:52:49 -05:00
import {DamageTypes} from '@/ecs/components/vulnerable.js';
2024-07-26 18:05:24 -05:00
import styles from './damage.module.css';
2024-07-27 10:52:49 -05:00
const damageTypeMap = {
[DamageTypes.PAIN]: styles.pain,
[DamageTypes.HEALING]: styles.healing,
[DamageTypes.MANA]: styles.mana,
};
2024-07-27 09:47:17 -05:00
function Damage({
2024-07-26 18:05:24 -05:00
camera,
damage,
scale,
2024-07-27 09:47:17 -05:00
zIndex,
2024-07-26 18:05:24 -05:00
}) {
const [randomness] = useState({
2024-07-27 09:47:17 -05:00
radians: Math.random() * (Math.PI / 16) - (Math.PI / 32),
2024-07-26 18:05:24 -05:00
x: 1 * (Math.random() - 0.5),
2024-07-27 09:47:17 -05:00
y: Math.random(),
})
2024-07-26 18:05:24 -05:00
useEffect(() => {
2024-07-27 09:47:17 -05:00
const handle = setTimeout(() => {
damage.onClose();
}, 1_500);
2024-07-26 18:05:24 -05:00
return () => {
2024-07-27 09:47:17 -05:00
clearTimeout(handle);
2024-07-26 18:05:24 -05:00
};
2024-07-27 09:47:17 -05:00
}, [damage]);
2024-07-26 18:05:24 -05:00
const {amount, position} = damage;
return (
<div
className={styles.damage}
style={{
2024-07-27 12:31:52 -05:00
'--magnitude': Math.max(1, Math.floor(Math.log10(Math.abs(amount)))),
2024-07-27 09:47:17 -05:00
'--positionX': `${position.x * scale - camera.x}px`,
'--positionY': `${position.y * scale - camera.y}px`,
'--randomnessX': randomness.x,
'--randomnessY': randomness.y,
2024-07-27 10:52:49 -05:00
'--shimmer': damageTypeMap[damage.type],
2024-07-27 09:47:17 -05:00
rotate: `${randomness.radians}rad`,
zIndex,
2024-07-26 18:05:24 -05:00
}}
>
2024-07-27 12:31:52 -05:00
<p>{Math.abs(amount)}</p>
2024-07-26 18:05:24 -05:00
</div>
);
}
2024-07-27 09:47:17 -05:00
export default memo(Damage);