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

IR-3575 playmode on scene change #10985

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
83 changes: 83 additions & 0 deletions packages/spatial/src/common/functions/PlayModeFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
CPAL-1.0 License

The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.

The Original Code is Ethereal Engine.

The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.

All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

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
Loading