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

Commit

Permalink
moving start/stop playmode into its own functions file, try exiting p…
Browse files Browse the repository at this point in the history
…lay mode on scene loading
  • Loading branch information
SamMazerIR committed Aug 16, 2024
1 parent 1bf2882 commit db8de10
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
2 changes: 2 additions & 0 deletions packages/editor/src/functions/sceneFunctions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { GLTFSourceState } from '@etherealengine/engine/src/gltf/GLTFState'
import { handleScenePaths } from '@etherealengine/engine/src/scene/functions/GLTFConversion'
import { getMutableState, getState } from '@etherealengine/hyperflux'
import { EngineState } from '@etherealengine/spatial/src/EngineState'
import { tryStopPlayMode } from '@etherealengine/spatial/src/common/functions/PlayModeFunctions'
import { SceneComponent } from '@etherealengine/spatial/src/renderer/components/SceneComponents'
import { Params } from '@feathersjs/feathers'
import { EditorState } from '../services/EditorServices'
Expand Down Expand Up @@ -190,6 +191,7 @@ export const onNewScene = async (
}

export const setCurrentEditorScene = (sceneURL: string, uuid: EntityUUID) => {
tryStopPlayMode()
const gltfEntity = GLTFSourceState.load(sceneURL, uuid, getState(EngineState).originEntity)
setComponent(gltfEntity, SceneComponent)
getMutableState(EditorState).rootEntity.set(gltfEntity)
Expand Down
58 changes: 58 additions & 0 deletions packages/spatial/src/common/functions/PlayModeFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AuthState } from '@etherealengine/client-core/src/user/services/AuthService'
import { Engine, UUIDComponent, getComponent, removeComponent, removeEntity } from '@etherealengine/ecs'
import { TransformGizmoControlledComponent } from '@etherealengine/editor/src/classes/TransformGizmoControlledComponent'
import { EditorState } from '@etherealengine/editor/src/services/EditorServices'
import { transformGizmoControlledQuery } from '@etherealengine/editor/src/systems/GizmoSystem'
import { VisualScriptActions, visualScriptQuery } from '@etherealengine/engine'
import { AvatarComponent } from '@etherealengine/engine/src/avatar/components/AvatarComponent'
import { getRandomSpawnPoint } from '@etherealengine/engine/src/avatar/functions/getSpawnPoint'
import { spawnLocalAvatarInWorld } from '@etherealengine/engine/src/avatar/functions/receiveJoinWorld'
import { dispatchAction, getMutableState, getState } from '@etherealengine/hyperflux'
import { WorldNetworkAction } from '@etherealengine/network'
import { EngineState } from '../../EngineState'
import { FollowCameraComponent } from '../../camera/components/FollowCameraComponent'
import { TargetCameraRotationComponent } from '../../camera/components/TargetCameraRotationComponent'
import { ComputedTransformComponent } from '../../transform/components/ComputedTransformComponent'

/**
* Returns true if we stopped play mode, false if we were not in play mode
*/
export const tryStopPlayMode = (): boolean => {
const entity = AvatarComponent.getSelfAvatarEntity()
if (entity) {
dispatchAction(WorldNetworkAction.destroyEntity({ entityUUID: getComponent(entity, UUIDComponent) }))
removeEntity(entity)
const viewerEntity = getState(EngineState).viewerEntity
removeComponent(viewerEntity, ComputedTransformComponent)
removeComponent(viewerEntity, FollowCameraComponent)
removeComponent(viewerEntity, TargetCameraRotationComponent)
getMutableState(EngineState).isEditing.set(true)
visualScriptQuery().forEach((entity) => dispatchAction(VisualScriptActions.stop({ entity })))
// stop all visual script logic
}
return !!entity
}

export const startPlayMode = () => {
const authState = getState(AuthState)
// const authState = useHookstate(getMutableState(AuthState))
const avatarDetails = authState.user.avatar //.value

const avatarSpawnPose = getRandomSpawnPoint(Engine.instance.userID)
const currentScene = getComponent(getState(EditorState).rootEntity, UUIDComponent)

if (avatarDetails)
spawnLocalAvatarInWorld({
parentUUID: currentScene,
avatarSpawnPose,
avatarID: avatarDetails.id!,
name: authState.user.name //.value
})

// todo
getMutableState(EngineState).isEditing.set(false)
// run all visual script logic
visualScriptQuery().forEach((entity) => dispatchAction(VisualScriptActions.execute({ entity })))
transformGizmoControlledQuery().forEach((entity) => removeComponent(entity, TransformGizmoControlledComponent))
//just remove all gizmo in the scene
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,9 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import { AuthState } from '@etherealengine/client-core/src/user/services/AuthService'
import { UUIDComponent } from '@etherealengine/ecs'
import { getComponent, removeComponent } from '@etherealengine/ecs/src/ComponentFunctions'
import { Engine } from '@etherealengine/ecs/src/Engine'
import { removeEntity } from '@etherealengine/ecs/src/EntityFunctions'
import { TransformGizmoControlledComponent } from '@etherealengine/editor/src/classes/TransformGizmoControlledComponent'
import { EditorState } from '@etherealengine/editor/src/services/EditorServices'
import { transformGizmoControlledQuery } from '@etherealengine/editor/src/systems/GizmoSystem'
import { VisualScriptActions, visualScriptQuery } from '@etherealengine/engine'
import { AvatarComponent } from '@etherealengine/engine/src/avatar/components/AvatarComponent'
import { getRandomSpawnPoint } from '@etherealengine/engine/src/avatar/functions/getSpawnPoint'
import { spawnLocalAvatarInWorld } from '@etherealengine/engine/src/avatar/functions/receiveJoinWorld'
import { dispatchAction, getMutableState, getState, useHookstate } from '@etherealengine/hyperflux'
import { WorldNetworkAction } from '@etherealengine/network'
import { getMutableState, useHookstate } from '@etherealengine/hyperflux'
import { EngineState } from '@etherealengine/spatial/src/EngineState'
import { FollowCameraComponent } from '@etherealengine/spatial/src/camera/components/FollowCameraComponent'
import { TargetCameraRotationComponent } from '@etherealengine/spatial/src/camera/components/TargetCameraRotationComponent'
import { ComputedTransformComponent } from '@etherealengine/spatial/src/transform/components/ComputedTransformComponent'
import { startPlayMode, tryStopPlayMode } from '@etherealengine/spatial/src/common/functions/PlayModeFunctions'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { HiOutlinePause, HiOutlinePlay } from 'react-icons/hi2'
Expand All @@ -51,39 +36,10 @@ const PlayModeTool = () => {
const { t } = useTranslation()

const isEditing = useHookstate(getMutableState(EngineState).isEditing)
const authState = useHookstate(getMutableState(AuthState))

const onTogglePlayMode = () => {
const entity = AvatarComponent.getSelfAvatarEntity()
if (entity) {
dispatchAction(WorldNetworkAction.destroyEntity({ entityUUID: getComponent(entity, UUIDComponent) }))
removeEntity(entity)
removeComponent(Engine.instance.cameraEntity, ComputedTransformComponent)
removeComponent(Engine.instance.cameraEntity, FollowCameraComponent)
removeComponent(Engine.instance.cameraEntity, TargetCameraRotationComponent)
getMutableState(EngineState).isEditing.set(true)
visualScriptQuery().forEach((entity) => dispatchAction(VisualScriptActions.stop({ entity })))
// stop all visual script logic
} else {
const avatarDetails = authState.user.avatar.value

const avatarSpawnPose = getRandomSpawnPoint(Engine.instance.userID)
const currentScene = getComponent(getState(EditorState).rootEntity, UUIDComponent)

if (avatarDetails)
spawnLocalAvatarInWorld({
parentUUID: currentScene,
avatarSpawnPose,
avatarID: avatarDetails.id!,
name: authState.user.name.value
})

// todo
getMutableState(EngineState).isEditing.set(false)
// run all visual script logic
visualScriptQuery().forEach((entity) => dispatchAction(VisualScriptActions.execute({ entity })))
transformGizmoControlledQuery().forEach((entity) => removeComponent(entity, TransformGizmoControlledComponent))
//just remove all gizmo in the scene
if (!tryStopPlayMode()) {
startPlayMode()
}
}

Expand Down

0 comments on commit db8de10

Please sign in to comment.