fix(game): fixed state management causing a rerender stuttered trigger when boosting with flame Particles
This commit is contained in:
1 parent
fdd821e630
commit
54810a73cd
2 files changed
+56
-47
No files matched your search
@@ -1,22 +1,21 @@
|
|||||||
import React, { useState, useEffect, useRef } from "react";
|
import React, { useRef, useMemo } from "react";
|
||||||
import { useLoader, useFrame } from "@react-three/fiber";
|
import { useLoader, useFrame } from "@react-three/fiber";
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
|
|
||||||
export const FlameParticle = ({ position, png, turboColor, delay = 0 }) => {
|
export const FlameParticle = ({ position, png, isBoosting, delay = 0 }) => {
|
||||||
const texture = useLoader(THREE.TextureLoader, png);
|
const texture = useLoader(THREE.TextureLoader, png);
|
||||||
const pointsRef = useRef();
|
const pointsRef = useRef();
|
||||||
const [size, setSize] = useState(1);
|
const initialized = useRef(false);
|
||||||
const [opacity, setOpacity] = useState(1);
|
|
||||||
const [initialized, setInitialized] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
// Initialize after delay
|
||||||
|
useMemo(() => {
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
setInitialized(true);
|
initialized.current = true;
|
||||||
}, delay);
|
}, delay);
|
||||||
return () => clearTimeout(timer);
|
return () => clearTimeout(timer);
|
||||||
}, [delay]);
|
}, [delay]);
|
||||||
|
|
||||||
const points = React.useMemo(() => {
|
const points = useMemo(() => {
|
||||||
const geom = new THREE.BufferGeometry();
|
const geom = new THREE.BufferGeometry();
|
||||||
geom.setAttribute(
|
geom.setAttribute(
|
||||||
"position",
|
"position",
|
||||||
@@ -26,29 +25,41 @@ export const FlameParticle = ({ position, png, turboColor, delay = 0 }) => {
|
|||||||
}, [position]);
|
}, [position]);
|
||||||
|
|
||||||
useFrame(({clock}, delta) => {
|
useFrame(({clock}, delta) => {
|
||||||
if (!initialized) return;
|
if (!initialized.current) return;
|
||||||
|
|
||||||
pointsRef.current.position.y += 0.03 * delta * 144;
|
const pointsCurrent = pointsRef.current;
|
||||||
pointsRef.current.position.z += 0.06 * delta * 144;
|
|
||||||
if(pointsRef.current.position.y > 0.4) {
|
if (isBoosting) {
|
||||||
pointsRef.current.position.y = 0;
|
// Update logic when boosting
|
||||||
pointsRef.current.position.z = 0;
|
pointsCurrent.position.y += 0.03 * delta * 144;
|
||||||
setOpacity(1);
|
pointsCurrent.position.z += 0.06 * delta * 144;
|
||||||
}
|
|
||||||
if(opacity > 0) {
|
if(pointsCurrent.position.y > 0.4) {
|
||||||
setOpacity((opacity) => opacity - 0.05 * delta * 144);
|
pointsCurrent.position.y = 0;
|
||||||
|
pointsCurrent.position.z = 0;
|
||||||
|
pointsCurrent.material.opacity = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pointsCurrent.material.opacity > 0) {
|
||||||
|
pointsCurrent.material.opacity -= 0.05 * delta * 144;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Reset position and opacity when not boosting
|
||||||
|
pointsCurrent.position.y = 0;
|
||||||
|
pointsCurrent.position.z = 0;
|
||||||
|
pointsCurrent.material.opacity = 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<points ref={pointsRef} geometry={points}>
|
<points ref={pointsRef} geometry={points}>
|
||||||
<pointsMaterial
|
<pointsMaterial
|
||||||
size={size}
|
size={1}
|
||||||
alphaMap={texture}
|
alphaMap={texture}
|
||||||
transparent={true}
|
transparent={true}
|
||||||
depthWrite={false}
|
depthWrite={false}
|
||||||
color={0x000000}
|
color={0x000000}
|
||||||
opacity={opacity}
|
opacity={1}
|
||||||
toneMapped={false}
|
toneMapped={false}
|
||||||
/>
|
/>
|
||||||
</points>
|
</points>
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ import { FlameParticle } from "./FlameParticle";
|
|||||||
|
|
||||||
export const FlameParticles = ({ isBoosting }) => {
|
export const FlameParticles = ({ isBoosting }) => {
|
||||||
|
|
||||||
if (!isBoosting) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<group>
|
<group>
|
||||||
{/* bottom left */}
|
{/* bottom left */}
|
||||||
@@ -12,37 +10,37 @@ export const FlameParticles = ({ isBoosting }) => {
|
|||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={0}
|
delay={0}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={100}
|
delay={100}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={200}
|
delay={200}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={300}
|
delay={300}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={400}
|
delay={400}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={500}
|
delay={500}
|
||||||
/>
|
/>
|
||||||
</group>
|
</group>
|
||||||
@@ -52,37 +50,37 @@ export const FlameParticles = ({ isBoosting }) => {
|
|||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={0}
|
delay={0}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={100}
|
delay={100}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={200}
|
delay={200}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={300}
|
delay={300}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={400}
|
delay={400}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={500}
|
delay={500}
|
||||||
/>
|
/>
|
||||||
</group>
|
</group>
|
||||||
@@ -91,37 +89,37 @@ export const FlameParticles = ({ isBoosting }) => {
|
|||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={0}
|
delay={0}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={100}
|
delay={100}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={200}
|
delay={200}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={300}
|
delay={300}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={400}
|
delay={400}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={500}
|
delay={500}
|
||||||
/>
|
/>
|
||||||
</group>
|
</group>
|
||||||
@@ -129,37 +127,37 @@ export const FlameParticles = ({ isBoosting }) => {
|
|||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={0}
|
delay={0}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={100}
|
delay={100}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={200}
|
delay={200}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={300}
|
delay={300}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_02.png"
|
png="./fire_02.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={400}
|
delay={400}
|
||||||
/>
|
/>
|
||||||
<FlameParticle
|
<FlameParticle
|
||||||
position={[0, 0, 0]}
|
position={[0, 0, 0]}
|
||||||
png="./fire_01.png"
|
png="./fire_01.png"
|
||||||
turboColor={0xfea347}
|
isBoosting={isBoosting}
|
||||||
delay={500}
|
delay={500}
|
||||||
/>
|
/>
|
||||||
</group>
|
</group>
|
||||||
|
|||||||
Reference in new issue
Block a user