Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
only init physics if it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Aug 17, 2024
1 parent 64f7afd commit 53a61fd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/spatial/src/physics/systems/PhysicsSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { useEffect } from 'react'
import { getComponent, removeComponent, useComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { ECSState } from '@etherealengine/ecs/src/ECSState'
import { Entity } from '@etherealengine/ecs/src/Entity'
import { QueryReactor, defineQuery } from '@etherealengine/ecs/src/QueryFunctions'
import { QueryReactor, defineQuery, useQuery } from '@etherealengine/ecs/src/QueryFunctions'
import { defineSystem } from '@etherealengine/ecs/src/SystemFunctions'
import { SimulationSystemGroup } from '@etherealengine/ecs/src/SystemGroups'
import { getMutableState, getState, none, useHookstate } from '@etherealengine/hyperflux'
Expand Down Expand Up @@ -115,6 +115,8 @@ const PhysicsSceneReactor = () => {

const reactor = () => {
const physicsLoaded = useHookstate(false)
const physicsLoadPending = useHookstate(false)
const physicsQuery = useQuery([SceneComponent])

useEffect(() => {
const networkState = getMutableState(NetworkState)
Expand All @@ -124,15 +126,21 @@ const reactor = () => {
write: PhysicsSerialization.writeRigidBody
})

Physics.load().then(() => {
physicsLoaded.set(true)
})

return () => {
networkState.networkSchema[PhysicsSerialization.ID].set(none)
}
}, [])

useEffect(() => {
if (physicsLoaded.value || physicsLoadPending.value) return
if (physicsQuery.length) {
physicsLoadPending.set(true)
Physics.load().then(() => {
physicsLoaded.set(true)
})
}
}, [physicsQuery])

if (!physicsLoaded.value) return null

return (
Expand Down

0 comments on commit 53a61fd

Please sign in to comment.