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

Commit 059b160

Browse files
committed
begin supporting multiple scenes and tracking dependencies. scene loads via reactor
1 parent 65ec78f commit 059b160

9 files changed

Lines changed: 182 additions & 135 deletions

File tree

packages/engine/src/ecs/classes/Scene.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,42 @@ Ethereal Engine. All Rights Reserved.
2525

2626
import { Color, Texture } from 'three'
2727

28-
import { SceneData } from '@etherealengine/common/src/interfaces/SceneInterface'
29-
import { defineState, getMutableState } from '@etherealengine/hyperflux'
28+
import { defineState, getMutableState, getState } from '@etherealengine/hyperflux'
3029

31-
import { scenePath } from '../../schemas/projects/scene.schema'
30+
import { UUIDComponent } from '../../scene/components/UUIDComponent'
31+
import { SceneDataType, SceneID, scenePath } from '../../schemas/projects/scene.schema'
3232
import { Engine } from './Engine'
33-
import { UndefinedEntity } from './Entity'
33+
34+
export interface StagedScene {
35+
data: SceneDataType
36+
}
3437

3538
export const SceneState = defineState({
3639
name: 'SceneState',
3740
initial: () => ({
38-
sceneData: null as SceneData | null,
39-
sceneEntity: UndefinedEntity,
40-
/** @todo support multiple scenes */
41-
// sceneEntities: {} as Record<string /* SceneID */, EntityUUID>,
41+
scenes: {} as Record<SceneID, StagedScene>,
42+
/** @todo replace activeScene with proper multi-scene support */
43+
activeScene: null as null | SceneID,
4244
background: null as null | Color | Texture
43-
})
45+
}),
46+
47+
loadScene: (sceneID: SceneID, data: SceneDataType) => {
48+
getMutableState(SceneState).scenes[sceneID].set({ data })
49+
getMutableState(SceneState).activeScene.set(sceneID)
50+
},
51+
52+
getRootEntity: (sceneID: SceneID) => {
53+
const scene = getState(SceneState).scenes[sceneID]
54+
return UUIDComponent.entitiesByUUID[scene.data.scene.root]
55+
}
4456
})
4557

4658
export const SceneServices = {
4759
setCurrentScene: async (projectName: string, sceneName: string) => {
4860
const sceneData = await Engine.instance.api
4961
.service(scenePath)
5062
.get(null, { query: { project: projectName, name: sceneName } })
51-
getMutableState(SceneState).sceneData.set(sceneData)
63+
/**@todo replace projectName/sceneName with sceneID once #9119 */
64+
SceneState.loadScene(`${projectName}/${sceneName}` as SceneID, sceneData)
5265
}
5366
}
54-
// export const
55-
56-
// export const getActiveSceneEntity = () => {
57-
// const state = getState(SceneState)
58-
// return UUIDComponent.entitiesByUUID[state.sceneEntities[state.sceneEntity]]
59-
// }

packages/engine/src/ecs/functions/EntityTree.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import {
4545
destroyEntityTree,
4646
findIndexOfEntityNode,
4747
getEntityNodeArrayFromEntities,
48-
initializeSceneEntity,
4948
iterateEntityNode,
5049
removeFromEntityTree,
5150
reparentEntityNode,

packages/engine/src/ecs/functions/EntityTree.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ import { dispatchAction, getMutableState, getState, hookstate, NO_PROXY, none }
3232
import { matchesEntityUUID } from '../../common/functions/MatchesUtils'
3333
import { WorldNetworkAction } from '../../networking/functions/WorldNetworkAction'
3434
import { NetworkState } from '../../networking/NetworkState'
35-
import { NameComponent } from '../../scene/components/NameComponent'
36-
import { SceneTagComponent } from '../../scene/components/SceneTagComponent'
3735
import { UUIDComponent } from '../../scene/components/UUIDComponent'
38-
import { VisibleComponent } from '../../scene/components/VisibleComponent'
3936
import { serializeEntity } from '../../scene/functions/serializeWorld'
4037
import { LocalTransformComponent, TransformComponent } from '../../transform/components/TransformComponent'
4138
import { Engine } from '../classes/Engine'
@@ -51,7 +48,7 @@ import {
5148
removeComponent,
5249
setComponent
5350
} from '../functions/ComponentFunctions'
54-
import { createEntity, entityExists, removeEntity } from '../functions/EntityFunctions'
51+
import { entityExists, removeEntity } from '../functions/EntityFunctions'
5552

5653
type EntityTreeSetType = {
5754
parentEntity: Entity | null
@@ -153,24 +150,6 @@ export const EntityTreeComponent = defineComponent({
153150

154151
export type EntityOrObjectUUID = Entity | string
155152

156-
/**
157-
* Initialize the world with enity tree
158-
* @param scene World
159-
*/
160-
export function initializeSceneEntity(): void {
161-
const oldSceneEntity = getState(SceneState).sceneEntity
162-
if (oldSceneEntity && entityExists(oldSceneEntity)) removeEntity(oldSceneEntity)
163-
164-
const sceneEntity = createEntity()
165-
getMutableState(SceneState).sceneEntity.set(sceneEntity)
166-
setComponent(sceneEntity, NameComponent, 'scene')
167-
setComponent(sceneEntity, VisibleComponent, true)
168-
setComponent(sceneEntity, UUIDComponent, MathUtils.generateUUID() as EntityUUID)
169-
setComponent(sceneEntity, SceneTagComponent, true)
170-
setComponent(sceneEntity, TransformComponent)
171-
setComponent(sceneEntity, EntityTreeComponent, { parentEntity: null })
172-
}
173-
174153
/**
175154
* Recursively destroys all the children entities of the passed entity
176155
*/

packages/engine/src/initializeEngine.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { Engine } from './ecs/classes/Engine'
3232
import { getComponent, setComponent } from './ecs/functions/ComponentFunctions'
3333
import { executeSystems, startCoreSystems } from './ecs/functions/EngineFunctions'
3434
import { createEntity } from './ecs/functions/EntityFunctions'
35-
import { EntityTreeComponent, initializeSceneEntity } from './ecs/functions/EntityTree'
35+
import { EntityTreeComponent } from './ecs/functions/EntityTree'
3636
import { EngineRenderer } from './renderer/WebGLRendererSystem'
3737
import { addObjectToGroup } from './scene/components/GroupComponent'
3838
import { NameComponent } from './scene/components/NameComponent'
@@ -81,8 +81,6 @@ export const createEngine = () => {
8181
camera.matrixAutoUpdate = false
8282
camera.matrixWorldAutoUpdate = false
8383

84-
initializeSceneEntity()
85-
8684
EngineRenderer.instance = new EngineRenderer()
8785
startCoreSystems()
8886
Engine.instance.engineTimer = Timer(executeSystems)

packages/engine/src/scene/components/ChildrenComponent.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/engine/src/scene/components/ParentComponent.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/engine/src/scene/components/SceneDynamicLoadTagComponent.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
2323
Ethereal Engine. All Rights Reserved.
2424
*/
2525

26+
import { EntityUUID } from '@etherealengine/common/src/interfaces/EntityUUID'
27+
import { createState } from '@etherealengine/hyperflux'
2628
import { defineComponent } from '../../ecs/functions/ComponentFunctions'
2729

30+
const entityUUIDLoaded = {} as Record<EntityUUID, boolean>
31+
2832
export const SceneDynamicLoadTagComponent = defineComponent({
2933
name: 'SceneDynamicLoadTagComponent',
3034
jsonID: 'dynamic-load',
@@ -45,5 +49,8 @@ export const SceneDynamicLoadTagComponent = defineComponent({
4549
return {
4650
distance: component.distance.value
4751
}
48-
}
52+
},
53+
54+
entityUUIDLoadedState: createState(entityUUIDLoaded),
55+
entityUUIDLoaded: entityUUIDLoaded as Readonly<typeof entityUUIDLoaded>
4956
})

0 commit comments

Comments
 (0)