1- import { type IRunOptions , NfNotFound } from "@nanoforge-dev/common" ;
1+ import { type IAssetManagerLibrary , type IRunOptions , NfNotFound } from "@nanoforge-dev/common" ;
22import type { ECSClientLibrary , Entity } from "@nanoforge-dev/ecs-client" ;
33
4+ import type { ApplicationConfig } from "../../../core/src/application/application-config" ;
45import { EventEmitter } from "../common/context/event-emitter" ;
56import { CoreEvents } from "../common/context/events/core-events" ;
67import type { Save } from "../common/context/save.type" ;
@@ -9,19 +10,24 @@ import type { Core } from "../core/core";
910export class CoreEditor {
1011 public eventEmitter : EventEmitter ;
1112 private ecsLibrary : ECSClientLibrary ;
13+ private assetLibrary : IAssetManagerLibrary ;
1214 private lastLoadedSave : Save ;
1315 private core : Core ;
1416 private _isPaused : boolean = false ;
1517
16- constructor ( core : Core , editor : IRunOptions [ "editor" ] , ecsLibrary : ECSClientLibrary ) {
18+ constructor ( core : Core , editor : IRunOptions [ "editor" ] , config : ApplicationConfig ) {
1719 this . eventEmitter = new EventEmitter ( editor ) ;
1820 this . lastLoadedSave = JSON . parse ( JSON . stringify ( editor . save ) ) ;
19- this . ecsLibrary = ecsLibrary ;
21+
22+ this . ecsLibrary = config . getComponentSystemLibrary < ECSClientLibrary > ( ) . library ;
23+ this . assetLibrary = config . getAssetManagerLibrary ( ) . library ;
24+
2025 this . eventEmitter . on ( CoreEvents . HOT_RELOAD , this . hotReloadEvent . bind ( this ) ) ;
2126 this . eventEmitter . on ( CoreEvents . HARD_RELOAD , this . hardReloadEvent . bind ( this ) ) ;
2227 this . eventEmitter . on ( CoreEvents . PAUSE_GAME , this . pauseGameEvent . bind ( this ) ) ;
2328 this . eventEmitter . on ( CoreEvents . STOP_GAME , this . stopGameEvent . bind ( this ) ) ;
2429 this . eventEmitter . on ( CoreEvents . UNPAUSE_GAME , this . unpauseGameEvent . bind ( this ) ) ;
30+
2531 this . core = core ;
2632 }
2733
@@ -34,32 +40,26 @@ export class CoreEditor {
3440 }
3541
3642 public hotReloadEvent ( save : Save ) : void {
37- const reg = this . ecsLibrary . registry ;
38- save . entities . forEach ( ( { id, components } ) => {
39- Object . entries ( components ) . forEach ( ( [ componentName , params ] ) => {
40- const ogComponent = save . components . find ( ( { name } ) => name === componentName ) ;
41- if ( ! ogComponent ) {
42- throw new NfNotFound ( "Component: " + componentName + " not found in saved components" ) ;
43- }
44- const ecsEntity : Entity = this . getEntityFromEntityId ( id ) ;
45- const ecsComponent = reg . getEntityComponent ( ecsEntity , {
46- name : componentName ,
47- } ) ;
48- Object . entries ( params ) . forEach ( ( [ paramName , paramValue ] ) => {
49- const lastLoadedParam = this . lastLoadedSave . entities . find ( ( e ) => e . id === id ) ?. components [
50- componentName
51- ] ?. [ paramName ] ;
52- if ( lastLoadedParam !== paramValue ) ecsComponent [ paramName ] = paramValue ;
53- } ) ;
54- reg . addComponent ( ecsEntity , ecsComponent ) ;
55- } ) ;
56- } ) ;
57- this . lastLoadedSave = JSON . parse ( JSON . stringify ( save ) ) ;
43+ this . reloadEvent ( save , false ) ;
5844 }
5945
6046 public hardReloadEvent ( save : Save ) : void {
47+ this . reloadEvent ( save , true ) ;
48+ }
49+
50+ public pauseGameEvent ( ) : void {
51+ this . _isPaused = true ;
52+ }
53+ public unpauseGameEvent ( ) : void {
54+ this . _isPaused = false ;
55+ }
56+
57+ public stopGameEvent ( ) : void {
58+ this . core . getExecutionContext ( ) . application . setIsRunning ( false ) ;
59+ }
60+
61+ private reloadEvent ( save : Save , hard : boolean ) : void {
6162 const reg = this . ecsLibrary . registry ;
62- this . lastLoadedSave = JSON . parse ( JSON . stringify ( save ) ) ;
6363 save . entities . forEach ( ( { id, components } ) => {
6464 Object . entries ( components ) . forEach ( ( [ componentName , params ] ) => {
6565 const ogComponent = save . components . find ( ( { name } ) => name === componentName ) ;
@@ -71,22 +71,25 @@ export class CoreEditor {
7171 name : componentName ,
7272 } ) ;
7373 Object . entries ( params ) . forEach ( ( [ paramName , paramValue ] ) => {
74- ecsComponent [ paramName ] = paramValue ;
74+ if ( ! hard ) {
75+ const lastLoadedParam = this . lastLoadedSave . entities . find ( ( e ) => e . id === id )
76+ ?. components [ componentName ] ?. [ paramName ] ;
77+ if ( lastLoadedParam === paramValue ) return ;
78+ }
79+
80+ const ogParam = ogComponent . paramsNames . find (
81+ ( param ) => param === paramName || param === `__RESERVED_ASSET_${ paramName } ` ,
82+ ) ;
83+ if ( ! ogParam ) return ;
84+
85+ ecsComponent [ paramName ] = ogParam . startsWith ( "__RESERVED_ASSET_" )
86+ ? this . assetLibrary . getAsset ( paramValue )
87+ : paramValue ;
7588 } ) ;
7689 reg . addComponent ( ecsEntity , ecsComponent ) ;
7790 } ) ;
7891 } ) ;
79- }
80-
81- public pauseGameEvent ( ) : void {
82- this . _isPaused = true ;
83- }
84- public unpauseGameEvent ( ) : void {
85- this . _isPaused = false ;
86- }
87-
88- public stopGameEvent ( ) : void {
89- this . core . getExecutionContext ( ) . application . setIsRunning ( false ) ;
92+ this . lastLoadedSave = JSON . parse ( JSON . stringify ( save ) ) ;
9093 }
9194
9295 private getEntityFromEntityId ( entityId : string ) : Entity {
0 commit comments