diff --git a/src/lib/client/project/save-handler.ts b/src/lib/client/project/save-handler.ts index ada63c0..caa6f65 100644 --- a/src/lib/client/project/save-handler.ts +++ b/src/lib/client/project/save-handler.ts @@ -13,6 +13,9 @@ export class SaveHandler { components: [], systems: [], }); + private _readyToSync = true; + private _syncTimer?: Timer; + private _needSync: Writable = writable(false); constructor(project: Project) { this._project = project; @@ -20,16 +23,48 @@ export class SaveHandler { async init() { await this.fetchFromServer(); + this._save.subscribe(() => { + this.syncToServer(); + }); } async fetchFromServer() { this._save.set(await this._project.actions.save.get()); } + async syncToServer() { + if (this._readyToSync) { + this._readyToSync = false; + + await this._project.actions.save.set({ save: get(this._save) }); + + this._syncTimer = setTimeout(() => { + this._readyToSync = true; + if (get(this._needSync)) { + this.syncToServer(); + this._needSync.set(false); + } + }, 5000); + } else { + this._needSync.set(true); + } + } + + async forceSyncToServer() { + this._readyToSync = true; + this._needSync.set(false); + clearTimeout(this._syncTimer); + await this.syncToServer(); + } + get save(): Save { return get(this._save); } + get needSync(): Writable { + return this._needSync; + } + addComponent(component: SaveComponent) { get(this._save).components.push(component); } diff --git a/src/lib/components/Widget/EditorGame/game.svelte.ts b/src/lib/components/Widget/EditorGame/game.svelte.ts index 1ab0de7..a26f074 100644 --- a/src/lib/components/Widget/EditorGame/game.svelte.ts +++ b/src/lib/components/Widget/EditorGame/game.svelte.ts @@ -1,18 +1,3 @@ -import { SvelteMap } from 'svelte/reactivity'; -import { type Writable, writable } from 'svelte/store'; - -import type { Save } from '@utils/types'; - -export const mainModule: Writable = writable(undefined); -export const env: Writable> = writable({}); -export const files: Writable> = writable(new SvelteMap()); -export const save: Writable = writable({ - libraries: [], - entities: [], - components: [], - systems: [], -}); - export enum GameState { INIT_STATE = 0, RELOAD_FROM_SERVER, diff --git a/src/lib/components/Widget/EntityInspector/EntityInspectorWidget.svelte b/src/lib/components/Widget/EntityInspector/EntityInspectorWidget.svelte index 0f4db21..c80d581 100644 --- a/src/lib/components/Widget/EntityInspector/EntityInspectorWidget.svelte +++ b/src/lib/components/Widget/EntityInspector/EntityInspectorWidget.svelte @@ -1,5 +1,4 @@
- {#if $selectedEntity} -
-
- {$selectedEntity.id} -
+ {#key refresh} + {#if $selectedEntity} +
+
+ {$selectedEntity.id} + +
-
- {#each Object.keys($selectedEntity.components) as componentName (componentName)} - - {#if !openMap[componentName]} - {#each packages.getComponentManifest(componentName)?.params as param (param.name)} -
-
{param.name}
+ > + + {componentName} + + + {#if !openMap[componentName]} + {#each packages.getComponentManifest(componentName)?.params as param (param.name)} +
+
{param.name}
- {#if param.type === 'string'} - event.emit(CoreEvents.HOT_RELOAD)} - /> - {:else if param.type === 'number'} - event.emit(CoreEvents.HOT_RELOAD)} - /> - {:else if param.type === 'boolean'} - event.emit(CoreEvents.HOT_RELOAD)} - /> - {/if} -
- {/each} - {/if} - {/each} + {#if param.type === 'string'} + + {:else if param.type === 'number'} + + {:else if param.type === 'boolean'} + + {/if} +
+ {/each} + {/if} + {/each} +
-
-
- +
+ !Object.keys($selectedEntity?.components ?? {}).includes(c.name), + )} + open={openComponentSelector} + onClose={() => (openComponentSelector = false)} + onSelect={(c) => { + addComponent(c.name); + openComponentSelector = false; }} - > - - Add component - -
- !Object.keys($selectedEntity?.components ?? {}).includes(c.name), - )} - open={openComponentSelector} - onClose={() => (openComponentSelector = false)} - onSelect={(c) => { - addComponent(c.name); - openComponentSelector = false; - }} - /> - {:else} -
- Select an entity in the left panel -
- {/if} + /> + {:else} +
+ Select an entity in the left panel +
+ {/if} + {/key} diff --git a/src/lib/components/Widget/EntityInspector/component-creator.ts b/src/lib/components/Widget/EntityInspector/component-creator.ts deleted file mode 100644 index 383e642..0000000 --- a/src/lib/components/Widget/EntityInspector/component-creator.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { get } from 'svelte/store'; - -import { useProject } from '$lib/client/project'; -import { save } from '$lib/components/Widget/EditorGame/game.svelte'; - -import type { SaveComponent } from '@utils/types'; - -export async function createDefaultComponent( - componentName: string, -): Promise<[string, Record]> { - const savedComponent: SaveComponent[] = get(save).components; - const componentToCreate = savedComponent?.find((c) => c.name === componentName); - if (!componentToCreate) throw Error(`Can't create ${componentName}: not found`); - - const { packages } = useProject(); - - const componentManifest = packages.getComponentManifest(componentToCreate.path); - - if (!componentManifest) throw Error(`Can't create ${componentName}: not found`); - - const defaultParams: Record = {}; - componentManifest.params.forEach((param) => { - defaultParams[param.name] = param.default; - }); - return [componentManifest.name, defaultParams]; -}