From cff143c9250c2fbd9111d0df5ea47c5c69d85ecc Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 2 Feb 2024 16:01:18 +0100 Subject: [PATCH] fix:(game) fixed bananas not removing themselves after intersection --- src/components/Experience.jsx | 3 ++- src/components/PlayerController.jsx | 4 ++-- .../models/items/Banana_peel_mario_kart.jsx | 16 ++++++---------- src/components/store.jsx | 7 ++++++- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/components/Experience.jsx b/src/components/Experience.jsx index 23e9e06..3f464c7 100644 --- a/src/components/Experience.jsx +++ b/src/components/Experience.jsx @@ -48,7 +48,8 @@ export const Experience = () => { ))} diff --git a/src/components/PlayerController.jsx b/src/components/PlayerController.jsx index 3a38216..1ba0b0e 100644 --- a/src/components/PlayerController.jsx +++ b/src/components/PlayerController.jsx @@ -422,7 +422,7 @@ export const PlayerController = () => { // Calculate the position for the new banana const bananaPosition = kartPosition.sub(scaledBackwardDirection); const newBanana = { - id: Math.random() + "-" + new Date(), + id: Math.random() + "-" + +new Date(), position: bananaPosition, player: true, }; @@ -445,7 +445,7 @@ export const PlayerController = () => { // Calculate the position for the new banana const shellPosition = kartPosition.sub(scaledBackwardDirection); const newShell = { - id: Math.random() + "-" + new Date(), + id: Math.random() + "-" + +new Date(), position: shellPosition, player: true, rotation: kartRotation diff --git a/src/components/models/items/Banana_peel_mario_kart.jsx b/src/components/models/items/Banana_peel_mario_kart.jsx index 9ef60ea..6df9a35 100644 --- a/src/components/models/items/Banana_peel_mario_kart.jsx +++ b/src/components/models/items/Banana_peel_mario_kart.jsx @@ -14,13 +14,16 @@ import { BallCollider, RigidBody } from '@react-three/rapier' import { useFrame } from '@react-three/fiber'; import { useStore } from '../../store'; -export function Banana({onCollide, ...props}) { +export function Banana({onCollide, id, ...props}) { const { nodes, materials } = useGLTF('./models/items/banana_peel_mario_kart-transformed.glb'); const rigidBody = useRef(); const ref = useRef(); const [scale, setScale] = React.useState(0.002); + const {actions} = useStore(); + + console.log('banana', id); return ( <> { - - if(scale === 0.002) { + onIntersectionEnter={() => { actions.setShouldSlowDown(true); - console.log(ref.current, rigidBody.current); - ref.current.visible = false; - setScale(0); - rigidBody.setEnable(false); - } - + actions.removeBananaById(id); }} colliders={false} name='banana' diff --git a/src/components/store.jsx b/src/components/store.jsx index 5204548..5142044 100644 --- a/src/components/store.jsx +++ b/src/components/store.jsx @@ -75,12 +75,17 @@ export const useStore = create((set, get) => ({ }, removeBanana: (banana) => { set((state) => ({ - bananas: state.bananas.filter((b) => b.id !== banana.id), + bananas: state.bananas.filter((id) => id !== banana.id), })); }, getBananas: () => { return get().bananas; }, + removeBananaById: (id) => { + set((state) => ({ + bananas: state.bananas.filter((b) => b.id !== id), + })); + }, setItem:() => { set((state) => ({ item: state.items[Math.floor(Math.random() * state.items.length)],