@@ -8,7 +8,6 @@ import type { EnvironmentState, WeatherType } from '../../state/types';
88import type { FogSpec } from '../../scene/types' ;
99
1010let _scene : import ( '../../scene/types' ) . IScene | null = null ;
11- let _currentWeather : WeatherType = 'clear' ;
1211
1312interface TransitionState {
1413 fromFog : FogSpec ;
@@ -46,7 +45,7 @@ function rebuildEnvironment(env: EnvironmentState): void {
4645
4746 const current = entityManager . getEntities ( ) ;
4847 for ( const e of current ) {
49- if ( e . id === 'environment' || e . id === 'rain' ) entityManager . detach ( e ) ;
48+ if ( e . id === 'environment' || e . id === 'rain' || e . id === 'mist' ) entityManager . detach ( e ) ;
5049 }
5150
5251 const effective = computeEffectiveEnvironment ( env ) ;
@@ -59,30 +58,58 @@ function rebuildEnvironment(env: EnvironmentState): void {
5958 entityManager . attach ( createEnvironmentEntity ( env ) , s ) ;
6059}
6160
61+ function getActiveEnvironment ( ctx : PluginContext ) : EnvironmentState {
62+ const loc = ctx . state . get ( 'activeLocation' ) as string ;
63+ const envs = ctx . state . get ( 'locations' ) as Record < string , { environment : EnvironmentState } > ;
64+ return envs [ loc ] ?. environment ;
65+ }
66+
67+ /** Apply the current environment state immediately (used by environment editor). */
68+ export function applyEnvironment ( ctx : PluginContext ) : void {
69+ const env = getActiveEnvironment ( ctx ) ;
70+ if ( env ) rebuildEnvironment ( env ) ;
71+ }
72+
6273export const environmentControllerPlugin : ScenePlugin = {
6374 id : 'environment-controller' ,
6475 label : 'Environment Controller' ,
6576 modes : new Set ( [ 'edit' , 'play' ] ) ,
6677 priority : 100 ,
6778
6879 init ( ctx : PluginContext ) {
69- _currentWeather = ( ctx . state . get ( 'environment.weather' ) as WeatherType ) ?? 'clear' ;
70-
71- const initEnv = ctx . state . get ( 'environment' ) as EnvironmentState ;
72- const initEffective = computeEffectiveEnvironment ( initEnv ) ;
73- if ( _scene ) {
74- _scene . fog = initEffective . fog ;
75- if ( initEffective . sky ) {
76- _scene . background = initEffective . sky . gradientTop ;
80+ const initEnv = getActiveEnvironment ( ctx ) ;
81+ if ( initEnv ) {
82+ const initEffective = computeEffectiveEnvironment ( initEnv ) ;
83+ if ( _scene ) {
84+ _scene . fog = initEffective . fog ;
85+ if ( initEffective . sky ) {
86+ _scene . background = initEffective . sky . gradientTop ;
87+ }
7788 }
7889 }
7990
80- ctx . state . watch ( s => s . environment . weather , ( w ) => {
81- if ( w === _currentWeather ) return ;
82- _currentWeather = w ?? 'clear' ;
91+ let _lastLocation = ctx . state . get ( 'activeLocation' ) as string ;
92+ let _lastWeather : WeatherType = initEnv ?. weather ?? 'clear' ;
93+
94+ ctx . state . watch ( s => ( {
95+ loc : s . activeLocation ,
96+ weather : s . locations [ s . activeLocation ] ?. environment . weather ?? 'clear' ,
97+ } ) , ( { loc, weather } ) => {
98+ if ( loc !== _lastLocation ) {
99+ _lastLocation = loc ;
100+ _lastWeather = weather ;
101+ _transition = null ;
102+ const env = getActiveEnvironment ( ctx ) ;
103+ if ( env ) rebuildEnvironment ( env ) ;
104+ return ;
105+ }
83106
84- const baseEnv = ctx . state . get ( 'environment' ) as EnvironmentState ;
85- const toEnv = { ...baseEnv , weather : _currentWeather } ;
107+ if ( weather === _lastWeather ) return ;
108+ _lastWeather = weather ;
109+
110+ const env = getActiveEnvironment ( ctx ) ;
111+ if ( ! env ) return ;
112+ const toEnv = { ...env , weather } ;
86113 const toEffective = computeEffectiveEnvironment ( toEnv ) ;
87114 const fromFog = _scene ?. fog ?? toEffective . fog ;
88115 const fromBg = _scene ?. background ?? toEffective . sky ?. gradientTop ?? '#406888' ;
@@ -98,13 +125,6 @@ export const environmentControllerPlugin: ScenePlugin = {
98125 toEnv,
99126 } ;
100127 } ) ;
101-
102- ctx . state . watch ( s => s . activeLocation , ( ) => {
103- _transition = null ;
104- const env = ctx . state . get ( 'environment' ) as EnvironmentState ;
105- const merged = { ...env , weather : _currentWeather } ;
106- rebuildEnvironment ( merged ) ;
107- } ) ;
108128 } ,
109129
110130 render ( dt : number ) {
0 commit comments